Visual C++ 中的偵錯類別

更新:2007 年 11 月

在 Visual C++ 應用程式中使用 Debug 時,偵錯的組建 (Debug Build) 和發行的組建 (Release Build) 之間行為並未變更。

備註

Trace 的行為與偵錯類別的行為完全相同,只是必須根據所定義的符號 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 指示詞。以下是之前程式碼範例的修正,以示範這種修復 (Fix):

程式碼

// 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 程式設計指南