问题
I am trying to wrap a DLL, using its header file, through SWIG. I got a syntax error while processing my interface .i file using SWIG. After tracking down what was the offending line (line number of the printed by SWIG error message did not match the true offending line), I found that the minimum non-processable SWIG interface file is:
/* example.i */
%module example
%{
extern __declspec(dllimport) INT __cdecl function(int argument);
%}
extern __declspec(dllimport) INT __cdecl function(int argument);
Where the prototype tern extern __declspec(dllimport) INT __cdecl function(int argument);
was obtained from the header file I was trying to wrap.
There are tens of functions declared with this type. As you can see I dont have enough C experience to tell whether this type makes sense. The header file came from the vendor and it compiles OK using Visual C++ 6. Could somebody clarify me?
回答1:
OK... ended up learning what calling conventions are. The problem was how the SWIG interface file should be written.
/* 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
In the region where the function you want to wrap goes, should not contain the calling convention modifier (__cdecl,__stdcall,etc). But I did not want to remove the calling convention modifiers by hand, specially because I did not want to modify the header file I was trying to wrap (Imagine the vendor releasing new and incompatible version of header file, I would have to modify the header all over again). A solution is to #define the calling convention modifiers away, SWIG already provides a file that contain this #define'tions, the "windows.i". You just have to include it before including the header containing the functions you want to wrap. Like this:
/* example.i */
%module example
%{
#include "example.h"
%}
%include "windows.i"
%include "example.h"
where
/* example.h */
int __cdecl foo(void);
int __stdcall foo2(void);
I just don't understand why it does not do this by default. But I am happy with what I have found. Thank you guys for the pointers...
回答2:
Just the INT jumps out as trouble. That's a macro, you'd need like #include <windows.h>
to define it. It is very unlikely to be anything else but a regular int. The "extern" is superfluous, any compiler would ignore it.
来源:https://stackoverflow.com/questions/3294655/does-a-function-type-extern-declspecdllimport-int-cdecl-makes-sense-in-c