Xcode Preprocessor Macros

14,658

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.

Share:
14,658
DavidG
Author by

DavidG

Updated on June 30, 2022

Comments

  • DavidG
    DavidG over 1 year

    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.

  • Joe D'Andrea
    Joe D'Andrea about 14 years
    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).
  • DrMickeyLauer
    DrMickeyLauer almost 12 years
    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.
  • daniel
    daniel over 5 years
    Now how do I use that value in my code, for example just to print that value in the debug window?