子项目的 Xcode 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7556558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode environment variables for sub projects
提问by tomj
My current Xcode iOS project uses a number of static libraries. The different code modules in the static libraries have various levels of debug that I can switch on/off with #defines from within that module.
我当前的 Xcode iOS 项目使用了许多静态库。静态库中的不同代码模块具有不同级别的调试,我可以从该模块中使用 #defines 打开/关闭这些调试级别。
What I want to do is have all the debug default to off in the library then set the debug level from the parent project. I want to do this so any proj that uses the lib has to explicitly turn on debug.
我想要做的是将库中的所有调试默认设置为关闭,然后从父项目设置调试级别。我想这样做,所以任何使用 lib 的项目都必须显式打开调试。
So MainProj uses myLib1 and myLib2 etc. Within myLib1 is a module called fooModule. fooModule has debug code such as:
所以 MainProj 使用 myLib1 和 myLib2 等。在 myLib1 中有一个名为 fooModule 的模块。fooModule 具有调试代码,例如:
#if FOOMODULE_DEBUG_LEVEL > 0
//debug code, console logs etc
#endif
I want to be able to define FOOMODULE_DEBUG_LEVEL in the parent project so the library picks it up at build time and compiles appropriately.
我希望能够在父项目中定义 FOOMODULE_DEBUG_LEVEL,以便库在构建时选择它并进行适当的编译。
I have tried:
我试过了:
#define FOOMODULE_DEBUG_LEVEL 1
in the main project .pch and I have tried adding FOOMODULE_DEBUG_LEVEL as a user defined environment variable with a value of 1. Neither of which were picked up by the sub project lib.
在主项目 .pch 中,我尝试添加 FOOMODULE_DEBUG_LEVEL 作为用户定义的环境变量,其值为 1。这两个都没有被子项目库选中。
Is there a way of doing this or am I approaching this in the wrong way?
有没有办法做到这一点,或者我是否以错误的方式接近这个?
回答by justin
you accomplish this without multiple definitions by creating xcconfig files and then referencing or #include
-ing them throughout your projects. so, you could apply Mattias' suggestion and then define the preprocessor defs in the xcconfig. then you have one file to change (and a full rebuild if you require these defs in the pch file, which there are separate settings for).
您可以通过创建 xcconfig 文件然后#include
在整个项目中引用或-ing 它们来完成此操作,而无需多个定义。因此,您可以应用 Mattias 的建议,然后在 xcconfig.xml 文件中定义预处理器 defs。那么您有一个文件要更改(如果您需要 pch 文件中的这些定义,则需要完全重建,这些文件有单独的设置)。
xcode also lets you assign separate xcconfigs per build configuration.
xcode 还允许您为每个构建配置分配单独的 xcconfig。
回答by TRVD1707
I would edit the schema and add a pre-build shell script to set the proper variables. When you add a script you can establish from which target you are getting the definitions.
我会编辑架构并添加一个预构建的 shell 脚本来设置适当的变量。添加脚本时,您可以确定从哪个目标获取定义。
回答by tomj
Solution
解决方案
1) Target > Build Settings > Preprocessor macros. Set environment variable as preprocessor def for target (seems that must be target rather than project), e.g. DEBUG_VARIABLE=1
1) 目标 > 构建设置 > 预处理器宏。将环境变量设置为目标的预处理器 def(似乎必须是目标而不是项目),例如 DEBUG_VARIABLE=1
2) Project > Build phases > Add build phase. Then in the Run Script export the variable: export DEBUG_VARIABLE
2) 项目 > 构建阶段 > 添加构建阶段。然后在运行脚本中导出变量:export DEBUG_VARIABLE
All sub projects now pick up this environment variable.
所有子项目现在都选择这个环境变量。
I think the ideal would be to also use Justin's suggestion of having an .xcconfig file with all the preprocessor macros defined in one place to make it easy to edit them. For the life of me I can't make this work. If I put this in the .xcconfig file: GCC_PREPROCESSOR_DEFINITIONS = DEBUG_VARIABLE=1 $(inherited) Then base the debug and/or release build on this configuration the DEBUG_VARIABLE environment variable never gets set.
我认为理想的做法是也使用 Justin 的建议,即拥有一个 .xcconfig 文件,其中所有预处理器宏都定义在一个地方,以便于编辑它们。对于我的生活,我无法完成这项工作。如果我把它放在 .xcconfig 文件中: GCC_PREPROCESSOR_DEFINITIONS = DEBUG_VARIABLE=1 $(inherited) 然后基于这个配置的调试和/或发布构建 DEBUG_VARIABLE 环境变量永远不会被设置。
回答by Mattias Wadman
Maybe add a define using the "Preprocessor macros" build setting to the targets and or debug/release build configurations where you want to enable debug.
可能使用“预处理器宏”构建设置向目标和/或调试/发布构建配置添加定义,您要在其中启用调试。
In your case you would double click on on the value column and then click "+" to add a new macro. The marco would be "FOOMODULE_DEBUG_LEVEL=1" which should result in -DFOOMODULE_DEBUG_LEVEL=1
to the compiler.
在您的情况下,您可以双击值列,然后单击“+”以添加新宏。marco 将是“FOOMODULE_DEBUG_LEVEL=1”,这应该导致-DFOOMODULE_DEBUG_LEVEL=1
编译器。