xcconfig: Different preprocessor macros for Debug/Release

≯℡__Kan透↙ 提交于 2019-12-07 08:39:02

问题


I have created and applied a simple .xcconfig file containing

GCC_PREPROCESSOR_DEFINITIONS[config=Debug] = FOODEBUG
GCC_PREPROCESSOR_DEFINITIONS[config=Release] = FOORELEASE

and main.cpp containing

#include <iostream>

// This warning IS shown
#if DEBUG
#warning DEBUG is set to 1
#endif

// This warning IS NOT shown
#ifdef FOODEBUG
#warning FOODEBUG is set
#endif

// This warning IS NOT shown
#ifdef FOORELEASE
#warning FOORELEASE is set
#endif

int main(int argc, const char * argv[])
{
    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}

Now I'm wondering why in main.cpp, neither FOODEBUG nor FOORELEASE are defined ??!

As expected, the build settings show the two lines of my .xcconfig file ("Any Architecture | Any SDK"), but they are not actually used.

How could I achieve that?


回答1:


If you have a preprocessor macro you need to give it a value to be able to use it as you do, see a screenshot of one of my project setups as a sample:

The reason why you can access DEBUG is difference is the different behaviour between #if and #ifdef. #if will be true when the macro exists, #ifdef if it has a non zero value. I suggest to always assign the value one to be save, because I'm not sure the above is true for all compiler versions.

UPDATE:
Did not know that before, but it seems config=Debug does not work. Although the macros get visible in the settings, they do not inherit up. What does work is 2 xcconfig files similar to this:

Release.xcconfig:

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOORELEASE=1

Debug.xcconfig

#include "Release.xcconfig"
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOODEBUG=1

Please also see James Moores answer here: How to append values in xcconfig variables?



来源:https://stackoverflow.com/questions/21727717/xcconfig-different-preprocessor-macros-for-debug-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!