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?
We can disable assertions in a program by using NDEBUG macro. Using NDEBUG macro in a program disables all calls to assert.
Using the -O flag (capital O) disables all assert statements in a process.
In Visual Studio, go to Debug / Windows / Exception Settings. In the Exception Settings, go to Win32 Exceptions / 0xc0000420 Assertion Failed. Uncheck the box in front of that entry.
The macro NDEBUG denotes not using debugging information. If NDEBUG is defined, then assert is defined as an expression which does nothing. Otherwise assert will print debugging information if the expression it tests is false.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With