How can I completely disable calls to assert()?

C++CAssert

C++ Problem Overview


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.

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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

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.

Solution 5 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbruzzo Forte e GentileView Question on Stackoverflow
Solution 1 - C++GWWView Answer on Stackoverflow
Solution 2 - C++Prasoon SauravView Answer on Stackoverflow
Solution 3 - C++AlnitakView Answer on Stackoverflow
Solution 4 - C++dcnView Answer on Stackoverflow
Solution 5 - C++Fred FooView Answer on Stackoverflow