5

I have created and applied a simple .xcconfig file containing

GCC_PREPROCESSOR_DEFINITIONS[config=Debug] = FOODEBUG
GCC_PREPROCESSOR_DEFINITIONS[config=Release] = FOORELEASE

and main.cpp containing

#include <iostream>

// This warning IS shown
#if DEBUG
#warning DEBUG is set to 1
#endif

// This warning IS NOT shown
#ifdef FOODEBUG
#warning FOODEBUG is set
#endif

// This warning IS NOT shown
#ifdef FOORELEASE
#warning FOORELEASE is set
#endif

int main(int argc, const char * argv[])
{
    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}

Now I'm wondering why in main.cpp, neither FOODEBUG nor FOORELEASE are defined ??!

As expected, the build settings show the two lines of my .xcconfig file ("Any Architecture | Any SDK"), but they are not actually used.

Xcode screenshot

How could I achieve that?

AndiDog
  • 68,631
  • 21
  • 159
  • 205

2 Answers2

4

If you have a preprocessor macro you need to give it a value to be able to use it as you do, see a screenshot of one of my project setups as a sample:

enter image description here

The reason why you can access DEBUG is difference is the different behaviour between #if and #ifdef. #if will be true when the macro exists, #ifdef if it has a non zero value. I suggest to always assign the value one to be save, because I'm not sure the above is true for all compiler versions.

UPDATE:
Did not know that before, but it seems config=Debug does not work. Although the macros get visible in the settings, they do not inherit up. What does work is 2 xcconfig files similar to this:

Release.xcconfig:

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOORELEASE=1

Debug.xcconfig

#include "Release.xcconfig"
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FOODEBUG=1

Please also see James Moores answer here: How to append values in xcconfig variables?

Community
  • 1
  • 1
dogsgod
  • 6,267
  • 6
  • 25
  • 53
  • How does this answer the question? – AndiDog Nov 14 '14 at 06:28
  • Sorry for being unclear, please see my edited answer. I'm not talking about changing the macros, but assigning a non zero value. Hope that helps – dogsgod Nov 14 '14 at 06:53
  • Still I don't get what this has to do with my question. The difference is very clear to me, but none of the macros is defined *at all* by Xcode. Having a value or not shouldn't matter here. – AndiDog Nov 14 '14 at 13:43
  • Maybe I'm just not getting you right ... It does make a difference if you have a value, try setting it in the settings window. If for some reason you need to do that in the xcconfig file try: GCC_PREPROCESSOR_DEFINITIONS[config=Debug] = "FOODEBUG=1" (not sure about the quotes) – dogsgod Nov 14 '14 at 13:55
  • I did that and it doesn't work. If you could provide a working example, the bounty is yours :) – Hai Feng Kao Nov 14 '14 at 15:52
  • 1
    It looks like you can not achieve that with a single config file, you will need to have separate files for debug and release. I updated the answer with an example – dogsgod Nov 18 '14 at 12:18
  • "#if will be true when the macro exists, #ifdef if it has a non zero value." - this is the other way around. – The_Falcon May 27 '21 at 16:06
0

You need to define preprocessor macro this way:

GCC_PREPROCESSOR_DEFINITIONS[config=Debug] = $(inherited) FOODEBUG=1
GCC_PREPROCESSOR_DEFINITIONS[config=Release] = $(inherited) FOORELEASE=1

For more details see the official documentaton.

Soumya Mahunt
  • 2,148
  • 12
  • 30