通过 Xcode Scheme 添加预处理器定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8319325/
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
Adding a Preprocessor Definition via a Xcode Scheme
提问by Christopher A
I currently have a number of special "flavors" of an iPhone application that I need to build. Ideally, I would like to have a scheme for each "flavor" and each scheme would define (or set) one or more preprocessor definitions that I can use to branch on in my code and possibly even preprocess my info.plist file. This can obviously be done with multiple targets, but since I could have many different "flavors" of the app, it would be great to do it with schemes to keep the target count down. My current thought is to add these preprocessor definitions during a pre-action script, but I can not for the life of me find any way to update GCC_PREPROCESSOR_DEFINITIONS. Since it is an environment variable, shouldn't I have access to append onto GCC_PREPROCESSOR_DEFINITIONS?
我目前有一些我需要构建的 iPhone 应用程序的特殊“风格”。理想情况下,我希望为每个“风味”制定一个方案,每个方案都会定义(或设置)一个或多个预处理器定义,我可以使用这些定义在我的代码中进行分支,甚至可能预处理我的 info.plist 文件。这显然可以通过多个目标来完成,但由于我可以拥有应用程序的许多不同“风格”,因此最好使用方案来减少目标计数。我目前的想法是在预操作脚本期间添加这些预处理器定义,但我终生无法找到任何更新 GCC_PREPROCESSOR_DEFINITIONS 的方法。由于它是一个环境变量,我是否应该有权附加到 GCC_PREPROCESSOR_DEFINITIONS 上?
采纳答案by Christopher A
To meet my requirement of allowing schemes to set preprocessor definitions, the best solution I have come up with is to have scheme pre-action and post-action scripts modify a xcconfig file. This file, in turn, updates the build configuration, setting the preprocessor definitions and will even allow me to define preprocessor definitions to conditionally modify the info.plist. If anyone else goes down this route, make sure you take into account how this file is handled by source control.
为了满足我允许方案设置预处理器定义的要求,我想出的最佳解决方案是让方案前操作和后操作脚本修改 xcconfig 文件。该文件反过来更新构建配置,设置预处理器定义,甚至允许我定义预处理器定义以有条件地修改 info.plist。如果其他人走这条路,请确保考虑源代码管理如何处理此文件。
This article's question and associated answers was helpful to me: How to append values in xcconfig variables?
这篇文章的问题和相关答案对我有帮助:如何在 xcconfig 变量中附加值?
回答by Dustin
Worse case scenario you can do a pre-build script for the scheme. You'll have to include the script for every scheme though:
更糟糕的情况是,您可以为该方案执行预构建脚本。但是,您必须为每个方案包含脚本:
I would prefer to attach it to a Configuration:
我更愿意将它附加到配置:
Then you can easily add preprocessor macros for the various configurations, like I have here for Debug:
然后,您可以轻松地为各种配置添加预处理器宏,就像我在这里为 Debug 所做的那样:
The <ProjectName>_Prefix.pch file is a great place to put macros that effect the whole program, like I have here:
<ProjectName>_Prefix.pch 文件是放置影响整个程序的宏的好地方,就像我在这里:
In my example we're effectively turning off console output when not in debug mode, providing a little speed boost.
在我的示例中,我们在不处于调试模式时有效地关闭了控制台输出,从而提供了一点速度提升。
回答by mask
If I understood your question correctly, you are looking to add some of user defined preprocessor macros to your source code, there is a way to add them in your target using Xcode. (e.g. GCC_PREPROCESSOR_DEFINITIONS = USE_TAPJOY )
如果我正确理解了您的问题,您希望将一些用户定义的预处理器宏添加到您的源代码中,有一种方法可以使用 Xcode 将它们添加到您的目标中。(例如 GCC_PREPROCESSOR_DEFINITIONS = USE_TAPJOY )
Step 1) Decide marco name e.g USE_TAPJOY
Step 2) Go to target-> select tab "Build Setting" (Make sure all tab is enabled)
Step 3) in search box search for "Preprocessor Macro")
Step 4) Check for Debug/Release section
Step 5) Enter your Marco there
步骤 1) 确定宏名称,例如 USE_TAPJOY 步骤 2) 转到目标-> 选择选项卡“构建设置”(确保启用所有选项卡)步骤 3)在搜索框中搜索“预处理器宏”)步骤 4)检查调试/发布部分 步骤 5) 在那里输入您的 Marco
Step 6) Use this macro in your source code as below
步骤 6)在您的源代码中使用此宏,如下所示
For conditional include
#ifdef USE_TAPJOY
#import <Tapjoy/Tapjoy.h>
#endif
For conditional source code
#ifdef USE_TAPJOY // Tapjoy Connect Notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tjcConnectSuccess:)
name:TJC_CONNECT_SUCCESS
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tjcConnectFail:)
name:TJC_CONNECT_FAILED
object:nil];
#endif
Good luck
祝你好运
回答by Mark Granoff
How about defining multiple targets and defining pre-processor macros in the target-specific build options? Then you need only have one scheme, and you can build all the targets in one shot, all with their own specific build configurations.
如何在特定于目标的构建选项中定义多个目标并定义预处理器宏?那么你只需要一个方案,你就可以一次构建所有目标,所有目标都有自己特定的构建配置。