I'm writing a program which has debug code which can be disabled by a macro, DEBUG
. When set to 1
, the debug code should be compiled into the binary, and when set to 0
, the debug code should not be.
I am compiling with -Wunreachable-code enabled, and my question results from compiling code such as this:
if (DEBUG) {
printf("debug string!\n");
}
When debugging is enabled, this will compile fine, as the condition will always evaluate to 1 (true). However, when debugging is disabled (DEBUG=0
), I get an unreachable code warning on the printf
call on the second line. I am aware this can be solved with the #ifdef/#endif
preprocessor directives, however I don't like filling the body of my program with preprocessor directives if it can be avoided.
I am wondering if there is any way I can leave my code with c-style conditionals without using diagnostic pragmas (#pragma GCC diagnostic
). Any help would be appreciated, thank you!