c++ - 使用 C/C++ DLL header 构建构建 C 应用程序时出现语法错误 C2059

标签 c++ c visual-studio dll

我正在尝试将 C++ DLL 头文件转换为 C/C++ 兼容头文件。虽然我已经掌握了大部分主要构造,但我遇到了最后一个我似乎无法解释的编译器问题。以下代码在 C++ 中运行良好,但当我尝试编译仅包含此文件的 C 应用程序时,我在头文件中遇到函数定义错误。

代码.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 syntax error using declspec macro for one function; compiles fine without it

http://support.microsoft.com/kb/117687/en-us

为什么会出现这个语法错误?

最佳答案

在 C 中,结构不是类型,因此您必须使用 struct Fooenum Bar,而在 C++ 中您可以使用 FooBar

注意事项:

  • 在 C++ 中,即使类型是类,您仍然可以使用旧语法。
  • 在 C 中,人们经常使用 typedef struct Foo Foo,它允许使用与 C++ 中相同的语法。

关于c++ - 使用 C/C++ DLL header 构建构建 C 应用程序时出现语法错误 C2059,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27828169/

相关文章:

c# - Visual Studio Ordered 测试运行两次

.net - Visual Studio 2013 中的内存分析

c++ - 从 c/c++ 或 linux 发布到 facebook 墙上

c - 让子进程等待父进程

c++ std::vector<> vs new[] 性能

c - fclose 不可能的正确错误处理(根据联机帮助页)?

c - 对象链接顺序问题

.net - 在源代码管理中管理我的数据库

c++ - 如何获取调用COM服务器方法的用户名?

C++ Qt - QString remove() {brackets} 之间的正则表达式