[comp.lang.c] Project/Make problem in Turbo C 2.0

ndimas@pikes.Colorado.EDU (Nicholas Dimas) (11/24/90)

Hi, I'm having the following problem with the Project/Make
utility.  I have a header file "define2.h" that contains in it the 
following items:

		#include <stdio.h>
		#include <math.h>
		#define stuff 
		double weights[3]={1 2 3};
		double zeros[3]={1 2 3};

Now I have main with the following items:

		#include "define2.h"

		extern void main_1( void );
	
		main()
		{
		stuff
		main_1();
		}

Now I have main_1() with the following items:

		#include "define2.h"
		
		main_1()
		{
		stuff
		}
The Project/Make file has the name prog.prj and contains the following:  

		prog1.c /* which is main */
		prog2.c /* which is main_1() */

When I press F9 like the manual writes, I get the following errors:

		_weights defined in prog1.c is redefined in prog2.c
		_zeros defined in prog1.c is redefined in prog2.c

I do not understand this error.  Can someone please help me.

Thank You
Nicholas Dimas
e-mail: ndimas@pikes.denver.colorado.edu

hp@vmars.tuwien.ac.at (Peter Holzer) (11/27/90)

ndimas@pikes.Colorado.EDU (Nicholas Dimas) writes:


>Hi, I'm having the following problem with the Project/Make
>utility.  I have a header file "define2.h" that contains in it the 
>following items:

>		#include <stdio.h>
>		#include <math.h>
>		#define stuff 
>		double weights[3]={1 2 3};
>		double zeros[3]={1 2 3};

>Now I have main with the following items:

>		#include "define2.h"

>		extern void main_1( void );
>	
>		main()
>		{
>		stuff
>		main_1();
>		}

>Now I have main_1() with the following items:

>		#include "define2.h"
>		
>		main_1()
>		{
>		stuff
>		}
>The Project/Make file has the name prog.prj and contains the following:  

>		prog1.c /* which is main */
>		prog2.c /* which is main_1() */

>When I press F9 like the manual writes, I get the following errors:

>		_weights defined in prog1.c is redefined in prog2.c
>		_zeros defined in prog1.c is redefined in prog2.c

>I do not understand this error.  Can someone please help me.

The problem is that you define weights and zeros in both prog1.c
and prog2.c (by including define2.c). So when TC links the two
modules it discovers two variables of the same name and does not
know which to use.

The right way to do it is to define them in one of the modules
and only declare them in the header file:

/* define2.h */
#include <stdio.h>
#include <math.h>
#define stuff 
extern double weights[3];
extern double zeros[3];

/* prog1.c */
#include "define2.h"

double weights[3]={1 2 3};
double zeros[3]={1 2 3};

extern void main_1( void );
	
main()
{
	stuff
	main_1();
}

/* prog2.c */
#include "define2.h"

main_1()
{
	stuff
}

This way, the compiler knows that weights and zeros exist
somewhere in both prog1.c and prog2.c but allocates storage for
them only in prog1.c.

>Thank You
You're welcome.
--
|    _  | Peter J. Holzer                       | Think of it   |
| |_|_) | Technical University Vienna           | as evolution  |
| | |   | Dept. for Real-Time Systems           | in action!    |
| __/   | hp@vmars.tuwien.ac.at                 |     Tony Rand |