106

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steph Thirion
  • 9,313
  • 9
  • 50
  • 58
  • Related: "[Add preprocessor macro to a target in xcode 6](https://stackoverflow.com/q/26928622/90527)" – outis Jun 11 '19 at 21:11

8 Answers8

117

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)

jww
  • 97,681
  • 90
  • 411
  • 885
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • 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 Dec 15 '08 at 05:41
  • 2
    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 Dec 20 '08 at 02:56
  • 2
    Amazing answer. I still have hair because of this. Thank you thank you thank you. +1 (I wish it could be more) – Ali Parr Mar 25 '09 at 22:08
  • 11
    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 Sep 13 '10 at 21:58
  • Can I define these on the command line.... i.e... constant_1=VALUE xcodebuild ... – Matt Jan 09 '13 at 06:57
  • 12
    This is no longer a Gear icon on the lower left, but instead a Plus icon on the lower right (of the Build Settings tab of your target). – JTE Feb 13 '13 at 05:06
  • 3
    you should update this response. there is no gear icon anymore – Radu Simionescu Aug 01 '16 at 12:04
86

You don't need to create a user-defined setting. The built-in setting "Preprocessor Macros" works just fine. alt text http://idisk.mac.com/cdespinosa/Public/Picture%204.png

If you have multiple targets or projects that use the same prefix file, use Preprocessor Macros Not Used In Precompiled Headers instead, so differences in your macro definition don't trigger an unnecessary extra set of precompiled headers.

cdespinosa
  • 20,661
  • 6
  • 33
  • 39
  • 1
    What do you do if this section is missing from the build settings? – Kevin Laity Oct 08 '09 at 21:39
  • 3
    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 Sep 13 '10 at 21:57
43

As an addendum, if you are using this technique to define strings in your target, this is how I had to define and use them:

In Build Settings -> Preprocessor Macros, and yes backslashes are critical in the definition:

APPURL_NSString=\@\"www.foobar.org\"

And in the source code:

objectManager.client.baseURL = APPURL_NSString;
Stickley
  • 4,561
  • 3
  • 30
  • 29
  • Thanks for this solution, been searching for something EXACTLY like this, yet stack overflow is full of over complicated solutions. – David P May 16 '16 at 08:52
5

You can use the *_Prefix.pch file to declare project wide macros. That file is usually in you Other Sources group.

chunkyguy
  • 3,509
  • 1
  • 29
  • 34
4

For Xcode 9.4.1 and C++ project. Adding const char* Preprocessor Macros to both Debug and Release builds.

  1. Select your project

    select project

  2. Select Build Settings

    select build settings

  3. Search "Preprocessor Macros"

    search1 search2

  4. Open interactive list

    open interactive list

  5. Add your macros and don't forget to escape quotation

    add path

  6. Use in source code as common const char*

    ...
    #ifndef JSON_DEFINITIONS_FILE_PATH
    static constexpr auto JSON_DEFINITIONS_FILE_PATH = "definitions.json";
    #endif
    ...
    FILE *pFileIn = fopen(JSON_DEFINITIONS_FILE_PATH, "r");
    ...
    
Petr Javorik
  • 1,695
  • 19
  • 25
3

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...

hEADcRASH
  • 1,905
  • 17
  • 17
  • 10
    Instead of #if DEBUG, try #if defined(DEBUG) or #ifdef DEBUG – Simo Salminen Nov 22 '10 at 06:52
  • Strangely enough, I found myself here in 2019, with Swift and Xcode 10.3. Anyone with the same issue described above might find this helpful; it certainly brought my `#if DEBUG` to life :) https://stackoverflow.com/a/47395485/2778502 – jeff-h Aug 28 '19 at 10:50
1

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
  • 1,447
  • 1
  • 11
  • 9
0

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
  • 207
  • 3
  • 2