Preprocessor debugging flags
By using the preprocessor to
#define one or more debugging flags (preferably in
a header file), you can test a flag using an
#ifdef statement and conditionally include
debugging code. When you think your debugging is finished, you can simply
#undef the flag(s) and the code will automatically
be removed (and you’ll reduce the size and runtime overhead of your
executable file).
It is best to decide on names for
debugging flags before you begin building your project so the names will be
consistent. Preprocessor flags are traditionally distinguished from variables by
writing them in all upper case. A common flag name is simply DEBUG (but
be careful you don’t use NDEBUG, which is reserved in C). The
sequence of statements might be:
#define DEBUG // Probably in a header file
//...
#ifdef DEBUG // Check to see if flag is defined
/* debugging code here */
#endif // DEBUG
Most C and C++ implementations will also
let you #define and #undef flags from the compiler command line,
so you can re-compile code and insert debugging information with a single
command (preferably via the makefile, a tool that will be described shortly).
Check your local documentation for details.