在#define 宏中转义# 符号?

在不涉及血淋淋的细节的情况下,我想使用一个 #define 宏,该宏将扩展为一个 #include 但#"符号会混淆预处理器(如它认为我想引用一个论点.)

Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)

例如,我想做这样的事情:

For example, I want to do something like this:

#define MACRO(name) #include "name##foo"

然后这样使用它:

MACRO(Test)

这将扩展为:

#include "Testfoo"

不起眼的 # 符号导致预处理器出错.MinGW 给我以下错误:

The humble # sign is causing the preprocessor to barf. MinGW gives me the following error:

'#'后面没有宏参数

我想我需要转义 # 符号,但如果这可能的话我不会.

I guess I need to escape the # sign but I don't if this is even possible.

是的,宏确实是邪恶的……

Yes, macros are indeed evil...

推荐答案

据我所知,你不能在定义中使用另一个预处理器指令.

As far as I remember you cannot use another preprocessor directive in define.

相关文章