使用Swig在C ++中调用Python函数

时间:2010-07-21 17:40:22

标签: python swig

这是我的c ++代码:

void callMethod(void (*someMethod)()) {
    (*someMethod)();
}

我的Swig .i文件是:

%module test
%{
#define SWIG_FILE_WITH_INIT

extern void callMethod(void (*someMethod)());
%}

%typemap (in) void*
%{
    $1 = PyCObject_AsVoidPtr($input);
%}


extern void callMethod(void (*someMethod)());

这是我的错误:

In [1]: import test

In [2]: b=test.callMethod

In [3]: def func():
   ...:     print "hi"
   ...:     
   ...:     

In [4]: b(func)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

$DIR/<ipython console> in <module>()

TypeError: in method 'callMethod', argument 1 of type 'void (*)()'

如何用Swig做我想做的事?

提前致谢!

1 个答案:

答案 0 :(得分:1)