How to put assert into release builds in C/C++

天大地大妈咪最大 提交于 2019-11-30 03:54:53

Undefine the NDEBUG macro - you can do this locally around the asserts you want to remain in the build:

#undef NDEBUG
#include <assert.h>   // reinclude the header to update the definition of assert()

or do whatever you need to do so your build process does not define the NDEBUG macro in the first place.

Baiyan Huang

Why not just define your own assert:

#define assert(x) MessageBox(...);
Daniel Daranas

Just call directly the part of the assert macro definition that is active in release mode.

You can find very useful definitions for assertions in C++ in this great article by Miro Samek (PDF). Then you can tweak them a bit to satisfy your needs. For example, you might create another macro, release_assert, that does the same as assert but regardless of whether it's in release or debug mode.

The default behaviour of ASSERT is to abort the program under the Debug configuration, but this generally becomes a no-op under a Release configuration. I believe it does this by checking for the existence of the preprocessor NDEBUG macro. I'm not at work at the moment so cannot check this.

I think the easiest way around this is to modify the Debug configuration to turn all optimisations up to the same level as Release (O2 from memory), and then re-build your software. This will give you the equivalent performance and speed of a Release build, but it will still define the NDEBUG preprocessor macro which means all failed ASSERTs will still cause the program to abort. Just remember to change the optimisation level back later, otherwise you will have trouble debugging under the Debug configuration.

In general though, ASSERTs should only be used for programming preconditions and never to handle failures in shipping software. You want to fail quickly during development, but gracefully in front of a user.

I like to define it to throw some sort of assert_exception derived from a std::runtime_error. Then catch it somewhere and do something useful.

Actually - I'd go with shipping the debug version if you can live with it. If you don't need the performance of the release version, use the debug. It tend's to have fewer bugs (this is a gross oversimplification and if you program is free of bugs, just switching to release does not change this, but due to things the compiler does in debug mode, the bugs may not occur and/or have less severe consequences).

Perhaps it is also possible to optimize only the time critical parts of you program.

It can also simplify debugging.

When using Visual Studio you can undefine the NDEBUG precompiler definition to active asserts in the Release build.

For example you can set $(undefesTheNDEBUG) in your Projekt settings for the /U option and then define the environment variable undefesTheNDEBUG to NDEBUG (SET undefesTheNDEBUG=NDEBUG) or pass it along with msbuild (/p:undefesTheNDEBUG=NDEBUG)

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