Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a #define from the command line in xcode 4.6

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?

like image 200
Roger Gilbrat Avatar asked Mar 29 '13 18:03

Roger Gilbrat


People also ask

What are the 3 types of setting?

The three types of setting are the elements of time, place, and environment (both physical and social). Each of these types contributes to building the setting of a story.

What are the 4 types of setting?

Students are familiar with the four types of setting: physical, social, historical and psychological.

What is called a setting?

Definition of setting 1 : the manner, position, or direction in which something is set. 2 : the frame or bed in which a gem is set also : style of mounting. 3a : the time, place, and circumstances in which something occurs or develops. b : the time and place of the action of a literary, dramatic, or cinematic work.

What is setting in a?

Setting is the time and place (or when and where) of the story. It's a literary element of literature used in novels, short stories, plays, films, etc., and usually introduced during the exposition (beginning) of the story, along with the characters.


2 Answers

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.

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.

  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!

like image 72
Bryan Musial Avatar answered Oct 21 '22 18:10

Bryan Musial


Swift 5+, Xcode 13+

You can pass OTHER_SWIFT_FLAGS to xcodebuild thus:

xcodebuild \
    build \
    "OTHER_SWIFT_FLAGS=${inherited} -D STAGING" \
    -target "TargetName" \
    -scheme "SchemeName"

This will result in the following:

#if STAGING
// this will compile
#else
// this won't compile
#endif
like image 1
Eric Avatar answered Oct 21 '22 19:10

Eric