[comp.sys.mac.programmer] Link error : multiply defined variables in THINK C

ginting@fred.cs.washington.edu (Eka Namara Ginting) (08/08/90)

Hi,
  I'm writing a program that has two source files (.c) that import
  a common .h file. This .h file includes another .h file that contains
  some variable declarations. When I try to run this program, THINK C
  compiler complains with "link failed : multiply defined variables" and 
  gives me the list of variables that I declared in the second .h file
  (the one included by the other .h file). These variables are of type
  unsigned long, not initialized, nor of class static.

Any of you care to give me some pointers on this ?
I have succesfully compiled, linked, and run the program under MPW 3.0.
I've tried to use "#ifndef" at the beginning of the .h file, but
still got the same problem.

Thanks a lot,

-- EKa Ginting
ginting@fred.cs.washington.edu

slewis@aerospace.aero.org (Steven Lewis) (08/09/90)

In article <12725@june.cs.washington.edu> ginting@fred.cs.washington.edu (Eka Namara Ginting) writes:
>
>  I'm writing a program that has two source files (.c) that import
>  a common .h file. This .h file includes another .h file that contains
>  some variable declarations. When I try to run this program, THINK C
>  compiler complains with "link failed : multiply defined variables" and 
>  gives me the list of variables that I declared in the second .h file
>  (the one included by the other .h file). These variables are of type
>  unsigned long, not initialized, nor of class static.
>
Did you declare the variables extern in the header file??
failure to do so will cause definition in both files.
another trick is to do something like this:

#undef EXTERN /* make sure this not done earlier */
#ifdef FOO  /* something defined in only one file */
#define EXTERN  /* This actually declares the variable */
#else
#define EXTERN extern /* only make extern call */
#endif FOO
  Now declare all variables EXTERN

also make sure your #ifndef statements are done correctly
while THINK_C