如何在 Xcode 中定义预处理器符号
是否可以通过在 Xcode 项目中设置属性来设置条件编译的符号?
我的目标是创建一个可用于所有文件的符号,而不必使用导入/包含,以便一组常见的类可以在某些项目中具有特殊的行为。 如下所示,但带有我自己的符号。
#if TARGET_IPHONE_SIMULATOR
...
#endif
Is it possible to set a symbol for conditional compilation by setting up properties in an Xcode project?
My aim is to to create a symbol that is available to all files, without having to use import/include, so that a set of common classes can have a special behavior in some projects. Like the following, but with my own symbols.
#if TARGET_IPHONE_SIMULATOR
...
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
适用于 Xcode 9.4.1 和 C++ 项目。 将
const char*
预处理器宏添加到调试和发布版本。选择您的项目
选择构建设置
搜索“预处理器宏”
打开互动列表
添加宏,不要忘记转义引号
在源代码中与普通
const char*
一样使用<前><代码>...
#ifndef JSON_DEFINITIONS_FILE_PATH
static constexpr auto JSON_DEFINITIONS_FILE_PATH = "definitions.json";
#万一
...
FILE *pFileIn = fopen(JSON_DEFINITIONS_FILE_PATH, "r");
...
For Xcode 9.4.1 and C++ project. Adding
const char*
Preprocessor Macros to both Debug and Release builds.Select your project
Select Build Settings
Search "Preprocessor Macros"
Open interactive list
Add your macros and don't forget to escape quotation
Use in source code as common
const char*
它位于“GCC 4.2 Preprocessing”下(或者只是在搜索框中输入“prepro”)......
但是,对于我的一生,我无法让它工作。
我有标准的调试和发布配置,并且我想在调试配置中定义 DEBUG=1。 但将其添加为值后:(
在设置窗口中)> 预处理器宏:DEBUG=1
...从不打印/被调用。 这让我疯狂...
It's under "GCC 4.2 Preprocessing" (or just put "prepro" in the search box)...
...however, for the life of me I can't get it to work.
I have my standard Debug and Release configurations, and I want to define DEBUG=1 in the debugging configuration. But after adding it as a value:
(in the settings window) > Preprocessor Macros : DEBUG=1
...never prints/gets called. It's driving me crazy...
为了回应 Kevin Laity 的评论(请参阅 cdespinosa 的回答),关于 GCC 预处理部分未显示在构建设置中,请将 Active SDK 设为其后显示 (Base SDK) 的部分,此部分将会出现。 您可以通过选择菜单“项目”>“项目”来完成此操作。 设置活动目标> XXX(基础 SDK)。 在不同版本的 XCode(Base SDK)中可能会有所不同,例如(项目设置或项目默认)。
出现此部分后,您可以将定义添加到处理器宏,而不是创建用户定义的设置。
In response to Kevin Laity's comment (see cdespinosa's answer), about the GCC Preprocessing section not showing in your build settings, make the Active SDK the one that says (Base SDK) after it and this section will appear. You can do this by choosing the menu Project > Set Active Target > XXX (Base SDK). In different versions of XCode (Base SDK) maybe different, like (Project Setting or Project Default).
After you get this section appears, you can add your definitions to Processor Macros rather than creating a user-defined setting.
您可以复制具有预处理部分的目标,将其重命名为您想要的任何名称,然后更改预处理器宏值。
You can duplicate the target which has the preprocessing section, rename it to any name you want, and then change your Preprocessor macro value.
转到目标或项目设置,单击左下角的齿轮图标,然后选择“添加用户定义的设置”。 新设置名称应为
GCC_PREPROCESSOR_DEFINITIONS
,您可以在右侧字段中键入您的定义。根据 Steph 的评论,完整的语法是:
请注意,如果您只想 #define 一个符号,而不是给它一个值,则不需要“=”(对于
# ifdef 语句)
Go to your Target or Project settings, click the Gear icon at the bottom left, and select "Add User-Defined Setting". The new setting name should be
GCC_PREPROCESSOR_DEFINITIONS
, and you can type your definitions in the right-hand field.Per Steph's comments, the full syntax is:
Note that you don't need the '='s if you just want to #define a symbol, rather than giving it a value (for
#ifdef
statements)您不需要创建用户定义的设置。 内置设置“预处理器宏”工作得很好。
替代文本 http://idisk.mac.com/cdespinosa/Public/Picture%204.png
如果您有多个使用相同前缀文件的目标或项目,请改用预编译头中未使用的预处理器宏,这样宏定义中的差异就不会触发不必要的额外预编译头集。
You don't need to create a user-defined setting. The built-in setting "Preprocessor Macros" works just fine.
alt text http://idisk.mac.com/cdespinosa/Public/Picture%204.png
If you have multiple targets or projects that use the same prefix file, use Preprocessor Macros Not Used In Precompiled Headers instead, so differences in your macro definition don't trigger an unnecessary extra set of precompiled headers.
作为附录,如果您使用此技术在目标中定义字符串,这就是我必须定义和使用它们的方式:
在“构建设置”->“ 预处理器宏,是的,反斜杠在定义中至关重要:
在源代码中:
As an addendum, if you are using this technique to define strings in your target, this is how I had to define and use them:
In Build Settings -> Preprocessor Macros, and yes backslashes are critical in the definition:
And in the source code:
您可以使用
*_Prefix.pch
文件来声明项目范围的宏。该文件通常位于您的其他来源组中。
You can use the
*_Prefix.pch
file to declare project wide macros.That file is usually in you Other Sources group.