C++ 除錯技巧: Using Preprocessor Directive

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

Debug Tips -- using Preprocessor Directive
#ifdefDEBUG
/*
Debug information
*/
#endif

__LINE__:被編譯的程式碼行號
__FILE__:檔案名稱
__TIME__:檔案被編譯的時間
__DATE__:檔案被編譯的日期

g++ compile option: -DDEBUG

// Sample Code
#include<iostream>
#include<vector>

usingstd::vector;
usingstd::cout;
usingstd::string;
usingstd::cin;
usingstd::endl;

intmain()
{
cout << "File name: "<< __FILE__ << endl;
cout << "Compile at "<< __DATE__ << ": "<< __TIME__ << endl;
#ifdefDEBUG
cout << "Beginning execution of main().\n";
#endif

string word;
vector< string > text;

while( cin >> word )
{
#ifdefDEBUG
cout << "word read: "<< word << " at line: "<< __LINE__ << "\n";
#endif

text.push_back( word );
}

return0;
}


沒有留言: