25

出荷ビルドのみを実行する必要があり、問題が修正されたかどうかを確認するために、リリース ビルドの特定の条件でアサートする必要があります。どうすればいいのですか?

4

7 に答える 7

26

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.

于 2009-03-06T23:44:32.433 に答える
19

独自のアサートを定義してみませんか。

#define assert(x) MessageBox(...);
于 2009-03-07T00:36:15.587 に答える
5

assertリリース モードでアクティブなマクロ定義の部分を直接呼び出すだけです。

Miro Samek によるこの素晴らしい記事( PDF )では、C++ でのアサーションの非常に便利な定義を見つけることができます。次に、ニーズを満たすためにそれらを少し調整できます。たとえばrelease_assert、リリース モードかデバッグ モードかに関係なく、assert と同じことを行う別のマクロ を作成できます。

于 2009-03-10T08:51:07.773 に答える
3

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.

于 2009-03-06T23:48:12.003 に答える
2

std::runtime_error から派生した何らかの assert_exception をスローするように定義するのが好きです。それからどこかでそれをキャッチして、何か役に立つことをしてください。

于 2009-05-15T04:12:15.867 に答える
1

Visual Studioを使用する場合、リリースビルドでアクティブなアサートに対してNDEBUGプリコンパイラ定義の定義を解除できます。

たとえば、/ Uオプションのプロジェクト設定で$(undefesTheNDEBUG)を設定してから、環境変数undefesTheNDEBUGをNDEBUG(SET undefesTheNDEBUG = NDEBUG)に定義するか、msbuild(/ p:undefesTheNDEBUG = NDEBUG)と一緒に渡すことができます。

于 2011-03-29T06:31:50.020 に答える
1

実際には、問題なく使用できるのであれば、デバッグ バージョンを出荷することをお勧めします。リリース版のパフォーマンスが必要ない場合は、デバッグを使用してください。バグが少ない傾向にあります (これは非常に単純化されたものであり、プログラムにバグがない場合、リリースに切り替えるだけではこれは変わりませんが、コンパイラがデバッグ モードで行うことにより、バグが発生しないか、バグが発生する可能性があります)。それほど深刻な結果にはなりません)。

おそらく、プログラムのタイム クリティカルな部分だけを最適化することも可能です。

また、デバッグを簡素化することもできます。

于 2009-09-28T13:04:32.097 に答える