在 xcode 4.6 中从命令行设置 #define

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

Setting a #define from the command line in xcode 4.6

xcodexcode4xcodebuild

提问by Roger Gilbrat

I'm trying get set a #define macro when doing a command line build using xcodebuild and having no luck.

我正在尝试在使用 xcodebuild 进行命令行构建并且没有运气时设置一个 #define 宏。

I've tried -DMYMACRO=1and MYMACRO=1everything else I can think of and nothing works.

我已经尝试过了-DMYMACRO=1MYMACRO=1我能想到的所有其他方法都没有奏效。

How do you set a #define from the command line?

如何从命令行设置#define?

回答by Bryan Musial

Roger,

罗杰,

What you are looking for is a way to set GCC_PREPROCESSOR_MACROS via the command line tool xcodebuild. The man page for xcodebuild shows the format for applying these settings, however the SYNOPSIS section only refers to this as "setting=value ..."

您正在寻找的是一种通过命令行工具 xcodebuild 设置 GCC_PREPROCESSOR_MACROS 的方法。xcodebuild 的手册页显示了应用这些设置的格式,但是 SYNOPSIS 部分仅将其称为“setting=value ...”

 xcodebuild [-project projectname] -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]
 xcodebuild -workspace workspacename -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]
 xcodebuild [-project projectname] -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]
 xcodebuild -workspace workspacename -scheme schemename [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]

Ultimately, you have the ability to set any of the Build Settings values directly on the command line by using this format and knowing the actual build setting name you wish to alter. Naturally, this brings up the question...

最终,您可以通过使用此格式并了解您希望更改的实际构建设置名称,直接在命令行上设置任何构建设置值。自然而然,这就引出了一个问题......

How do I find Build Setting Names?

如何找到构建设置名称?

Glad you asked! Xcode 4's sidebar is the easiest place to look to find the actual command-line build setting name.

很高兴你问了!Xcode 4 的侧边栏是查找实际命令行构建设置名称的最简单的位置。

Annotated Build Settings Screen Clip

带注释的构建设置屏幕剪辑

When looking for a build setting name, the Quick Help Inspector of Xcode 4's Utilities sidebar is the place to go looking.

在查找构建设置名称时,可以在 Xcode 4 的实用工具侧边栏的 Quick Help Inspector 中查找。

  1. Access the Build Settings screen of your project.
  2. Ensure the Sidebar (what Xcode calls 'Utilities') is visible by clicking the 'Utilities' button next to the Organizer button in the upper right corner of Xcode.
  3. Within the Utilities sidebar, ensure the 'Quick Help Inspector' is visible.
  1. 访问项目的构建设置屏幕。
  2. 通过单击 Xcode 右上角的 Organizer 按钮旁边的“Utilities”按钮,确保侧栏(Xcode 称为“Utilities”)可见。
  3. 在实用工具侧栏中,确保“快速帮助检查器”可见。

Alternatively, use Option+Command+2 to show the Quick Help Inspector directly!

或者,使用 Option+Command+2 直接显示快速帮助检查器!

Finally you are ready to find your build setting:

最后,您已准备好找到您的构建设置:

  1. Either perform a search for the build setting you want to change or scroll through the list of build settings.
  2. Click on the build setting you are interested in and observe the Quick Help Inspector update.
  3. The 'Declaration' section of the Quick Help Inspector shows the command-line build setting name you want to use.
  1. 搜索要更改的构建设置或滚动浏览构建设置列表。
  2. 单击您感兴趣的构建设置并观察 Quick Help Inspector 更新。
  3. 快速帮助检查器的“声明”部分显示了您要使用的命令行构建设置名称。

In the case of the Preprocessor Macros setting you originally asked about, that setting is:

在您最初询问的预处理器宏设置的情况下,该设置是:

GCC_PREPROCESSOR_DEFINITIONS

GCC_PREPROCESSOR_DEFINITIONS

Pulling this back together to your original question, you can set this build setting on the command line simply by providing the SETTING_NAME='DESIRED_VALUE' along with the rest of your xcodebuild command. In the case of a quick little test project I threw together called 'TestApp' where I wanted Preprocessor Macro 'BKM' to be set to value 1, my xcodebuild command would be:

将其拉回到您的原始问题中,您只需提供 SETTING_NAME='DESIRED_VALUE' 以及 xcodebuild 命令的其余部分,即可在命令行上设置此构建设置。在我将一个名为“TestApp”的快速小测试项目放在一起的情况下,我希望将预处理器宏“BKM”设置为值 1,我的 xcodebuild 命令将是:

xcodebuild -project TestApp.xcodeproj -scheme TestApp GCC_PREPROCESSOR_DEFINITIONS='${inherited} BKM=1'

xcodebuild -project TestApp.xcodeproj -scheme TestApp GCC_PREPROCESSOR_DEFINITIONS='${inherited} BKM=1'

Why did you stick ${inherited} in there?

你为什么把 ${inherited} 放在那里?

If you are using Preprocessor Macros then you likely have more than one that you are using. Some of which you may not want to change from the command line, but still have coded into the target or project's build settings. The use of '${inherited}' instructs xcodebuild to also use the build settings defined at a higher level instead of using only the ones we've defined in the xcodebuild command. In most cases you'll want to use ${inherited} to pull in any other configured values you've setup.

如果您正在使用预处理器宏,那么您使用的可能不止一个。其中一些您可能不想从命令行更改,但仍已编码到目标或项目的构建设置中。'${inherited}' 的使用指示 xcodebuild 也使用在更高级别定义的构建设置,而不是仅使用我们在 xcodebuild 命令中定义的设置。在大多数情况下,您需要使用 ${inherited} 来获取您设置的任何其他配置值。

Do I have to wrap the value in apostrophes?

我是否必须将值用撇号括起来?

If you want to set more than one value, then yes you will need to wrap the value in apostrophes otherwise if you set two or more Preprocessor Macros from the command line, the 2nd+ macro will be interpreted as a build setting instead of a Preprocessor Macro...not quite the right behavior. The apostrophes act as a way to collect multiple values for a setting together. In the case of my sample xcodebuild command, I wanted to have xcodebuild use the Inherited set of Preprocessor Macros along with my specific BKM setting so I wrapped the value in apostrophes to tell xcodebuild to treat them both as Preprocessor Macros.

如果您想设置多个值,那么是的,您需要将值包裹在撇号中,否则如果您从命令行设置两个或多个预处理器宏,则第 2+ 个宏将被解释为构建设置而不是预处理器宏...不太正确的行为。撇号是一种将一个设置的多个值收集在一起的方法。在我的示例 xcodebuild 命令的情况下,我希望 xcodebuild 使用继承的预处理器宏集以及我的特定 BKM 设置,因此我将值包装在撇号中以告诉 xcodebuild 将它们都视为预处理器宏。

Will this work with Workspaces too?

这也适用于工作区吗?

Absolutely! Just modify the command to use the -workspace parameter instead of the -project parameter and you'll be in business!

绝对地!只需修改命令以使用 -workspace 参数而不是 -project 参数,您就可以开始工作了!