gcc编译阶段打印宏定义的内容 - Suiyek - 博客园
宏参数(Arguments)的扩展 - chaoguo1234 - 博客园
#include <iostream>
#define PRINT_MACRO_HELPER(x) #x
#define PRINT_MACRO(x) #x "=" PRINT_MACRO_HELPER(x)
#define NUMBER_MACRO 3.14159
#define STRING_MACRO "This is a string."
#pragma message(PRINT_MACRO(NUMBER_MACRO))
#pragma message(PRINT_MACRO(STRING_MACRO))
#pragma message(PRINT_MACRO(UNDEF_MACRO))
int main(int argc, char** argv)
{
return 0;
}
编译过程中输出:
> g++ cstr2string.cpp -o cstr2string
cstr2string.cpp:9:42: note: #pragma message: NUMBER_MACRO=3.14159
#pragma message(PRINT_MACRO(NUMBER_MACRO))
^
cstr2string.cpp:10:42: note: #pragma message: STRING_MACRO="This is a string."
#pragma message(PRINT_MACRO(STRING_MACRO))
^
cstr2string.cpp:11:41: note: #pragma message: UNDEF_MACRO=UNDEF_MACRO
#pragma message(PRINT_MACRO(UNDEF_MACRO))
^
分类: C++