【学习笔记】预处理宏 GCC:粘贴 x 和 x 并不生成有效的预处理标记

Preprocessor macro GCC: pasting x and x does not give a valid preprocessing token

预处理宏 GCC:粘贴 x 和 x 并不生成有效的预处理标记

#define  PATH  "yagh/headers/"
#define  FILNAME  "includefile"


#define CONCAT(a__, b__) CONCAT_DO(a__, b__)
#define CONCAT_DO(a__, b__) a__##b__
#define CONCATTHREE(a__, b__, c__) CONCAT(CONCAT(a__, b__), c__)
#define STRINGIFY(a__) #a__


#include STRINGIFY(CONCATTHREE(PATH  ,FILNAME  ,.h));

This macro works fine in VS compiler but does not compile on GCC compiler:

这个宏在 VS 编译器中工作正常,但在 GCC 编译器上无法编译:

Error: error: pasting “/” and “includefile” does not give a valid preprocessing token

错误:错误:粘贴 “/” 和 “includefile” 并没有生成有效的预处理标记

and for some include files it gives the error:

对于一些 include 文件,它给出了错误:

Error: pasting “includefile” and “.” does not give a valid preprocessing token
错误:粘贴 “includefile” 和 “.” 并没有生成有效的预处理令牌

回答

GCC is being a bit stricter in enforcing the C standard: see Differences in Macro ## concatenation operator between Visual-C++ and gcc and http://gcc.gnu.org/onlinedocs/gcc-4.3.3/cpp/Concatenation.html#Concatenation.

GCC 在遵循 C 标准时更加严格:参见 Visual-C++ 和 gcc 之间宏 ## 连接操作符的差异http://gcc.gnu.org/onlinedocs/gcc-4.3.3/cpp/Concatenation.html#Concatenation

You might try #include STRINGIFY(PATH FILNAME.h) (the lack of space between FILNAME and .h is important) – that works for me with gcc 4.6.3.

你可以尝试 #include STRINGIFY(PATH FILNAME.h)FILNAME.h 之间缺少空格很重要) - 这个方法对我使用 gcc 4.6.3 来说是有效的。

[参考资料]

c - Preprocessor macro GCC: pasting x and x does not give a valid preprocessing token - Stack Overflow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值