#define needs to be local?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

#define needs to be local?

Post by pjc30943 »

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...
Paul
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

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.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: #define needs to be local?

Post by dkinzer »

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: Select all

-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: Select all

#define WITH_DEBUG
Public Const WithDebug as Boolean = CBool(WITH_DEBUG)
- Don Kinzer
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Post by pjc30943 »

Thank you for the replies; this is much more clear now.
Paul
Post Reply