#ifdef inside #define

agent.smith picture agent.smith · Apr 7, 2011 · Viewed 35.7k times · Source

I am trying to write something like this:

#define COV_ON(x) \
                #ifdef COVERAGE_TOOL \
                    _Pragma (COVERAGE #x)
                #endif

Is there any way to define COV_ON like this? I know what I have done above is wrong as I can't have #ifdef inside #define. (# is not an allowed character in #define). So is there any solution?

Answer

Hans Passant picture Hans Passant · Apr 7, 2011

Not possible. Do it the other way around:

#ifdef COVERAGE_TOOL
#define COV_ON(x) _Pragma (COVERAGE #x)
#else
#define COV_ON(x)
#endif