views:

2873

answers:

1

In Xcode, I can edit my preprocessor macros in the project settings. I want to create a macro that refers to an environment variable. Basically, I want to be able to refer to $SRC_ROOT in my code. What I currently have in my macros is:

SRC_ROOT=${SRC_ROOT}

but it isn't working.

+8  A: 

In Xcode build settings, you're not actually referring to an environment variable value. Instead, you're referring to a build setting value. The syntax for that is the Makefile-style $(SETTING_NAME) rather than the shell-style ${SETTING_NAME} you used above.

So what you want to do is add

SRC_ROOT="$(SRCROOT)"

to your Preprocessor Macros build setting.

As an added bonus, if you know that your macros won't affect the contents of your precompiled prefix file, instead of Preprocessor Macros you should use Preprocessor Macros Not Used in Precompiled Headers instead.

That way you can improve sharing of your precompiled prefix header (defined by a pch file) between different targets in your project, or even different projects. Technical Note 2190: Speeding up your Xcode Builds goes into more detail on this: If you use the same prefix file name and contents, and build using the same build settings, across multiple projects, you can get dramatic improvements in build performance because Xcode will recognize when it can re-use existing precompiled prefix files.

Chris Hanson
Riddle me this though: If I edit my iPhone app's Project (not target) settings, and search each configuration for "Preprocessor Macros" ... I see a "GCC 4.2 - Preprocessing" section and "Preprocessor Macros" et. al for Ad-Hoc, Release and Distribution configs ... but nothing for Debug and Analyzer configs - though the latter two DO have three user-defined settings: GCC_C_LANGUAGE_STANDARD (c99), GCC_WARN_ABOUT_RETURN_TYPE (YES), and GCC_WARN_UNUSED_VARIABLE (YES).
Joe D'Andrea