gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:40 25 4
gpt4 key购买 nike

我正在尝试通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com