Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcodebuild - how to define preprocessor macro?

Tags:

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.

like image 417
Jaka Jančar Avatar asked Apr 25 '10 13:04

Jaka Jančar


People also ask

What is a preprocessor macro?

Macros allow you to write commonly used PL/I code in a way that hides implementation details and the data that is manipulated and exposes only the operations. In contrast with a generalized subroutine, macros allow generation of only the code that is needed for each individual use.

What is preprocessor macros in Xcode?

Preprocessor Macros are directives executed by the compiler, so they're not part of Objective C. We've used them many times before with the #import statement. But to stay with our Lite and Pro example: you can use a Preprocessor if/then statement to check which version is being compiled.

Is macro and preprocessor same?

macros are name for some fragment of code.So wherever the name is used it get replaced by the fragment of code by the preprocessor program. There are also many predefined macros in C for example __DATE__ , __TIME__ etc.


2 Answers

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[@]}")" 
like image 165
lhunath Avatar answered Dec 27 '22 01:12

lhunath


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.

like image 40
kennytm Avatar answered Dec 27 '22 00:12

kennytm