If I understand this correctly, cocos2d-x is one of several possible APIs.
The preprocessor can check whether a character is defined or not (using #ifdef and the defined operator), and it can evaluate constant integer expressions (using #if ).
One approach would be to define a numerical code for each API:
#define COCOS2X 1 #define ANOTHER_API 2 #define YET_ANOTHER_API 3
and then:
#define API_USED COCOS2DX #if API_USED == COCOS2DX #define USING_COCOS2DX #undef USING_OPENGL #elif API_USED == ANOTHER_API #elif API_USED == YET_ANOTHER_API #endif
Note that I changed the macro name __API_USED to API_USED . Identifiers starting with two underscores (or with an underscore followed by an uppercase letter) are reserved for implementation for all purposes; you should not define them in your own code.
One of the drawbacks of this approach is that spelling errors will not be flagged; the preprocessor quietly replaces undefined identifiers with constant 0 .
It is probably best to have an identifier for each API and define it if and only if this API is used:
#define USING_COCOS2X
and then:
#if USING_API_COCOS2X #undef USING_OPENGL #endif
Testing whether a macro is defined or not is more reliable than checking if it has a specific meaning.
You just need to make sure that the USING_API_* macros (if there are several) are defined sequentially.
source share