Is it possible to compare #ifdef values for conditional use

Jasmine picture Jasmine · Aug 12, 2014 · Viewed 23.1k times · Source

I'm trying to come up with a generic, easy to use way to only run certain code, depending upon the api I am using at the time.

In a header:

#define __API_USED cocos2d-x

#define COCOS2DX  cocos2d-x
#define OPENGL opengl

#ifdef __API_USED == COCOS2DX
    #define USING_COCOS2DX
    #undef USING_OPENGL
#endif

in source:

#ifdef USING_COCOS2DX
    ......
#endif

However I don't think this would work.

Is there a way to accomplish what I am looking to do?

Answer

Ross Ridge picture Ross Ridge · Aug 12, 2014

You can but you need to do it like this:

#define __API_USED COCOS2DX

#define COCOS2DX  1
#define OPENGL 2

#if __API_USED == COCOS2DX
    #define USING_COCOS2DX
    #undef USING_OPENGL
#endif

As Keith Thompson explained undefined tokens (macros) like cocos2d and x evaluate to 0 so you need to define values for the macros you're using.