By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ian
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.

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.