我怎样才能完全禁用调用声明() ?

我的代码充满了对 assert(condition)的调用。 在调试版本中,我使用 g++ -g触发我的断言。 出乎意料的是,同样的断言在我的版本中也会被触发,这个版本没有使用 -g选项进行编译。

如何在编译时完全禁用断言?我是否应该在我生成的任何构建中明确定义 NDEBUG,而不管它们是调试、发布还是其他任何东西?

72176 次浏览

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.

You can either disable assertions completely by

#define NDEBUG
#include <assert.h>

or you can set NDEBUG (via -DNDEBUG) in your makefile/build procedure depending on whether you want a productive or dev version.

Yes, define NDEBUG on the command line/build system with the preprocessor/compiler option -DNDEBUG.

This has nothing to do with the debugging info inserted by -g.

The -g flag doesn't affect the operation of assert, it just ensures that various debugging symbols are available.

Setting NDEBUG is the standard (as in official, ISO standard) way of disabling assertions.

Use #define NDEBUG

7.2 Diagnostics

1 The header defines the assert macro and refers to another macro,

NDEBUG

which is not defined by <assert.h>. If NDEBUG is defined as a macro name at the point in the source file where is included, the assert macro is defined simply as

#define assert(ignore) ((void)0)

The assert macro is redefined according to the current state of NDEBUG each time that <assert.h> is included.