C++ 根据 Visual Studio 中的调试/发布配置自动#defines

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4604283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:54:09  来源:igfitidea点击:

Automatic #defines according to Debug/Release config in Visual Studio

c++visual-studiovisual-studio-2010compilation

提问by Felix Dombek

I have debug output in my program like this:

我的程序中有这样的调试输出:

#define DEBUG
...
#ifdef DEBUG
    std::cout << "[RE_words] " << m_re << std::endl;
#endif

and DEBUGis defined in my program manually. I always comment out the line when I make a release version. In Visual Studio, there are also Configurations for Debugvs Releaseversions which handle the commandline etc. used for compiling. Can I also use the Configuration "Debug" to automatically define DEBUGto the compiler? How?

DEBUG在我的程序中手动定义。当我制作发布版本时,我总是注释掉这一行。在 Visual Studio 中,还有用于DebugRelease版本的配置,用于处理用于编译的命令行等。我还可以使用配置“调试”来自动定义DEBUG给编译器吗?如何?

回答by Jan Holecek

The Visual Studio automatically defines _DEBUG symbol for Debug builds (and NDEBUG for non-debug builds).

Visual Studio 会自动为调试版本定义 _DEBUG 符号(以及为非调试版本定义 NDEBUG)。

Another way to do this is to go to the project settings -> configuration properties -> C/C++ -> preprocessor, and edit the preprocessor definitions manually.

另一种方法是转到项目设置 -> 配置属性 -> C/C++ -> 预处理器,并手动编辑预处理器定义。

See also:
This answerexplains the differences between _DEBUG and NDEBUG in more detail.
This answerexplains the purpose of the NDEBUG symbol and whether or not is it defined by the standard.

另请参阅:
此答案更详细地解释了 _DEBUG 和 NDEBUG 之间的区别。
这个答案解释了 NDEBUG 符号的用途以及它是否由标准定义。

回答by Moo-Juice

Use _DEBUG. Visual C++ defines this for a Debug configuration. Check out the preprocessor directives for the Debug Configuration in your project's properties dialog.

使用_DEBUG. Visual C++ 为调试配置定义了它。在项目的属性对话框中查看调试配置的预处理器指令。

回答by Quijote

I too thought I just had to look at the preprocessor property and remove _DEBUG. Visual Studio tries to help out by setting _DEBUG if you select one of the debug run-time library options.

我也认为我只需要查看预处理器属性并删除 _DEBUG。如果您选择调试运行时库选项之一,Visual Studio 会尝试通过设置 _DEBUG 来提供帮助。

On the project property page Configuration Properties\C/C++\Code Generationthe option selected for Runtime Libraryaffects several defines. When selecting a debug library (/MTdor /MDd) the _DEBUGdefine isset.

在项目属性页Configuration Properties\C/C++\Code Generation 上,为Runtime Library选择的选项会影响多个定义。选择调试库(/MTd/MDd)时,设置_DEBUG定义。

See MSDN /MD, /MT ...for more info on the switches. There are several #defines that are set based on these options. They are pretty invisible when trying to find who sets a #define before you even include any header files!!

有关 开关的更多信息请参阅MSDN /MD、/MT ...。有几个#defines 是基于这些选项设置的。在您甚至在包含任何头文件之前尝试查找谁设置了#define 时,它​​们是非常不可见的!!

回答by Nickolay Olshevsky

Yes, you should check project options page, Compile->Advanced compile options. However, VS by default automatically defines DEBUG directive for Debug mode.

是的,您应该检查项目选项页面,Compile->Advanced compile options。但是,VS 默认情况下会自动为 Debug 模式定义 DEBUG 指令。