Xcode 预处理器宏

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

Xcode Preprocessor Macros

xcodemacrospreprocessor

提问by DavidG

In Xcode, I can edit my preprocessor macros in the project settings. I want to create a macro that refers to an environment variable. Basically, I want to be able to refer to $SRC_ROOT in my code. What I currently have in my macros is:

在 Xcode 中,我可以在项目设置中编辑我的预处理器宏。我想创建一个引用环境变量的宏。基本上,我希望能够在我的代码中引用 $SRC_ROOT。我目前在我的宏中拥有的是:

SRC_ROOT=${SRC_ROOT}

but it isn't working.

但它不起作用。

回答by Chris Hanson

In Xcode build settings, you're not actually referring to an environment variable value. Instead, you're referring to a build settingvalue. The syntax for that is the Makefile-style $(SETTING_NAME)rather than the shell-style ${SETTING_NAME}you used above.

在 Xcode 构建设置中,您实际上并不是指环境变量值。相反,您指的是构建设置值。它的语法是 Makefile 风格,$(SETTING_NAME)而不是${SETTING_NAME}你上面使用的 shell 风格。

So what you want to do is add

所以你想要做的是添加

SRC_ROOT="$(SRCROOT)"

to your Preprocessor Macrosbuild setting.

到您的预处理器宏构建设置。

As an added bonus, if you know that your macros won't affect the contents of your precompiled prefix file, instead of Preprocessor Macrosyou should use Preprocessor Macros Not Used in Precompiled Headersinstead.

作为额外的奖励,如果你知道你的宏不会影响,而不是你的预编译前缀文件的内容,预处理宏,你应该使用预处理宏在预编译头不使用来代替。

That way you can improve sharing of your precompiled prefix header (defined by a pchfile) between different targets in your project, or even different projects. Technical Note 2190: Speeding up your Xcode Buildsgoes into more detail on this: If you use the same prefix file name and contents, and build using the same build settings, across multiple projects, you can get dramatic improvements in build performance because Xcode will recognize when it can re-use existing precompiled prefix files.

通过这种方式,您可以改进pch项目中不同目标甚至不同项目之间预编译前缀标头(由文件定义)的共享。 技术说明 2190:加速您的 Xcode 构建对此进行了更详细的说明:如果您使用相同的前缀文件名和内容,并使用相同的构建设置在多个项目中构建,您可以获得构建性能的显着改进,因为 Xcode 将识别何时可以重用现有的预编译前缀文件。