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?
-
You might want to check out this answer: http://stackoverflow.com/a/26433618/1055722 – David K. Hess Oct 17 '14 at 21:36
4 Answers
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.
-
1Thanks Dustin. I want to reduce the number of configurations if at all possible. Good tip about the console output though. – Christopher A Dec 05 '11 at 17:49
-
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?

- 1
- 1

- 2,961
- 1
- 22
- 23
-
3Hi Christopher, can you expand on this? How do you modify the xcconfig file? – jasongregori Dec 17 '11 at 00:45
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
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

- 6,172
- 3
- 24
- 23
-
This was not the question. Question was the ability to set for each scheme a different Macro, while keeping only one target. – Cœur Nov 23 '15 at 11:59
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.

- 16,878
- 2
- 59
- 61
-
1Thanks for the suggestion. For some people, this may be a good solution. For this specific project, due to the scale and number of "flavors" I will need, this would be difficult to maintain. – Christopher A Dec 05 '11 at 17:48