问题描述
我创建并应用了一个简单的 .xcconfig 文件,其中包含
I have created and applied a simple .xcconfig file containing
GCC_PREPROCESSOR_DEFINITIONS[config=Debug] = FOODEBUG
GCC_PREPROCESSOR_DEFINITIONS[config=Release] = FOORELEASE
和 main.cpp 包含
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;
}
现在我想知道为什么在 main.cpp 中,既没有定义 FOODEBUG 也没有定义 FOORELEASE ??!
Now I'm wondering why in main.cpp, neither FOODEBUG nor FOORELEASE are defined ??!
正如预期的那样,构建设置显示了我的 .xcconfig 文件的两行(Any Architecture | Any SDK"),但实际上并未使用它们.
As expected, the build settings show the two lines of my .xcconfig file ("Any Architecture | Any SDK"), but they are not actually used.
我怎么能做到这一点?
推荐答案
如果你有一个预处理器宏,你需要给它一个值才能像你一样使用它,请看我的一个项目设置的屏幕截图示例:
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:
之所以可以访问 DEBUG 是因为 #if
和 #ifdef
之间的行为不同.#if
当宏存在时为真,#ifdef
为非零值.我建议始终分配值 1 以进行保存,因为我不确定以上是否适用于所有编译器版本.
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.
更新:
以前不知道,但似乎 config=Debug
不起作用.尽管宏在设置中可见,但它们不会继承.有效的是 2 个与此类似的 xcconfig 文件:
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:
Release.xcconfig:
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOORELEASE=1
调试.xcconfig
Debug.xcconfig
#include "Release.xcconfig"
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOODEBUG=1
另请参阅 James Moores 的回答:如何在 xcconfig 变量中附加值?
Please also see James Moores answer here: How to append values in xcconfig variables?
这篇关于xcconfig:用于调试/发布的不同预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!