c - 函数类型 "extern __declspec(dllimport) INT __cdecl"在 C/C++ 或 SWIG 中有意义吗?

标签 c swig word-wrap

我正在尝试通过 SWIG 使用其头文件包装一个 DLL。使用 SWIG 处理我的接口(interface) .i 文件时出现语法错误。在追踪到有问题的行之后(SWIG 错误消息打印的行号与真正的有问题的行不匹配),我发现最小不可处理的 SWIG 接口(interface)文件是:

/* example.i */
%module example
%{
extern  __declspec(dllimport) INT __cdecl function(int argument);
%}
extern  __declspec(dllimport) INT __cdecl function(int argument);

原型(prototype) tern extern __declspec(dllimport) INT __cdecl function(int argument); 是从我试图包装的头文件中获得的。

用这种类型声明的函数有几十个。如您所见,我没有足够的 C 经验来判断这种类型是否有意义。头文件来自供应商,它可以使用 Visual C++ 6 编译。有人可以解释一下吗?

最佳答案

好的...最终了解了什么是调用约定。问题在于应该如何编写 SWIG 接口(interface)文件。

/* example.i */
%module example
%{
int __cdecl foo(void);      // function declarations for the function 
int __stdcall foo2(void);   // you intend to wrap goes in here
%}
int foo(void);          // function you want to wrap goes in here
int foo2(void);         // without the calling convention

在你要包装的函数所在的区域,不应包含调用约定修饰符(__cdecl、__stdcall 等)。但我不想手动删除调用约定修饰符,特别是因为我不想修改我试图包装的头文件(想象供应商发布新的和不兼容的头文件版本,我将不得不修改头文件重新来过)。一个解决方案是去掉 #define 调用约定修饰符,SWIG 已经提供了一个包含此 #define'tions 的文件,即“windows.i”。您只需要在包含包含要包装的函数的 header 之前包含它。像这样:

/* example.i */
%module example
%{
#include "example.h"
%}
%include "windows.i"
%include "example.h"

在哪里

/* example.h */
int __cdecl foo(void);
int __stdcall foo2(void);

我只是不明白为什么默认情况下它不这样做。但我对我的发现很满意。谢谢各位指点...

关于c - 函数类型 "extern __declspec(dllimport) INT __cdecl"在 C/C++ 或 SWIG 中有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3294655/

相关文章:

C 程序未终止

c - C 代码段错误 : Return value

python - 理解 Swig 生成的 Python 文件

python - 如何编写 C++ 代码以使用 SWIG 在 python 提示符中打印变量值

c - 关于 Linux Hugepage

C - 将数组传递给链表的节点

c - 为什么这段代码不能识别任何 if 语句?

CSS: wrap text <pre> 标签断词问题

javascript - 启用换行后如何在 Ace Editor 中禁用后续行的缩进

c# - 如何实现自动换行?