0

Can you embed #ifdef and #endif?

For example:

#ifdef B
    run1();
    #ifdef C
        run2();
    #endif
#endif

Desired result is run2() only occurs if both B and C are defined

SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • possible duplicate of [Can if preprocessor directives be nested in c++?](http://stackoverflow.com/questions/6678396/can-if-preprocessor-directives-be-nested-in-c) – Blue Ice Mar 21 '14 at 20:34

1 Answers1

2

Yes you can.

See the official GCC documentation for details.

The controlled text inside of a conditional can include preprocessing directives. They are executed only if the conditional succeeds. You can nest conditional groups inside other conditional groups, but they must be completely nested. In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you cannot start a conditional group in one file and end it in another.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77