开发者

#define and how to use them - C++

In a pre-compiled header if I do:

#define DS_BUILD
#define PGE_BUILD
#define DEMO

then in source I 开发者_StackOverflow社区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


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


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.


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:).


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜