如何在_Pragma的参数中连接字符串 [英] How to concatenate strings in the arguments of _Pragma

查看:164
本文介绍了如何在_Pragma的参数中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_Pragma的参数是一个字符串,因此我认为当以正常的c预处理器方式将字符串粘贴在一起(即,将它们彼此紧挨着放置)时,您可以为_Pragma的参数形成一个新字符串。但是

The argument of _Pragma is a string so I would think that when you paste strings together in the normal c-preprocessor way (ie putting them right next to eachother) that you could form a new string for the argument of _Pragma. However

_Pragma("GCC Poison " "puts")

发生错误

error: _Pragma takes a parenthesized string literal

如何避免这种情况?

这个特定的例子不是很有用,它有一个简单的解决方案,使它们都以一个字符串开头,但是最终目标是将一个宏串入其中。

This particular example isn't very useful and has a trivial solution of making them all one string to begin with, but the end goal is to stringize a macro into it

推荐答案

DO_PRAGMA 在GNU文档中的定义如下

#define DO_PRAGMA(x) _Pragma (#x)

使用此方法,如果您将两个未串起的单独令牌彼此相邻放置,它们将被串化。要在定义中扩展宏,它必须经过一个间接级别,因此定义为

Using this, if you put two seperate token unstringized next to eachother, they will become stringized. To expand macros within the definition, it must go through one level of indirection, so define as

#define DO_PRAGMA_(x) _Pragma (#x)
#define DO_PRAGMA(x) DO_PRAGMA_(x)

使用这个可以为各种编译指示创建简写

Using this you can create shorthands for various pragmas like this

#define POISON(name) DO_PRAGMA(GCC poison name)
POISION(puts)//becomse _Pragma("GCC poison puts")

感谢Eugene Sh。将我指向 DO_PRAGMA

Thanks to Eugene Sh. for pointing me to the DO_PRAGMA macro

这篇关于如何在_Pragma的参数中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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