Often I use this. I have a debug.h
header define as the following:
#ifndef DEBUG_H
#define DEBUG_H
#ifdef DEBUG
#define debuf if(1)
#else
#define debug if(0)
#endif
#endif
and then:
debug {
printf("message from debug!");
}
if you want to get "message from debug!"
message, compile with:
gcc -D DEBUG foo.c
Otherwise, nothing happens. Gcc is a very smart compiler. If DEBUG
isn't defined, the generated if(0)
(dead code) will be removed from your code with some optimizations on.
You still can do more:
debug
{
pritnf("I'm in debug mode!\n");
}
else
{
printf("I'm not in debug mode\n");
}
Some days ago I seen the D programming language provide a feature very similar too.
If you think the above without context, you can define thinks as
#define in_debug if(1)
#define not_debug else
And then
in_debug {
printf("I'm in debug mode!");
}
not_debug {
printf("Not in debug mode!");
}