Calling a Python function in C++ with Swig

南笙酒味 提交于 2019-12-10 16:38:54

问题


Here is my c++ code:

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

My Swig .i file is:

%module test
%{
#define SWIG_FILE_WITH_INIT

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

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


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

Here is my error:

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 (*)()'

How can I do what I want with Swig?

Thanks in advance!


回答1:


Pointers to functions and callbacks



来源:https://stackoverflow.com/questions/3302146/calling-a-python-function-in-c-with-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!