how to completely disable assertion

瘦欲@ 提交于 2019-11-26 09:29:52

问题


I have my code full of call to assert(condition). In the debug version I use g++ -g exploiting my assertion. With my surprise I can see assertion working also in my release version, the one compiled without -g option.

How can I completely disable at compile time my assertion? Should I explicitly define NDEBUG in any build I produce despite they are debug,release or whatever any other?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/5354314/how-to-completely-disable-assertion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!