定义名称即宏为空

Define name i.e. macro as nothing

在使用第 3 方共享库(即 DLL)时,我必须通过符号名称定义可用的函数。所以我必须声明一些函数类型。

但是函数定义涉及调用约定,在我的例子中是 __cdecl

所以,当然,这个调用约定只适用于 MSVC 编译器,但我仍然想编译我的可移植 API 抽象代码,它使用 boost::dll 和 linux,甚至虽然我不能在我的开发 linux 机器上执行它,所以我仍然可以检查编译时的东西。

所以我的想法是用预处理器将 __cdecl 名称定义为空或白色 space,NOP,但我是无法做到这一点:

#if !BOOST_OS_WINDOWS
#define  __cdecl ( ) /* error, how can I define __cdecl as 'nothing" here? */
#endif

#include <boost/dll/import.hpp>
boost::dll::shared_library lib;

unsigned long device_count(char* pid) {
    typedef unsigned long(__cdecl func_sig)(char *pvid_id);
    return lib.get<func_sig>("DLL_Symbol")(pid);
}
#define __cdecl

再简单不过了!或者更好的是,限制实现保留标识符的传播:

#if BOOST_OS_WINDOWS
    #define YOURLIB_CDECL __cdecl
#else
    #define YOURLIB_CDECL /* nothing */
#endif