25

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.

Jaka Jančar
  • 11,386
  • 9
  • 53
  • 74
  • Does this answer your question? [Setting a #define from the command line in xcode 4.6](https://stackoverflow.com/questions/15708831/setting-a-define-from-the-command-line-in-xcode-4-6) – Eric Nov 30 '21 at 15:17

2 Answers2

38

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[@]}")"
lhunath
  • 120,288
  • 16
  • 68
  • 77
  • 2
    Can you tell me, is it obligatory to give a value, TESTING=1 or not? Is there something equivalent to #define TESTING ? – karim Feb 27 '13 at 09:51
  • 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. – karim Feb 27 '13 at 10:22
  • @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. – lhunath Feb 28 '13 at 13:01
  • $e contains a value from an array. My 2nd comment is an answer to my first comment. :) – karim Feb 28 '13 at 14:25
  • 5
    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'` – Goffredo May 15 '14 at 21:35
  • 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. – M Katz Nov 02 '21 at 12:33
18

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005