Christopher M. Lusardi wrote:
> Hello,
>
> How is this possible. If I have two separate files that include the
> same dot h file as described below why am I allowed to access the same
> value for a constant. I compiled this program using a makefile. (Sorry,
> I can't post the long makefile or program.)
>
> What the code is suppose to do is: main () includes the file and the
> variable multible_defined_var actually gets defined. file2.c will get the
> external definition only because now the constant TESTC is defined.
>
> If I fool around with the makefile I can remove the error saying variable
> multible_defined_var is multiply defined and the program works fine.
>
> The reason I have to know is if I create another test program and define
> just a constant without any other pre-compiler directives involved in just
> main() it's undefined in other program files.
>
put the line
int multible_defined_var;
in one of the c files, not the header!
Ian
> file1.c
> -------
>
> #include "test.h"
>
> main ()
> {
> ...
>
> t2 ();
> }
>
>
> test.h
> ------
> #ifndef TESTC
> #define TESTC
> int multible_defined_var;
> #else
> extern int multible_defined_var;
> #endif
>
>
> file2.c
> -------
> #include "test.h"
>
>
> void t2 ()
> {
> ...
> }
>
>
> Thank you,
> Christopher Lusardi
>
> P.S.: I apologize if there is a typo in the code I posted.
|