C++ NDEBUG 预处理器宏用于(在不同平台上)什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5473556/
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
What is the NDEBUG preprocessor macro used for (on different platforms)?
提问by Martin Ba
I'm interested in what purpose various platforms / compilers ("implementations") / frameworks assign to the the Cand C++ preprocessor macro NDEBUG
.
我对各种平台/编译器(“实现”)/框架分配给C和 C++ 预处理器宏的目的很感兴趣NDEBUG
。
The Cas well as the C++ standard only mention this definition once, namely to control the behavior of the assert()
macro.
该Ç以及C ++标准只提及这个定义一次,即控制的行为assert()
宏。
I would ask to include only specific answers, where you knowthat a certain platform / framework / library for Cor C++ uses the NDEBUG
definition to enable or disable anything else in addition to the standard defined assert()
macro.
我会要求只包含特定的答案,您知道C或 C++的某个平台/框架/库使用该NDEBUG
定义来启用或禁用除标准定义的assert()
宏之外的任何其他内容。
One reason for asking this question has been that MS (Visual-C++) always(?) uses "their" _DEBUG
define to distinguish between debug and release stuff and I was wondering if this is a common practice for a library / platform to have their "own" debug define or whether other libraries / platforms use NDEBUG
for their debug related stuff.
问这个问题的一个原因是 MS(Visual-C++)总是(?)使用“他们的”_DEBUG
定义来区分调试和发布内容,我想知道这是否是库/平台拥有他们的“的常见做法”自己的”调试定义或其他库/平台是否NDEBUG
用于其调试相关的东西。
采纳答案by DevSolar
That is a decision up to the maintainer(s) of the framework in question. Since such decisions are subject to change, it's nothing you should relyon. I use NDEBUG as a toggle for everything debug-related (e.g. trace outputs), but I might change my mind in the next release. No answer anyone could give here is a replacement for checking the API documentation of the frameworks you use in a given project.
这是由相关框架的维护者决定的。由于此类决定可能会发生变化,因此您不应依赖它。我使用 NDEBUG 作为与调试相关的所有内容(例如跟踪输出)的切换,但我可能会在下一个版本中改变主意。没有任何人可以在这里给出的答案是检查您在给定项目中使用的框架的 API 文档的替代方法。
That being said, using NDEBUG in library / framework headers, for anything else butassert()
, would be a rather dumb design decision. The application programmer is explicitlyallowed to set / unset NDEBUG however he sees fit, before and / or after including the headers of any library or framework, so the lib / framework maintainer could not rely on NDEBUG being set for a release lib or not set for a debugging lib. I doubt any significant project would have relied on NDEBUG that way.
话虽这么说,在图书馆/框架头使用NDEBUG,为别的,但是assert()
,将是一个相当愚蠢的设计决策。在包含任何库或框架的头文件之前和/或之后,明确允许应用程序员设置/取消设置 NDEBUG,但他认为合适,因此库/框架维护者不能依赖为发布库设置或未设置 NDEBUG对于调试库。我怀疑任何重要的项目都会以这种方式依赖 NDEBUG。
回答by Michael Burr
The only 'standard' thing about NDEBUG
is that it's used to control whether the assert
macro will expand into something that performs a check or not. MSVC helpfully defines this macro in release build configurations by defining it in the project for you. You can change that manually by editing the project configuration. Other toolchains might (or might not) do something similar.
唯一的“标准”NDEBUG
是它用于控制assert
宏是否会扩展为执行检查的内容。MSVC 通过在项目中为您定义此宏,在发布构建配置中对其进行了有益的定义。您可以通过编辑项目配置来手动更改它。其他工具链可能(也可能不会)做类似的事情。
Note that you can also change the state of the NDEBUG
macro within a translation unit (source file) using #define
and/or #undef
on NDEBUG
and re-include assert.h
to change how the assert
macro behaves (turn it on and off). That behavior is mandated by the standard, and is the only time (I think) where the standard permits including a standard header a second time to change the behavior of compilation after the second inclusion.
请注意,您还可以NDEBUG
使用#define
和/或#undef
打开NDEBUG
和重新包含assert.h
来更改翻译单元(源文件)中宏的状态,以更改assert
宏的行为方式(打开和关闭)。这种行为是标准规定的,并且是唯一一次(我认为)标准允许第二次包含标准标头以在第二次包含后更改编译行为。