次の方法で共有


Debug クラス (C++/CLI)

Visual C++ アプリケーションで Debug を使用した場合、デバッグ ビルドとリリース ビルドの間で動作の違いはありません。

解説

Trace の動作は、Debug クラスの動作と同じですが、定義されているシンボル TRACE に依存します。 つまり、リリース ビルドでのデバッグ動作を防ぐには、#ifdef で Trace 関連コードを定義する必要があります。

説明

次のサンプルは、/DDEBUG または /DTRACE を指定してコンパイルするかどうかに関係なく、常に出力ステートメントを実行します。

コード

// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();

   Debug::WriteLine("test");
}

出力

    Entering Main
Hello World.
    Exiting Main
test

説明

予測どおりの動作を行うようにする (つまり、リリース ビルドで "test" が出力されないようにする) には、#ifdef ディレクティブと #endif ディレクティブを使用する必要があります。 上に示したコード例を修正するには、次のように変更します。

コード

// mcpp_debug_class2.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();

#ifdef TRACE   // checks for a debug build
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
#endif
   Trace::Unindent();

#ifdef DEBUG   // checks for a debug build
   Debug::WriteLine("test");
#endif   //ends the conditional block
}

参照

その他の技術情報

.NET プログラミング ガイド