如何完全禁用对 assert() 的调用? [英] How can I completely disable calls to assert()?
问题描述
我的代码充满了对assert(condition)
的调用.在调试版本中,我使用 g++ -g
触发我的断言.出乎意料的是,在我的发布版本中也触发了相同的断言,即没有 -g
选项编译的版本.
My code is full of calls to assert(condition)
.
In the debug version I use g++ -g
which triggers my assertions.
Unexpectedly, the same assertions are also triggered in my release version, the one compiled without -g
option.
如何在编译时完全禁用我的断言?我是否应该在我生成的任何构建中明确定义 NDEBUG
,而不管它们是调试、发布还是其他任何内容?
How can I completely disable my assertions at compile time? Should I explicitly define NDEBUG
in any build I produce regardless of whether they are debug, release or anything else?
推荐答案
您必须 #define NDEBUG
(或使用标志 -DNDEBUG
和 g++em>) 这将禁用断言,只要它在包含断言头文件之前定义.
You must #define NDEBUG
(or use the flag -DNDEBUG
with g++) this will disable assert as long as it's defined before the inclusion of the assert header file.
这篇关于如何完全禁用对 assert() 的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!