Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files

Reply
Thread Tools

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

 
 
Christopher M. Lusardi
Guest
Posts: n/a
 
      09-02-2004
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.
 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      09-02-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"use constant X=>(1,2);" or "use constant X=>[1,2];"? Victor Porton Perl Misc 7 12-11-2007 09:10 PM
DataRelation on Multible layers of DataRelations =?Utf-8?B?SSBhbSBTYW0=?= ASP .Net 3 03-06-2005 04:29 AM
multible email address's with outlook Bill Lenny Computer Support 4 12-22-2003 12:12 PM
multible emails with outlook Bill Lenny Computer Support 4 12-22-2003 03:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57