簡體   English   中英

使用Swig在C ++中調用Python函數

[英]Calling a Python function in C++ with 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做我想做的事?

提前致謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM