python - 使用 swig 继承 c++ 时出错

标签 python c++ swig

我有一个由 swig 包装并由 python 使用的 c++ 代码。 我想创建一种在 C++ 代码中运行回调 Python 代码的方法。 所以我创建了一个“抽象”类(它还不是抽象的),我在 .i 文件中用 C++ 对其进行分类。

这是我写的一些代码片段

.h文件

class Listener {
public:
    Listener();
    virtual void register_new();
    virtual void run(Msg &m);
    virtual ~Listener();
};

.i 文件

%feature("director") PyListener;
using namespace std;
%{
#include "../core/listener.h"
%}

%include "../core/listener.h"

class PyListener : public Listener {
    PyObject * function;
public:
    PyListener() {};
    PyListener(PyObject * func) : function(func) {};
    void register_new() {Py_XINCREF(function);};
    ~PyListener () {Py_XDECREF(function);};
     void run(Msg &m) {
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();
        PyObject *pValue = PyCObject_FromVoidPtr((void*)&m,NULL);
        PyObject *pArgs = PyTuple_New(1);
        PyTuple_SetItem(pArgs, 0, pValue);
        PyObject_CallObject(function, pArgs);
        PyGILState_Release(gstate);
    };
};

但是在编译过程中得到

/bin/sh ../../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I../.. -I. -I./../core -I./../../../include -I/opt/regression/usr/include/ -I./../../include_dep/ -I/usr/include/python2.6  -fPIC    -g -O2 -MT sim_wrap.lo -MD -MP -MF .deps/sim_wrap.Tpo -c -o sim_wrap.lo sim_wrap.cxx
libtool: compile:  g++ -DHAVE_CONFIG_H -I. -I../.. -I. -I./../core -I./../../../include -I/opt/regression/usr/include/ -I./../../include_dep/ -I/usr/include/python2.6 -fPIC -g -O2 -MT sim_wrap.lo -MD -MP -MF .deps/sim_wrap.Tpo -c sim_wrap.cxx  -fPIC -DPIC -o .libs/sim_wrap.o
sim_wrap.cxx: In function ‘PyObject* _wrap_new_PyListener__SWIG_0(PyObject*, PyObject*)’:
sim_wrap.cxx:36391: error: ‘PyListener’ was not declared in this scope
sim_wrap.cxx:36391: error: ‘result’ was not declared in this scope
sim_wrap.cxx:36394: error: expected primary-expression before ‘)’ token
sim_wrap.cxx:36394: error: expected ‘;’ before ‘new’
...

最佳答案

由于您同时声明和定义了一个新类,因此您需要将其包含在 SWIG 接口(interface)内的 %inline 中,例如

%inline %{
    class PyListener : public Listener {
        PyObject * function;
    public:
        PyListener() {};
        PyListener(PyObject * func) : function(func) {};
        void register_new() {Py_XINCREF(function);};
        ~PyListener () {Py_XDECREF(function);};
         void run(Msg &m) {
            PyGILState_STATE gstate;
            gstate = PyGILState_Ensure();
            PyObject *pValue = PyCObject_FromVoidPtr((void*)&m,NULL);
            PyObject *pArgs = PyTuple_New(1);
            PyTuple_SetItem(pArgs, 0, pValue);
            PyObject_CallObject(function, pArgs);
            PyGILState_Release(gstate);
        };
    };
%}

这会导致 SWIG 不仅包装类型,还将声明/定义传递给生成的 C++ 代码。这是必需的,因为 PyListener 特定于您的 SWIG 接口(interface),因此在您的任何其他头文件中都不知道。

关于python - 使用 swig 继承 c++ 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323808/

相关文章:

python - SWIG/python - C 中定义的常量不可访问

python - 在无法访问库源的情况下将 swig c++ 用于 python

python - 如何从Apache Pig的part-r-0000获取输出

python - Celery Worker 中的多线程

c++ - VC++错误LNK2005已经定义在

c++ - 内部类模板的非成员运算符重载

java - swig java 中的简单类型映射示例

python - 将 numpy 数组中的连续值及其长度分组

python - ubuntu 升级到 17.10(从 17.04 开始)时出现 psycopg2 导入错误

c++ - 如何与 libtiff 链接