Search code examples
c++conditional-statementsc-preprocessorpreprocessor-directive

Is it possible to compare #ifdef values for conditional use


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?


Solution

  • 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.