【发布时间】:2015-11-30 01:45:41
【问题描述】:
这类似于How do I show the value of a #define at compile-time?。 Chris Barry 的回答对我不起作用:
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
...
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message "The value of GCC_VERSION: " XSTR(GCC_VERSION)
结果:
$ rm -f dlltest.o && make dlltest.o
g++ -DNDEBUG -g2 -O2 -march=native -pipe -c dlltest.cpp
dlltest.cpp:13:80: note: #pragma message: The value of GCC_VERSION: (4 * 10000 + 9 * 100 + 3)
#pragma message "The value of GCC_VERSION: " XSTR(GCC_VERSION)
^
dlltest.cpp:12:17: note: in definition of macro ‘STR’
#define STR(x) #x
^
dlltest.cpp:13:55: note: in expansion of macro ‘XSTR’
#pragma message "The value of GCC_VERSION: " XSTR(GCC_VERSION)
从上面可以看出,数值没有被打印出来。此外,还打印了很多多余的无用草料。
如何让 GCC 打印基于其他宏的定义的数值?
【问题讨论】:
-
Boost 有一些宏 (
BOOST_PP_ADD,BOOST_PP_MUL) 用于 PP 时间数学。但是,数字系统类似于 [0, 255]。 -
@Chris - 我没有使用 Boost。
标签: c++ macros c-preprocessor pragma