3

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.

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
DavidG
  • 1,796
  • 4
  • 21
  • 33

1 Answers1

20

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
  • 54,380
  • 8
  • 73
  • 102
  • 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 Feb 08 '10 at 22:57
  • Note that if you set the environment variable in a scheme-wide pre-build action, then it won't be of any help, since it's being defined in a subshell – hence it will not be taken account in the build process. – DrMickeyLauer Apr 10 '12 at 10:12
  • Now how do I use that value in my code, for example just to print that value in the debug window? – daniel Dec 06 '18 at 07:08