打印数值的定义是基于经过编译的消息等宏? [英] Print numeric value of a define that's based on other macros via pragma message?

查看:195
本文介绍了打印数值的定义是基于经过编译的消息等宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是类似于如何显示的#define在编译时的价值?.克里斯·巴里(Chris Barry)的答案对我不起作用:

This is similar to How do I show the value of a #define at compile-time?. Chris Barry's answer is not working for me:

#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)

如蜂上方观察时,数字值不打印.此外,很多额外无用饲料印刷.

As can bee seen above, the numeric value was not printed. In addition, a lot of extra useless fodder was printed.

我怎么能有GCC打印数值的定义是基于其他的宏?

How can I have GCC print the numeric value of a define that's based on other macros?

推荐答案

预处理器不执行算术替换. (在,它可以计算真/假结果,但不幸的是这其中预处理程序所做的计算的唯一地方.)

The preprocessor doesn't do arithmetic substitutions. (In a #if, it can compute a true/false result, but unfortunately that's the only place where the preprocessor does computations.)

在升压预处理器库解决了这个问题,通过使用可能的参数运算海量枚举,但它仅限于操作上(很)小整数.这是聪明的,但不能扩展.

The boost preprocessor library works around this by using massive enumerations of possible arguments to arithmetic operations, but it is límited to operations on (very) small integers. It's clever but not scalable.

因此,使用字符串连接比使用算术更好.

So you are better off using string concatenation than arithmetic.

#pragma message "GCC version: " XSTR(__GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__)

不幸的是,GCC不仅会产生该消息,而且原来的源极线的回声.这可能是更好的包裹在宏整个事情,使用特征,使得回荡源极线仅由一个字:

Unfortunately, gcc will not only produce the message, but also an echo of the original source line. It might be better to wrap the whole thing in a macro, using the _Pragma feature, so that the echoed source line consists only of a single word:

#define STR(x) #x
#define XSTR(x) STR(x)
#define MSG(x) _Pragma (STR(message (x)))
#define DISPLAY_GCC_VERSION \
  MSG("GCC version: " XSTR(__GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__))

DISPLAY_GCC_VERSION

这篇关于打印数值的定义是基于经过编译的消息等宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆