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,438
  • 68
  • 55
  • 126
user3564870
  • 375
  • 2
  • 12

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
  • 269,602
  • 39
  • 394
  • 697