如何通过SWIG使用单个C函数



我有一个SWIG C-python接口。我想让test.c中的各个函数在我的python接口中可用。我觉得我在文档上浪费时间,却没有找到简单的方法。我知道的唯一选择是:

%module test
%{
#include "test.h"
//#include "test.c"
%}
%include "stdint.i"  # Add support for uint8_t, int32_t, etc...
/** Keep test immutable for now because we have const struct members in the
*  header file which can not be reassigned.
*/
%immutable;
%include test.h // Test header for interface debugging
//%include test.c
//%import test.h
%mutable;

我尝试在接口的%{}部分中#include"test.c"(此部分只是将文件内容复制到目标接口中(、%include"test.c"或%import"test.c">

但是我在"test.c"中有一大堆其他函数,它们与其他文件有依赖关系,我无法在python中导入生成的接口(ImportError:未定义的符号:(

#include "test.h"
#include "someotherfunctions.h"
#include "specialdefines.h"

int testFunc(int n, int m) // The function i want to use
{
int res = n + m;
return res;
}
int otherFunc(void)
{
otherDifferentFunc(); // Stuff from other header/c files which my interface doesnt know
}
SPECIALMACRO(X) // More stuff from other headers that i dont want in my interface
int foo = someOtherOperation(500);

头文件测试.h:

/* Functions ----------------------------------------------------------------*/
int testFunc(int n, int m);

我觉得我没有正确的方法在Python接口中提供单个函数,而不在源文件的所有头文件中包含所有不需要的东西。事实上,我的"test.c"相当大,包含了一大堆用于各种符号和所用外部函数的头。如果我没有#在我的%{}接口部分中包括所有这些,我会得到导入错误。但是,如果我包含它们,它们再次需要其他头(这是一个大项目(,并且我正在为一个小函数启动一个混乱的包含链,这会使我的界面不必要地膨胀。这不可能是SWIG的方式,伙计们?

如果有人知道解决这个问题的好方法,请告诉我。

%module test
%{
// Whatever goes here is directly copied to the generated wrapper code.
// Put what you need here for the wrapper to build, typically just
// your public headers.
#include "test.h"
%}
// Anything you declare or %import is processed to make the SWIG interface.
// You can %import "test.h" and  all the functions *directly* in
// the header (not other #include in the header), are exposed to Python.
//
// If you only want one function, just put its prototype, like:
int testFunc(int n, int m);

构建包装器时,切换.i文件以生成test_wrap.c,然后编译test.c和test_wrap.c.并在链接步骤将它们链接在一起。

一个例子:

测试.i

%module test
%{
#include "test.h"
%}
// uncomment to bring in func1 and func2 (not func3, but it's still used by func2)
//%include "test.h"
// only exposes func1
int func1();

测试.h

int func1();
int func2();

测试.c

#include "test.h"
int func3() { return 3; }
int func2() { return func3() - 1; }
int func1() { return func2() - 1; }

生成文件(Windows、Microsoft编译器(

all: _test.pyd
test_wrap.c test.py: test.i test.h
echo swigging...
swig -python test.i
_test.pyd: test_wrap.c test.c test.h
cl /nologo /LD /MD /W3 /Fe_test.pyd /Id:devpython310include test_wrap.c test.c -link /libpath:d:devpython310libs

输出(如上所述构建,仅暴露func1(((:

>>> import test
>>> test.func1
<function func1 at 0x000001CF6DEF7F40>
>>> test.func1()
1
>>> test.func2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'test' has no attribute 'func2'. Did you mean: 'func1'?

输出(构建时取消注释%include并注释原型(:

>>> import test
>>> test.func1()
1
>>> test.func2()
2
>>> test.func3()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'test' has no attribute 'func3'. Did you mean: 'func1'?