views:

10636

answers:

7

Is it possible to set a symbol for conditional compilation by setting up properties in an Xcode project?

My aim is to to create a symbol that is available to all files, without having to use import/include, so that a set of common classes can have a special behavior in some projects. Like the following, but with my own symbols.

#if TARGET_IPHONE_SIMULATOR
    ...
#endif
+34  A: 

Go to your Target or Project settings, click the Gear icon at the bottom left, and select "Add User-Defined Setting". The new setting name should be GCC_PREPROCESSOR_DEFINITIONS, and you can type your definitions in the right-hand field.

Per Steph's comments, the full syntax is:

 constant_1=VALUE constant_2=VALUE

Note that you don't need the '='s if you just want to #define a symbol, rather than giving it a value (for #ifdef statements)

Ben Gottlieb
Either use backslashes in front of underscores to prevent them converting the text to italic or use back-quotes to enclose programming material - which is what I did.
Jonathan Leffler
Thanks Jonathan, will do!
Ben Gottlieb
Thanks! To whoever interested, the syntax looks like "kVarOne=5 myVar=3.0" (without the quotes), I found it by trial and error. Ben could you edit your answer to specify that? Thanks again.
Steph Thirion
Amazing answer. I still have hair because of this. Thank you thank you thank you. +1 (I wish it could be more)
Ali Parr
FYI, as of Xcode 3.2.4, "Preprocessor Macros" maps to GCC_PREPROCESSOR_DEFINITIONS, and Xcode will not let you do a user-define anymore (since it is already available).
Clay Bridges
+19  A: 
cdespinosa
What do you do if this section is missing from the build settings?
Kevin Laity
Do what Ben Gotliebb suggests. But as of version 3.2.4 of Xcode, "Preprocessor Macros" maps to GCC_PREPROCESSOR_DEFINITIONS. You cannot user-define something already available.
Clay Bridges
A: 

It's under "GCC 4.2 Preprocessing" (or just put "prepro" in the search box)...

...however, for the life of me I can't get it to work.

I have my standard Debug and Release configurations, and I want to define DEBUG=1 in the debugging configuration. But after adding it as a value:

(in the settings window) > Preprocessor Macros : DEBUG=1

#if DEBUG
    printf("DEBUG is set!");
#endif

...never prints/gets called. It's driving me crazy...

A: 

Instead of #if DEBUG, try

#if defined(DEBUG)

or

#ifdef DEBUG
Simo Salminen
This is not an answer to the question. Also, in his example, TARGET_IPHONE_SIMULATOR is *always* set as 0 or 1, so taking your advice would result in conditionals that always result to true.
benzado
+1  A: 

In response to Kevin Laity's comment (see cdespinosa's answer), about the GCC Preprocessing section not showing in your build settings, make the Active SDK the one that says (Base SDK) after it and this section will appear. You can do this by choosing the menu Project > Set Active Target > XXX (Base SDK). In different versions of XCode (Base SDK) maybe different, like (Project Setting or Project Default).

After you get this section appears, you can add your definitions to Processor Macros rather than creating a user-defined setting.

Mark24x7
A: 

Question: I find the GCC 4.2 Preprocessing section only in the Release configuration, not in the Debug configuration for my project. I want to use

ifdef DEBUG_MODE

to conditionalize code that writes stuff to the console. Should there be such a section in the Debug configuration? If not, can I add one?

Thx

Steve

Steve Fogel
Please open a new question. The answers section is not for asking questions.
Whisty
A: 

You can duplicate the target which has the preprocessing section, rename it to any name you want, and then change your Preprocessor macro value.

kslcam