2

Why doesn't this work (It's failing the assert...that I don't want it to execute)?

#define OS_MACOSX 0

#if defined(OS_MACOSX)
    fructose_assert(1==2);
#endif
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3564870
  • 385
  • 2
  • 13

1 Answers1

3

Because you're using the wrong test.

OS_MACOSX IS defined... to 0. You asked if it is defined. Ask instead if it is set:

#if OS_MACOSX
    fructose_assert(1==2);
#endif

If it is defined to something that evaluates to zero, or not defined at all, the #if will skip over the code. Any non-zero value, and the #if condition is met and the code is compiled.

See this related question: Why do people use #ifdef for feature flag tests?

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720