xcodebuild - how to define preprocessor macro?

18,282

Solution 1

Cmd + I on the project to open the Info dialog. Then in the "Build" tab, find the "Preprocessor Macros" setting. Add the macros there.

... Where you can find the setting name is GCC_PREPROCESSOR_DEFINITIONS, so you could add

GCC_PREPROCESSOR_DEFINITIONS="foo=bar"

to the xcodebuild arguments.

Solution 2

You pass GCC_PREPROCESSOR_DEFINITIONS on the xcodebuild command line.

Remember that the argument will be re-evaluated for shell-like word splitting and quote handling, so you need to be careful, especially when your macro values aren't just simple 1s (eg. NSString literals).

Also important is to expand the GCC_PREPROCESSOR_DEFINITIONS inside the value you set (single-quoted, so your script doesn't expand it but the build's shell expands it), otherwise you'll lose your project's build settings for this property.

The following code puts your defines in a nice bash array and then expands the array in the xcodebuild command line in a way that shell stuff gets nicely escaped:

defines=( TESTING=1 'IWISH_HOST=@"http://192.168.0.101:8080"' )

xcodebuild -verbose -scheme "MyAppScheme" \
    GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS '"$(printf '%q ' "${defines[@]}")"
Share:
18,282

Related videos on Youtube

Jaka Jančar
Author by

Jaka Jančar

Updated on December 01, 2021

Comments

  • Jaka Jančar
    Jaka Jančar over 2 years

    How can I define a preprocessor macro when using xcodebuild?

    I need to build my app using a bunch of different configurations, and I would like to do this using a shell script which runs xcodebuild a number of times with different preprocessor macros.

  • nall
    nall about 14 years
    I believe what KennyTM says will work fine in xcodebuild once it's setup (using the GUI).
  • Jaka Jančar
    Jaka Jančar about 14 years
    I was looking at xcodebuild's options/flags, didn't even notice that setting settings was part of the basic usage syntax. Thanks.
  • Dirty Henry
    Dirty Henry about 12 years
    what is the separator when you wish to use multiple definitions?
  • karim
    karim about 11 years
    Can you tell me, is it obligatory to give a value, TESTING=1 or not? Is there something equivalent to #define TESTING ?
  • karim
    karim about 11 years
    Ok, I have to use double quote and remove the $value. I had, GCC_PREPROCESSOR_DEFINITIONS='$value ${e}', which did not work, but GCC_PREPROCESSOR_DEFINITIONS="${e}" works.
  • lhunath
    lhunath about 11 years
    @karim I don't know what $e is but I get the feeling you've completely missed the point of this answer. Also, the defines array is where you put your own custom defines. The one above is just an example. In all likelihood your array will look entirely different.
  • karim
    karim about 11 years
    $e contains a value from an array. My 2nd comment is an answer to my first comment. :)
  • Goffredo
    Goffredo almost 10 years
    Here is a concrete example that works with XCode 5.1, in case it helps anyone. In the source code, you would have: #ifdef COOL_FEATURE_ENABLED NSLog(@"Cool feature enabled in this build."); #endif. For the command-line build, you would have: xcodebuild -verbose -scheme "MyAppScheme" GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS COOL_FEATURE_ENABLED=1'
  • M Katz
    M Katz over 2 years
    To further explain the example by Jeffro (and this answer in general), the single quotes around the value will not evaluate the $ values inside (as double quotes would). So the $ values are evaluated by xcodebuild itself. For this reason, we must pass $GCC_PREPROCESSOR_DEFINITIONS as the first value (actually a set of space-separated values), which means "all the preprocessor definitions already set in the xcode project.