预处理器宏 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));
该宏在 VS 编译器中运行良好,但在 GCC 编译器中无法编译:
错误:错误:粘贴“/”和“includefile”不会给出有效的预处理令牌
,对于某些包含文件,它会给出错误:
错误:粘贴“includefile”和“.”没有给出有效的预处理令牌
#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:
Error: error: pasting "/" and "includefile" does not give a valid preprocessing token
and for some include files it gives the error:
Error: pasting "includefile" and "." does not give a valid preprocessing token
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GCC 在执行 C 标准方面更加严格:请参阅 Visual-C++ 和 gcc 之间宏 ## 连接运算符的差异 和 http://gcc.gnu.org/onlinedocs/gcc-4.3.3/cpp/Concatenation.html#Concatenation。
您可以尝试
#include STRINGIFY(PATH FILNAME.h)
(FILNAME
和.h
之间缺少空格很重要)——这有效对于我来说,gcc 4.6.3。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.
You might try
#include STRINGIFY(PATH FILNAME.h)
(the lack of space betweenFILNAME
and.h
is important) -- that works for me with gcc 4.6.3.