C/C++编译时信息提示

在C/C++编译的时候,有时候会需要编译时log输出或是判断。可以参考编译的#error "message" 和#pragma message("")

代码参考
error, 如果被编译到,就会提示信息, 并结束编译过程

error token-string

The #error directive emits a user-specified error message at compile time, and then terminates the compilation.

#error "should not happened"

#pragma message("")则是编译时打印log

pragma message( message-string )

A typical use of the message pragma is to display informational messages at compile time.
The message-string parameter can be a macro that expands to a string literal, and you can concatenate such macros with string literals in any combination.

// pragma_directives_message1.cpp
// compile with: /LD
#if _M_IX86 >= 500
#pragma message("_M_IX86 >= 500")
#endif

#pragma message("")

#pragma message( "Compiling " __FILE__ )
#pragma message( "Last modified on " __TIMESTAMP__ )

#pragma message("")

// with line number
#define STRING2(x) #x
#define STRING(x) STRING2(x)

#pragma message (__FILE__ "[" STRING(__LINE__) "]: test")

#pragma message("")
标签:

About: kiah