在 XCode 中设置全局 #define

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1080131/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:38:47  来源:igfitidea点击:

Setting a global #define in XCode

xcode

提问by n179911

Can you please tell me how can I set a global #define in my Xcode project? I don't want to put #define DEBUG in every .h of my project.

你能告诉我如何在我的 Xcode 项目中设置全局 #define 吗?我不想将#define DEBUG 放在我项目的每个 .h 中。

Thank you

谢谢

回答by e.James

This is generally done in the Xcode project properties. Right-click on the project itself in the project window (the project is the top level of the heirarchy) and choose "get info". This will bring up the project inspector window. In the inspector window, choose the "Build Settings" tab. Now, use the search field to find an entry called "preprocessor macros", and put the string DEBUGinto that entry.

这通常在 Xcode 项目属性中完成。在项目窗口中右键单击项目本身(项目是层次结构的顶层)并选择“获取信息”。这将打开项目检查器窗口。在检查器窗口中,选择“构建设置”选项卡。现在,使用搜索字段查找名为“预处理器宏”的条目,并将字符串DEBUG放入该条目中。

If you do this only for the "Debug" build configuration (there should be a drop-down menu within the project inspector window), then this DEBUGmacro will only be #defined when you are actually debugging.

如果您仅针对“调试”构建配置执行此操作(项目检查器窗口中应该有一个下拉菜单),那么此DEBUG宏将仅在您实际调试时被 #defined 。

See Apple's documentationfor all the dirty details.

有关所有脏细节,请参阅Apple 的文档

回答by Robert

Another option is to use your project's .pchfile. It stand for precompiled header file and it compiled before any other file in your project.

另一种选择是使用您的项目.pch文件。它代表预编译头文件,它在项目中的任何其他文件之前编译。

This means if you put a #defineor an #importin the .pchfile it will be included in every file in your project.

这意味着如果您将 a#define或 an#import放在.pch文件中,它将包含在您项目中的每个文件中。

EDIT

编辑

As I have recently found out, you have to be carful using the .pchfile.

正如我最近发现的那样,您必须小心使用该.pch文件。

-- EVERY CHANGE YOU MAKE TO A FILE IN THE .pchEVERYFILE WILL BE RECOMPILED.--

-- 您对.pch每个文件中的文件所做的每次更改都将被重新编译。--

IF you have a large project this could take some time.

如果您有一个大型项目,这可能需要一些时间。