Forum Index
#define needs to be local?

 
Author Message
pjc30943



Joined: 02 Dec 2005

Posted: 06 August 2008, 1:06 AM    Post subject: #define needs to be local?

Is it correct to assume that conditional compilation should be addressed apprioriately if a #define appears in another module?

One of my programs has a #define in the "header" file, but the other modules don't see it. Obviously it's easy to add them to each file, but that partially defeats the purpose...
Back to top
mikep



Joined: 24 Sep 2005
Location: Austin, TX

Posted: 06 August 2008, 1:20 AM    Post subject:

It depends what you are using the #defines for.

If it is a constant (or an expression that evaluates to a constant) then you are probably best served by using a Const.

If you are writing conditional code using #ifdef then you can define global definitons to the compiler by using the -D option. You can also "undefine" values using -U option. These options are probably best placed in the project control file.

Another method is to put the #defines in a single file and then use the #include directive (just like C) to import the definitions into the other source file.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 06 August 2008, 2:07 AM    Post subject: Re: #define needs to be local?

pjc30943 wrote:
Is it correct to assume that conditional compilation should be addressed apprioriately if a #define appears in another module?
Not with respect to its use in a #ifdef or #if defined() construct. The scope of a #define extends from the point at which it appears in a module to the end of the module unless it is undefined in between using #undef.

As Mike suggested, the way to create a "global" #define is to use the -D command line option (typically placed in the .pjt file). If it is placed in the .pjt file, the scope of that definition covers all of the modules appearing after it in the .pjt unless there is an intervening -U to undefine the symbol. The same order applies for -D/-U that appear on the command line.

Consider the .pjt file excerpt below. When mod1.bas and mod2.bas are compiled, #if defined(WITH_DEBUG) will be true. Note, however, that if there is an #undef WITH_DEBUG in mod1.bas, the symbol will be undefined from the line at which it is undefined through the end of the mod1 only. It will still be defined for mod2.bas unless it is also undefined there. The symbol WITH_DEBUG will not be defined in mod3.bas unless it is defined therein.
Code:
-DWITH_DEBUG
mod1.bas
mod2.bas
-UWITH_DEBUG
mod3.bas

The reason that my answer is qualified to the use in a conditional is that you can see the effect of a #define globally if it is used via a global constant. Consider the excerpt below. Since the constant WithDebug is visible in all modules comprising project, all modules will see the value of WITH_DEBUG indirectly through the value of the global constant WithDebug. This is not the same, however, of seeing the actual value of WITH_DEBUG. Note, also, that you could place a #undef WITH_DEBUG after the line defining the constant WithDebug and it will no effect on the constant's value.
Code:
#define WITH_DEBUG
Public Const WithDebug as Boolean = CBool(WITH_DEBUG)
Back to top
pjc30943



Joined: 02 Dec 2005

Posted: 06 August 2008, 20:53 PM    Post subject:

Thank you for the replies; this is much more clear now.
Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group