_DEBUG 何时何地定义?

nit*_*ian 3 debugging gcc compiler-options c-preprocessor

我的头文件之一中有以下示例代码:

#ifdef _DEBUG
#define _DEBUG_NEW_REDEFINE_NEW 0
#include "debug_new.h"
#else
#define DEBUG_NEW new
#endif
Run Code Online (Sandbox Code Playgroud)

包含此头文件的应用程序是使用带-DDEBUG选项的 gcc 编译器编译的。

问题:

_DEBUG因为-DDEBUG选项而定义的吗?

Jef*_*kin 5

-DDEBUG只会定义DEBUG,不会_DEBUG。要弄清楚为什么(或是否)_DEBUG被定义,请尝试使用以下命令构建包含该标头的源文件

gcc --other_options source_file.cc -E -dD -o source_file.ii
Run Code Online (Sandbox Code Playgroud)

(您可能必须删除-o命令行中的另一个标志。)然后source_file .ii 将包含已定义的每个宏的 #define 行,以及# <lineno> <header>每次更改头文件时的行。

您可以阅读http://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/Preprocessor-Options.html#index-dD-946来了解具体的-dD作用。