Setting a #define from the command line in xcode 4.6

*爱你&永不变心* 提交于 2019-12-21 01:07:06

问题


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

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

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


回答1:


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 [-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.

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

  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.

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

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.

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

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:

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

Why did you stick ${inherited} in there?

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.

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.

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!



来源:https://stackoverflow.com/questions/15708831/setting-a-define-from-the-command-line-in-xcode-4-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!