Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top