Search code examples
debugginggccc-preprocessorcompiler-options

When and where is _DEBUG defined?


I have the following sample code in one of my header files:

#ifdef _DEBUG
#define _DEBUG_NEW_REDEFINE_NEW 0
#include "debug_new.h"
#else
#define DEBUG_NEW new
#endif

The application which includes this header file is compiled using gcc compiler with -DDEBUG option.

Question:

Is _DEBUG is defined because of the -DDEBUG option?


Solution

  • -DDEBUG will only define DEBUG, not _DEBUG. To figure out why (or if) _DEBUG is getting defined, try building the source file that includes that header with

    gcc --other_options source_file.cc -E -dD -o source_file.ii
    

    (You may have to remove another -o flag in the command line.) Then source_file.ii will include #define lines for every macro that was defined, and # <lineno> <header> lines each time it changes header files.

    You can read http://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/Preprocessor-Options.html#index-dD-946 to see exactly what -dD does.