編譯時在輸出窗口直接顯示:
==================================== File: test.cpp Line: 42 TODO : not implements now ! ==================================== ==================================== File: test.cpp Line: 43 HACK : function implemented by a hack way! ==================================== ==================================== File: test.cpp Line: 44 FIXME : buggy ! ! ====================================
利用 MSVC 編譯器的 #pragma message 或 __pragma(message(...)) 在編譯期輸出自定義信息。關鍵技巧:
__FILE__, __LINE__ 獲取位置。# 將行號轉為字符串。\n 換行。#define TOOLS_STRINGIZE(L) #L #define TOOLS_MAKESTRING_IMPL(M, L) M(L) #define TOOLS_MAKESTRING(x) TOOLS_MAKESTRING_IMPL(TOOLS_STRINGIZE, x) #define TOOLS_MESSAGE_LINE "\n====================================\n" #define TOOLS_MESSAGE_POSITION __FILE__ "\nLine:" TOOLS_MAKESTRING(__LINE__) "\n" #define TODO(x) __pragma(message(TOOLS_MESSAGE_LINE TOOLS_MESSAGE_POSITION "TODO : " x TOOLS_MESSAGE_LINE)) #define HACK(x) __pragma(message(TOOLS_MESSAGE_LINE TOOLS_MESSAGE_POSITION "HACK : " x TOOLS_MESSAGE_LINE)) #define FIXME(x) __pragma(message(TOOLS_MESSAGE_LINE TOOLS_MESSAGE_POSITION "FIXME : " x TOOLS_MESSAGE_LINE))
TODO("not implements now !")
HACK("this function is implemented by a hack way!")
FIXME("buggy ! !")
int main(int argc, char* argv[]) { return 0; }
#warning 或 _Pragma("GCC warning ...")。#ifdef _MSC_VER #define COMPILE_MSG(msg) __pragma(message(msg)) #elif defined(__GNUC__) #define COMPILE_MSG(msg) _Pragma(#msg) #else #define COMPILE_MSG(msg) #endif