473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compilation with preprocessor macros defined with no value

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?
Nov 13 '05 #1
7 2112
j

"Santa Claus" <sa***@NorthPol e.gov> wrote in message
news:pa******** *************** *****@NorthPole .gov...
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
Nov 13 '05 #2
On Wed, 01 Oct 2003 13:15:04 -0700, j wrote:

"Santa Claus" <sa***@NorthPol e.gov> wrote in message
news:pa******** *************** *****@NorthPole .gov...
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


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.


Nov 13 '05 #3
Santa Claus <sa***@NorthPol e.gov> wrote in
news:pa******** *************** *****@NorthPole .gov:
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


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

--
- Mark ->
--
Nov 13 '05 #4
On Wed, 01 Oct 2003 19:32:44 +0000, Mark A. Odell wrote:
Santa Claus <sa***@NorthPol e.gov> wrote in
news:pa******** *************** *****@NorthPole .gov:
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


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


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!

Nov 13 '05 #5
On Wed, 01 Oct 2003 20:02:18 +0000, Santa Claus wrote:
On Wed, 01 Oct 2003 19:32:44 +0000, Mark A. Odell wrote:
Santa Claus <sa***@NorthPol e.gov> wrote in
news:pa******** *************** *****@NorthPole .gov:
> 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

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


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.
Nov 13 '05 #6

On Wed, 1 Oct 2003, Santa Claus wrote:

On Wed, 01 Oct 2003 19:32:44 +0000, Mark A. Odell wrote:
Santa Claus <sa***@NorthPol e.gov> wrote in
> I would like for a piece of code to be compiled only if a certain
> macro has been defined AND has some specific value.


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.


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

Nov 13 '05 #7
In article <pa************ *************** *@NorthPole.gov >,
Santa Claus <sa***@NorthPol e.gov> wrote:
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=somethi ngelse
#endif
--
Rouben Rostamian <ro*******@umbc .edu>
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
9127
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
205
10660
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
13
2132
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int defined = 2;
10
2354
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and begun to use GNU Autotools. I'm sorry to say I'm new to gcc as well :( Now I get the most ridiculous compile error which I'm unable to solve. Can someone, please, help me with this? gcc output together with the files mentioned in the gcc error...
28
2637
by: richardlang | last post by:
Anyone out there ever come across a preprocessor macro that compares an argument value against the predefined __DATE__ macro in order to control conditional compilation based on date. Something along the lines of... # define DateLaterThan(x) ... that could be used for things like
3
4241
by: ezmeralda | last post by:
Hello, I have some code which shall be compiled in .NET v1.1 and v2.0 but which has to be different for the two .NET-Versions. Therefore I need to use a construct like #if NET_20 <2.0 specific code>
8
3641
by: Gowtham | last post by:
Hi, I am trying to write some code which acts differently when compiled on 32 bit and 64 bit machines. To identify the machine type, I am trying to find the sizeof( int ) and comparing it with 32 and 64. But, the compiler is complaining about syntax errors in the #if lines. Errors: 32-64.cpp:7:13: missing binary operator before '('
7
3360
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
8
7079
by: congot | last post by:
How to verify a preprocessor macros defined with no value , and only if defined with a value should the code within that be exectued. #ifdef ABC int a; #endif int a is available only if a value of ABC is available. ABC is defined as #define ABC
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.