views:

114

answers:

4

Hi All,

in a pre-compiled header if I do:

#define DS_BUILD
#define PGE_BUILD
#define DEMO

then in source I do:

#if (DS_BUILD && DEMO)
    ---- code---
#elif (PGE_BUILD && DEMO)
    --- code---
#else
    --- code ---
#endif

Do I get an error that states: error: operator '&&' has no right operand

I have never seen this before. I am using XCode 3.2, GCC 4.2 on OS X 10.6.3

+4  A: 

You have to decide first how you want to use your conditional compilation macros. There are normally two popular approaches. It is either

#define A
#define B

#ifdef A
...
#endif

#if defined(A) && defined(B)
...
#endif

or

#define A 1
#define B 0

#if A
...
#endif

#if A && B
...
#endif

I.e. either just define a macro and analyze it with #ifdef and/or #if defined() or define a macro for a numerical value and analyze if with #if.

You are mixing these two approaches in your code sample, which generally makes no sense. Decide which approach you want to use and stick to it.

AndreyT
+13  A: 

You need to add the defined keyword since you want to check that you defined have been defined.

#if defined (DS_BUILD) && defined (DEMO)
    ---- code---
#elif defined (PGE_BUILD) && defined (DEMO)
    --- code---
#else
    --- code ---
#endif
shf301
Alternatively you could #define DEMO 1
Martin Beckett
+2  A: 

The effect of #define DEMO is that during preprocessing every occurence of DEMO is replaced with nothing ( '' ). The same with #define PGE_BUILD. So, in the second sample you posted you effectively get #elif ( && ) which, you agree, doesn't make much sense for compiler:).

pajton
+1  A: 

You need to provide values for DS_BUILD , PGE_BUILD and DEMO, or you need to use ifdef

#define DS_BUILD 1
#define PGE_BUILD 1
#define DEMO 1

defining like above would work

Yogesh Arora