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

2019-02-02 09:26发布

I need to only run ship build and I need to assert on certain condition in release build to see if the problem is fixed. How do I do it?

7条回答
老娘就宠你
2楼-- · 2019-02-02 09:48

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)

查看更多
The star\"
3楼-- · 2019-02-02 09:50

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.

查看更多
我命由我不由天
4楼-- · 2019-02-02 09:50

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.

查看更多
神经病院院长
5楼-- · 2019-02-02 09:54

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-02-02 09:58

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.

查看更多
一纸荒年 Trace。
7楼-- · 2019-02-02 10:08

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.

查看更多
登录 后发表回答