通过Xcode方案添加预处理器定义

时间:2022-02-23 02:29:26

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吗?

4 个解决方案

#1


14  

Worse case scenario you can do a pre-build script for the scheme. You'll have to include the script for every scheme though: 通过Xcode方案添加预处理器定义

更糟糕的情况是,您可以为该方案执行预构建脚本。您必须为每个方案包含脚本:

I would prefer to attach it to a Configuration: 通过Xcode方案添加预处理器定义

我更愿意将它附加到配置:

Then you can easily add preprocessor macros for the various configurations, like I have here for Debug: 通过Xcode方案添加预处理器定义

然后,您可以轻松地为各种配置添加预处理器宏,就像我在这里进行调试一样:

The <ProjectName>_Prefix.pch file is a great place to put macros that effect the whole program, like I have here: 通过Xcode方案添加预处理器定义

_Prefix.pch文件是放置影响整个程序的宏的好地方,就像我在这里:

In my example we're effectively turning off console output when not in debug mode, providing a little speed boost.

在我的例子中,我们在不处于调试模式时有效地关闭控制台输出,提供一点速度提升。

#2


8  

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变量中追加值?

#3


0  

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.

如何在特定于目标的构建选项中定义多个目标并定义预处理器宏?那么你只需要一个方案,你可以一次性构建所有目标,所有目标都有自己特定的构建配置。

#4


0  

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 通过Xcode方案添加预处理器定义

步骤1)确定marco名称,例如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

#1


14  

Worse case scenario you can do a pre-build script for the scheme. You'll have to include the script for every scheme though: 通过Xcode方案添加预处理器定义

更糟糕的情况是,您可以为该方案执行预构建脚本。您必须为每个方案包含脚本:

I would prefer to attach it to a Configuration: 通过Xcode方案添加预处理器定义

我更愿意将它附加到配置:

Then you can easily add preprocessor macros for the various configurations, like I have here for Debug: 通过Xcode方案添加预处理器定义

然后,您可以轻松地为各种配置添加预处理器宏,就像我在这里进行调试一样:

The <ProjectName>_Prefix.pch file is a great place to put macros that effect the whole program, like I have here: 通过Xcode方案添加预处理器定义

_Prefix.pch文件是放置影响整个程序的宏的好地方,就像我在这里:

In my example we're effectively turning off console output when not in debug mode, providing a little speed boost.

在我的例子中,我们在不处于调试模式时有效地关闭控制台输出,提供一点速度提升。

#2


8  

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变量中追加值?

#3


0  

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.

如何在特定于目标的构建选项中定义多个目标并定义预处理器宏?那么你只需要一个方案,你可以一次性构建所有目标,所有目标都有自己特定的构建配置。

#4


0  

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 通过Xcode方案添加预处理器定义

步骤1)确定marco名称,例如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