çpreprocessor:扩大在一个指令#warning宏 [英] C preprocessor: expand macro in a #warning

查看:188
本文介绍了çpreprocessor:扩大在一个指令#warning宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印宏值(扩大宏)在#warning指令。

I would like to print a macro value (expand the macro) in the #warning directive.

例如,为code:

#define AAA 17
#warning AAA = ???

所需的编译时输出会

The desired compile-time output would be

warning: AAA = 17

我怎么使用?或者,我怎么充实到code?

What do I use for ???, or, how do I augment the code?

推荐答案

您可以使用preprocessor指令的#pragma消息

You can use the preprocessor directive #pragma message.

例如:

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#define AAA 123
#pragma message "content of AAA: " STR(AAA)

int main() { return 0; }

输出可能是这样的:

The output may look like this:

$ gcc test.c
test.c:5:9: note: #pragma message: content of AAA: 123
 #pragma message("content of AAA: " STR(AAA))
         ^

有关参考:

  • https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

这篇关于çpreprocessor:扩大在一个指令#warning宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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