我正在尝试将C ++ DLL标头文件转换为C / C ++兼容标头。虽然我已经了解了大多数主要结构,但我遇到了最后一个编译器问题,我似乎无法解释。以下代码在C ++中可以正常工作,但是当我尝试编译仅包含此文件的C应用程序时,在头文件中出现函数定义错误。
Code.h:
typedef void *PVOID;
typedef PVOID HANDLE;
#define WINAPI __stdcall
#ifdef LIB_EXPORTS
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif
struct ToolState
{
HANDLE DriverHandle;
HANDLE Mutex;
int LockEnabled;
int Type;
};
#ifdef __cplusplus
extern "C" {
#endif
(LIB_API) int SetRate(ToolState *Driver, int rate);
(LIB_API) void EnableLock(ToolState *Driver) ;
(LIB_API) int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//These also give me the same error:
//LIB_API WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//__declspec(dllimport) WINAPI int SendPacket(ToolState *Driver, unsigned char *OutBuffer, int frameSize);
//Original C++ call that works fine with C++ but has multiple issues in C
//LIB_API int SetRate(ToolState *Driver, int rate);
#ifdef __cplusplus
}
#endif
错误:
error C2059: syntax error : 'type'
error C2059: syntax error : 'type'
error C2059: syntax error : 'type'
Google搜索未产生任何相关结果。以下线程很接近,但不能完全回答我的问题:
C2059使用declspec宏的一个函数的语法错误;没有它就可以编译
http://support.microsoft.com/kb/117687/en-us
为什么会出现此语法错误?
在C中,结构不是类型,因此必须使用struct Foo
andenum Bar
在C ++中可以使用Foo
and的位置Bar
。
笔记:
typedef struct Foo Foo
它允许与C ++语言相同的语法。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句