如何在Xcode 4中定义预处理器宏?

时间:2023-02-08 07:12:00

I have two targets set up for my app (a lite version and a pro version) and I want to integrate some subtle differences in the code for each of them (e.g. the pro version will not show any iAd banners).

我为我的应用程序设定了两个目标(一个是lite版本,一个是pro版本),我想在每个目标的代码中集成一些细微的差异(例如,pro版本不会显示任何iAd横幅)。

I have been looking around and I see the easiest way to do this is through the use of preprocessor macros. The issue I'm facing is how to set them up in Xcode 4. I want to set up a macro called 'PRO_VERSION' in one target & 'LITE_VERSION' in the other.

我一直在寻找,我发现最简单的方法是使用预处理器宏。我面临的问题是如何在Xcode 4中设置它们。我想在一个目标中设置一个名为“PRO_VERSION”的宏,在另一个目标中设置一个名为“LITE_VERSION”的宏。

Below is an example of how I intend to use them:

下面是我打算如何使用它们的一个例子:

#ifdef PRO_VERSION
    // Hide ad banners
#else
    // Show ad banners
#endif

2 个解决方案

#1


40  

The build setting you need to change is called 'Preprocessor Macros' and it can be found in the 'Build Settings' tab of the Project Settings pane (use the search box to find it). Select each target in turn in the left-hand side of the Project Settings pane then modify the Preprocessor Macros setting.

需要更改的构建设置称为“预处理器宏”,可以在“项目设置”窗格的“构建设置”选项卡中找到(使用搜索框查找)。在Project Settings窗格的左侧依次选择每个目标,然后修改预处理器宏设置。

The setting is specified as a space-separated list of preprocessor macros in the form 'foo' or 'foo=bar'.

该设置被指定为“foo”或“foo=bar”格式的空间分隔的预处理器宏列表。

#2


3  

I'm not on my mac at the moment, so I can't give full step-by-step directions, but I believe this should be accurate, if not as detailed as I would otherwise be. Create a new build target. Go to the configuration screen for this new target. There should be a tab along the lines of compilation options. In this tab there should be a row for other compiler flags. In there, put -DPRO_VERSION.

我现在没有使用mac电脑,所以我不能给出完整的一步一步的指导,但我相信这应该是准确的,如果不是像我那样详细的话。创建一个新的构建目标。转到此新目标的配置屏幕。在编译选项的行中应该有一个选项卡。在这个选项卡中应该有一行用于其他编译器标志。在那里,把-DPRO_VERSION。

#1


40  

The build setting you need to change is called 'Preprocessor Macros' and it can be found in the 'Build Settings' tab of the Project Settings pane (use the search box to find it). Select each target in turn in the left-hand side of the Project Settings pane then modify the Preprocessor Macros setting.

需要更改的构建设置称为“预处理器宏”,可以在“项目设置”窗格的“构建设置”选项卡中找到(使用搜索框查找)。在Project Settings窗格的左侧依次选择每个目标,然后修改预处理器宏设置。

The setting is specified as a space-separated list of preprocessor macros in the form 'foo' or 'foo=bar'.

该设置被指定为“foo”或“foo=bar”格式的空间分隔的预处理器宏列表。

#2


3  

I'm not on my mac at the moment, so I can't give full step-by-step directions, but I believe this should be accurate, if not as detailed as I would otherwise be. Create a new build target. Go to the configuration screen for this new target. There should be a tab along the lines of compilation options. In this tab there should be a row for other compiler flags. In there, put -DPRO_VERSION.

我现在没有使用mac电脑,所以我不能给出完整的一步一步的指导,但我相信这应该是准确的,如果不是像我那样详细的话。创建一个新的构建目标。转到此新目标的配置屏幕。在编译选项的行中应该有一个选项卡。在这个选项卡中应该有一行用于其他编译器标志。在那里,把-DPRO_VERSION。