Christopher A
Christopher A

Reputation: 2961

Adding a Preprocessor Definition via a Xcode Scheme

I currently have a number of special "flavors" of an iPhone application that I need to build. Ideally, I would like to have a scheme for each "flavor" and each scheme would define (or set) one or more preprocessor definitions that I can use to branch on in my code and possibly even preprocess my info.plist file. This can obviously be done with multiple targets, but since I could have many different "flavors" of the app, it would be great to do it with schemes to keep the target count down. My current thought is to add these preprocessor definitions during a pre-action script, but I can not for the life of me find any way to update GCC_PREPROCESSOR_DEFINITIONS. Since it is an environment variable, shouldn't I have access to append onto GCC_PREPROCESSOR_DEFINITIONS?

Upvotes: 17

Views: 14460

Answers (4)

Dustin
Dustin

Reputation: 1030

Worse case scenario you can do a pre-build script for the scheme. You'll have to include the script for every scheme though:

I would prefer to attach it to a Configuration:

Then you can easily add preprocessor macros for the various configurations, like I have here for Debug:

The <ProjectName>_Prefix.pch file is a great place to put macros that effect the whole program, like I have here:

In my example we're effectively turning off console output when not in debug mode, providing a little speed boost.

Upvotes: 15

mask
mask

Reputation: 6232

If I understood your question correctly, you are looking to add some of user defined preprocessor macros to your source code, there is a way to add them in your target using Xcode. (e.g. GCC_PREPROCESSOR_DEFINITIONS = USE_TAPJOY )

Step 1) Decide marco name e.g USE_TAPJOY Step 2) Go to target-> select tab "Build Setting" (Make sure all tab is enabled) Step 3) in search box search for "Preprocessor Macro") Step 4) Check for Debug/Release section Step 5) Enter your Marco there enter image description here

Step 6) Use this macro in your source code as below

For conditional include 

    #ifdef USE_TAPJOY
    #import <Tapjoy/Tapjoy.h>
    #endif

    For conditional source code
    #ifdef USE_TAPJOY        // Tapjoy Connect Notifications
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(tjcConnectSuccess:)
                                                         name:TJC_CONNECT_SUCCESS
                                                       object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(tjcConnectFail:)
                                                         name:TJC_CONNECT_FAILED
                                                       object:nil];
    #endif

Good luck

Upvotes: 0

Christopher A
Christopher A

Reputation: 2961

To meet my requirement of allowing schemes to set preprocessor definitions, the best solution I have come up with is to have scheme pre-action and post-action scripts modify a xcconfig file. This file, in turn, updates the build configuration, setting the preprocessor definitions and will even allow me to define preprocessor definitions to conditionally modify the info.plist. If anyone else goes down this route, make sure you take into account how this file is handled by source control.

This article's question and associated answers was helpful to me: How to append values in xcconfig variables?

Upvotes: 10

Mark Granoff
Mark Granoff

Reputation: 16938

How about defining multiple targets and defining pre-processor macros in the target-specific build options? Then you need only have one scheme, and you can build all the targets in one shot, all with their own specific build configurations.

Upvotes: 0

Related Questions