Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files

  • Thread starter Christopher M. Lusardi
  • Start date
C

Christopher M. Lusardi

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.

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.
 
I

Ian

Christopher said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top