Compilation with preprocessor macros defined with no value

S

Santa Claus

I have the following problem:

I would like for a piece of code to be compiled only if a certain macro
has been defined AND has some specific value. Let me illustrate:

#ifdef SYMBOL
f() ;
#endif

will compile the f() line whenever SYMBOL is defined. Thus, if I have
compiled the file where all this stuff is contained with

-DSYMBOL=1

then f() will be compiled. It will also be compiled if I use

-DSYMBOL=

where no specific value follows the equal sign.

How can I arrange things so that f() does not get compiled in this latter
case?
 
S

Santa Claus

#if SYMBOL == 1
include/define stuff..
#endif

Thanks for your reply, but that's not it. I need to compile if SYMBOL is
defined and has some value - any value. The problem is that by doing
-DSYMBOL= (or -DSYMBOL) no specific value seems to be substituted for
SYMBOL. That is, -DSYMBOL defines SYMBOL, but doesn't assign any value to
it.
 
M

Mark A. Odell

Thanks for your reply, but that's not it. I need to compile if
SYMBOL is
defined and has some value - any value. The problem is that by doing
-DSYMBOL= (or -DSYMBOL) no specific value seems to be substituted for
SYMBOL. That is, -DSYMBOL defines SYMBOL, but doesn't assign any value
to it.

What about

#if defined(SYMBOL) && SYMBOL
f();
#endif
 
S

Santa Claus

What about

#if defined(SYMBOL) && SYMBOL
f();
#endif

I am afraid that doesn't do it either. If SYMBOL is defined but has no
value then the if clause will evaluate to something illegal, for there
will be nothing to the right of the && operator.

Boy, this is an annoying issue!
 
J

j

Santa Claus said:
I have the following problem:

I would like for a piece of code to be compiled only if a certain macro
has been defined AND has some specific value. Let me illustrate:

#ifdef SYMBOL
f() ;
#endif

will compile the f() line whenever SYMBOL is defined. Thus, if I have
compiled the file where all this stuff is contained with

-DSYMBOL=1

then f() will be compiled. It will also be compiled if I use

-DSYMBOL=

where no specific value follows the equal sign.

How can I arrange things so that f() does not get compiled in this latter
case?

#if SYMBOL == 1
include/define stuff..
#endif
 
A

Arthur J. O'Dwyer

I am afraid that doesn't do it either. If SYMBOL is defined but
has no value then the if clause will evaluate to something illegal,
for there will be nothing to the right of the && operator.

Check the Google Groups archive for this newsgroup.
This issue has come up many times before. IIRC, the
most portable way of doing it is to write

#if (SYMBOL-0 == 42)
f();
#endif

but you'd better check the archives first. Basically, a lot
of preprocessors do this in slightly different ways. I don't
know what the current C standard says, nor whether it differs
from C89.

Of course, the easiest thing to do is just to write

#ifdef SYMBOL
#if (SYMBOL == 42)
f();
#endif
#endif

....but why would you ever want to take the *easy* way out when
there's a complicated, obscure, and potentially non-portable
way?

-Arthur
 
S

Santa Claus

I am afraid that doesn't do it either. If SYMBOL is defined but has no
value then the if clause will evaluate to something illegal, for there
will be nothing to the right of the && operator.

Boy, this is an annoying issue!

In case anybody is in the same predicament, I have been given a solution
that works for me.

First, there is a difference between

-DSYMBOL

and
-DSYMBOL=

The first one, -DSYMBOL, makes SYMBOL equal to 1. This is not the case I
am interested in. -DSYMBOL= is. In this case, SYMBOL seems to be
blank. One can proceed as follows:

#if ((SYMBOL - 1) == -1)
#undef SYMBOL
#endif

This works because I know that, whenever there is something non-blank
following the = sign in -DSYMBOL=, it will always be 0 or a positive
integer.
 
R

Rouben Rostamian

I have the following problem:

I would like for a piece of code to be compiled only if a certain
macrohas been defined AND has some specific value. Let me illustrate:

#ifdef SYMBOL
f() ;
#endif

will compile the f() line whenever SYMBOL is defined. Thus, if I have
compiled the file where all this stuff is contained with

-DSYMBOL=1

then f() will be compiled. It will also be compiled if I use

-DSYMBOL=

where no specific value follows the equal sign.

How can I arrange things so that f() does not get compiled in this
latter case?

#define cat(x,y) x ## y
#define xcat(x,y) cat(x,y)
#if !defined(SYMBOL)
you did not define "SYMBOL"
#elif xcat(SYMBOL,1) == 1
you said -DSYMBOL=
#elif xcat(SYMBOL,1) == 11
you said -DSYMBOL or -DSYMBOL=1
#else
you said -DSYMBOL=somethingelse
#endif
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top