使用 C 预处理器在参数名称中加星号

Asterisk in the argument name with C preprocessor

我想用 mingw32/VC.
实现我的 DLL 的跨平台构建 目前,mingw 方面的一切都很完美。但是我必须在 VC 的宏中包装一些东西(它被构建为 /TC),例如:

void __attribute__((fastcall)) do1 (  A*, B , C, D );
bool __attribute__((fastcall)) ( *do2 ) ( E*, F );

第一个很简单,就是一个宏:

#ifdef __MINGW32__
    #define __FASTCALL__ __attribute__((fastcall))
#elif _MSC_VER
    #define __FASTCALL__ __fastcall
#else
    #error "unsupported compiler"
#endif

问题出在第二个。使用函数指针的调用约定应该类似于

bool ( __fastcall *do2 ) ( E*, F );

我尝试了以下宏(我跳过了 ifdef 部分):

#define __FASTCALLP__(func) (__attribute__((fastcall))(*##func))
#define __FASTCALLP__(func) (__fastcall *##func)

或者如果传递带有星号的函数名:

#define __FASTCALLP__(func) (__attribute__((fastcall))(##func))
#define __FASTCALLP__(func) (__fastcall ##func)

都失败了

error: pasting "*" and "function_name" does not give a valid preprocessing token

我的方法可能完全错误吗?或者我必须对整个代码块进行 ifdef 或将其分成不同的文件?

问题出在串联运算符 ## 上。它将通过连接不存在的左侧和右侧来生成新的预处理器标记(*do2 是未定义的标记)

干脆省略,这样写(省略#ifdefs):

#define __FASTCALL__(func) (__attribute__((fastcall))(func))
#define __FASTCALL__(func) (__fastcall func)

并像这样使用:

bool __FASTCALL__(do1)(A*, B , C, D);
bool __FASTCALL__(*do2)(E*, F);