无法为 c++ python 扩展编译 swig 生成的包装器

Posted

技术标签:

【中文标题】无法为 c++ python 扩展编译 swig 生成的包装器【英文标题】:Unable to compile swig generated wrapper for c++ python extension 【发布时间】:2015-11-05 19:17:41 【问题描述】:

您好,我正在尝试 swig python 扩展,在 c++ 中,例如 - on an Mit page

我的标题是 -shapes.h

#include <iostream>

class Shape

    public:
    Shape()
    
        nshapes++;
    
    ~Shape()
    
        nshapes--;
    
    double  x, y;   
    void    move(double dx, double dy);
    virtual double area() = 0;  
    virtual double perimeter() = 0;
    static  int nshapes;
;

class Circle : public Shape 
private:
  double radius;
public:
  Circle(double r) : radius(r)  ;
  virtual double area();
  virtual double perimeter();
;

class Square : public Shape 
private:
  double width;
public:
  Square(double w) : width(w)  ;
  virtual double area();
  virtual double perimeter();
;

对应的接口文件是-shapesModule.i

%module shapes

%
#include "shapes.h"
%

/* Let's just grab the original header file here */
%include "shapes.h"

我第一次运行 swig 命令 - swig -c++ -python shapesModule.i

这生成了以下 2 个文件 - shapeModule_wrap.cxx 形状.py

然后我运行 g++ 编译器 - g++ shapes.h shapesModule_wrap.cxx -I/usr/include/python2.7

但是我得到了很多我无法理解的错误 -

/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccfPRfYo.o: In function `SWIG_Python_str_AsChar(_object*)':
shapesModule_wrap.cxx:(.text+0x8ad): undefined reference to `PyString_AsString'
/tmp/ccfPRfYo.o: In function `SWIG_Python_str_FromChar(char const*)':
shapesModule_wrap.cxx:(.text+0x8c0): undefined reference to `PyString_FromString'
/tmp/ccfPRfYo.o: In function `SWIG_Python_ErrorType(int)':
shapesModule_wrap.cxx:(.text+0x8e8): undefined reference to `PyExc_MemoryError'
shapesModule_wrap.cxx:(.text+0x8f2): undefined reference to `PyExc_IOError'
shapesModule_wrap.cxx:(.text+0x8fc): undefined reference to `PyExc_RuntimeError'
shapesModule_wrap.cxx:(.text+0x906): undefined reference to `PyExc_IndexError'
shapesModule_wrap.cxx:(.text+0x910): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0x91a): undefined reference to `PyExc_ZeroDivisionError'
shapesModule_wrap.cxx:(.text+0x924): undefined reference to `PyExc_OverflowError'
shapesModule_wrap.cxx:(.text+0x92e): undefined reference to `PyExc_SyntaxError'
shapesModule_wrap.cxx:(.text+0x938): undefined reference to `PyExc_ValueError'
shapesModule_wrap.cxx:(.text+0x942): undefined reference to `PyExc_SystemError'
shapesModule_wrap.cxx:(.text+0x94c): undefined reference to `PyExc_AttributeError'
shapesModule_wrap.cxx:(.text+0x956): undefined reference to `PyExc_RuntimeError'
/tmp/ccfPRfYo.o: In function `SWIG_Python_AddErrorMsg(char const*)':
shapesModule_wrap.cxx:(.text+0x97e): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x9a0): undefined reference to `PyErr_Fetch'
shapesModule_wrap.cxx:(.text+0x9b6): undefined reference to `PyObject_Str'
shapesModule_wrap.cxx:(.text+0x9be): undefined reference to `PyErr_Clear'
shapesModule_wrap.cxx:(.text+0x9fe): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0xa59): undefined reference to `PyExc_RuntimeError'
shapesModule_wrap.cxx:(.text+0xa68): undefined reference to `PyErr_SetString'
/tmp/ccfPRfYo.o: In function `SWIG_Python_SetErrorObj(_object*, _object*)':
shapesModule_wrap.cxx:(.text+0xa8c): undefined reference to `PyErr_SetObject'
/tmp/ccfPRfYo.o: In function `SWIG_Python_SetErrorMsg(_object*, char const*)':
shapesModule_wrap.cxx:(.text+0xad2): undefined reference to `PyErr_SetString'
/tmp/ccfPRfYo.o: In function `SWIG_Python_SetConstant(_object*, char const*, _object*)':
shapesModule_wrap.cxx:(.text+0xaf3): undefined reference to `PyDict_SetItemString'
/tmp/ccfPRfYo.o: In function `SWIG_Python_AppendOutput(_object*, _object*)':
shapesModule_wrap.cxx:(.text+0xb3f): undefined reference to `_Py_NoneStruct'
shapesModule_wrap.cxx:(.text+0xb99): undefined reference to `PyList_New'
shapesModule_wrap.cxx:(.text+0xbb6): undefined reference to `PyList_SetItem'
shapesModule_wrap.cxx:(.text+0xbc8): undefined reference to `PyList_Append'
/tmp/ccfPRfYo.o: In function `SWIG_Python_UnpackTuple(_object*, char const*, int, int, _object**)':
shapesModule_wrap.cxx:(.text+0xc37): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0xc59): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0xcbb): undefined reference to `PyExc_SystemError'
shapesModule_wrap.cxx:(.text+0xccb): undefined reference to `PyErr_SetString'
shapesModule_wrap.cxx:(.text+0xcfa): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0xd20): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0xd49): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0xd6f): undefined reference to `PyErr_Format'
/tmp/ccfPRfYo.o: In function `SWIG_Py_Void':
shapesModule_wrap.cxx:(.text+0xdd0): undefined reference to `_Py_NoneStruct'
/tmp/ccfPRfYo.o: In function `SwigPyClientData_New':
shapesModule_wrap.cxx:(.text+0xe26): undefined reference to `PyClass_Type'
shapesModule_wrap.cxx:(.text+0xe5f): undefined reference to `PyObject_GetAttrString'
shapesModule_wrap.cxx:(.text+0xe88): undefined reference to `PyTuple_New'
shapesModule_wrap.cxx:(.text+0xeab): undefined reference to `PyTuple_SetItem'
shapesModule_wrap.cxx:(.text+0xed8): undefined reference to `PyObject_GetAttrString'
shapesModule_wrap.cxx:(.text+0xee3): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0xef1): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SwigPyObject_long':
shapesModule_wrap.cxx:(.text+0x1021): undefined reference to `PyLong_FromVoidPtr'
/tmp/ccfPRfYo.o: In function `SwigPyObject_format':
shapesModule_wrap.cxx:(.text+0x103c): undefined reference to `PyTuple_New'
shapesModule_wrap.cxx:(.text+0x106b): undefined reference to `PyTuple_SetItem'
shapesModule_wrap.cxx:(.text+0x109e): undefined reference to `PyString_Format'
/tmp/ccfPRfYo.o: In function `SwigPyObject_repr':
shapesModule_wrap.cxx:(.text+0x1174): undefined reference to `PyString_FromFormat'
shapesModule_wrap.cxx:(.text+0x11a4): undefined reference to `PyString_ConcatAndDel'
/tmp/ccfPRfYo.o: In function `SwigPyObject_richcompare':
shapesModule_wrap.cxx:(.text+0x11fd): undefined reference to `_Py_NotImplementedStruct'
shapesModule_wrap.cxx:(.text+0x1205): undefined reference to `_Py_NotImplementedStruct'
shapesModule_wrap.cxx:(.text+0x120a): undefined reference to `_Py_NotImplementedStruct'
shapesModule_wrap.cxx:(.text+0x1244): undefined reference to `PyBool_FromLong'
/tmp/ccfPRfYo.o: In function `SwigPyObject_dealloc':
shapesModule_wrap.cxx:(.text+0x13ab): undefined reference to `PyObject_CallFunctionObjArgs'
shapesModule_wrap.cxx:(.text+0x14ab): undefined reference to `PyObject_Free'
/tmp/ccfPRfYo.o: In function `SwigPyObject_own':
shapesModule_wrap.cxx:(.text+0x1594): undefined reference to `PyArg_UnpackTuple'
shapesModule_wrap.cxx:(.text+0x15b8): undefined reference to `PyBool_FromLong'
shapesModule_wrap.cxx:(.text+0x15cd): undefined reference to `PyObject_IsTrue'
/tmp/ccfPRfYo.o: In function `SwigPyObject_TypeOnce':
shapesModule_wrap.cxx:(.text+0x164e): undefined reference to `PyType_Ready'
/tmp/ccfPRfYo.o: In function `SwigPyObject_New':
shapesModule_wrap.cxx:(.text+0x168a): undefined reference to `PyObject_Malloc'
shapesModule_wrap.cxx:(.text+0x1696): undefined reference to `PyObject_Init'
/tmp/ccfPRfYo.o: In function `SwigPyPacked_repr':
shapesModule_wrap.cxx:(.text+0x1866): undefined reference to `PyString_FromFormat'
shapesModule_wrap.cxx:(.text+0x1883): undefined reference to `PyString_FromFormat'
/tmp/ccfPRfYo.o: In function `SwigPyPacked_str':
shapesModule_wrap.cxx:(.text+0x1919): undefined reference to `PyString_FromFormat'
/tmp/ccfPRfYo.o: In function `SwigPyPacked_dealloc':
shapesModule_wrap.cxx:(.text+0x1aa2): undefined reference to `PyObject_Free'
/tmp/ccfPRfYo.o: In function `SwigPyPacked_TypeOnce':
shapesModule_wrap.cxx:(.text+0x1b2d): undefined reference to `PyObject_GenericGetAttr'
shapesModule_wrap.cxx:(.text+0x1b6f): undefined reference to `PyType_Ready'
/tmp/ccfPRfYo.o: In function `SwigPyPacked_New':
shapesModule_wrap.cxx:(.text+0x1bab): undefined reference to `PyObject_Malloc'
shapesModule_wrap.cxx:(.text+0x1bb7): undefined reference to `PyObject_Init'
shapesModule_wrap.cxx:(.text+0x1c15): undefined reference to `PyObject_Free'
/tmp/ccfPRfYo.o: In function `SWIG_Python_GetSwigThis':
shapesModule_wrap.cxx:(.text+0x1cea): undefined reference to `PyInstance_Type'
shapesModule_wrap.cxx:(.text+0x1d00): undefined reference to `_PyInstance_Lookup'
shapesModule_wrap.cxx:(.text+0x1d13): undefined reference to `_PyObject_GetDictPtr'
shapesModule_wrap.cxx:(.text+0x1d3e): undefined reference to `PyDict_GetItem'
shapesModule_wrap.cxx:(.text+0x1d58): undefined reference to `_PyWeakref_ProxyType'
shapesModule_wrap.cxx:(.text+0x1d65): undefined reference to `_PyWeakref_CallableProxyType'
shapesModule_wrap.cxx:(.text+0x1d80): undefined reference to `_Py_NoneStruct'
shapesModule_wrap.cxx:(.text+0x1db4): undefined reference to `PyObject_GetAttr'
shapesModule_wrap.cxx:(.text+0x1df0): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x1dfe): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_Python_ConvertPtrAndOwn':
shapesModule_wrap.cxx:(.text+0x1ea8): undefined reference to `_Py_NoneStruct'
shapesModule_wrap.cxx:(.text+0x208e): undefined reference to `PyObject_CallFunctionObjArgs'
shapesModule_wrap.cxx:(.text+0x20a0): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x20ae): undefined reference to `PyErr_Clear'
shapesModule_wrap.cxx:(.text+0x216c): undefined reference to `_Py_NoneStruct'
shapesModule_wrap.cxx:(.text+0x2182): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2190): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_Python_ConvertFunctionPtr':
shapesModule_wrap.cxx:(.text+0x21ad): undefined reference to `PyCFunction_Type'
/tmp/ccfPRfYo.o: In function `SWIG_Python_NewShadowInstance':
shapesModule_wrap.cxx:(.text+0x2381): undefined reference to `PyObject_Call'
shapesModule_wrap.cxx:(.text+0x2395): undefined reference to `_PyObject_GetDictPtr'
shapesModule_wrap.cxx:(.text+0x23b1): undefined reference to `PyDict_New'
shapesModule_wrap.cxx:(.text+0x23d7): undefined reference to `PyDict_SetItem'
shapesModule_wrap.cxx:(.text+0x23de): undefined reference to `PyDict_New'
shapesModule_wrap.cxx:(.text+0x2402): undefined reference to `PyDict_SetItem'
shapesModule_wrap.cxx:(.text+0x2417): undefined reference to `PyInstance_NewRaw'
/tmp/ccfPRfYo.o: In function `SWIG_Python_SetSwigThis':
shapesModule_wrap.cxx:(.text+0x245c): undefined reference to `_PyObject_GetDictPtr'
shapesModule_wrap.cxx:(.text+0x2478): undefined reference to `PyDict_New'
shapesModule_wrap.cxx:(.text+0x249e): undefined reference to `PyDict_SetItem'
shapesModule_wrap.cxx:(.text+0x24b3): undefined reference to `PyObject_GetAttrString'
shapesModule_wrap.cxx:(.text+0x24d1): undefined reference to `PyDict_SetItem'
/tmp/ccfPRfYo.o: In function `SWIG_Python_NewPointerObj':
shapesModule_wrap.cxx:(.text+0x263d): undefined reference to `_PyObject_New'
/tmp/ccfPRfYo.o: In function `SWIG_Python_GetModule':
shapesModule_wrap.cxx:(.text+0x277a): undefined reference to `PyCapsule_Import'
shapesModule_wrap.cxx:(.text+0x2784): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2792): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_Python_DestroyModule':
shapesModule_wrap.cxx:(.text+0x27bd): undefined reference to `PyCapsule_GetPointer'
/tmp/ccfPRfYo.o: In function `SWIG_Python_SetModule':
shapesModule_wrap.cxx:(.text+0x2886): undefined reference to `Py_InitModule4'
shapesModule_wrap.cxx:(.text+0x28a4): undefined reference to `PyCapsule_New'
shapesModule_wrap.cxx:(.text+0x28cd): undefined reference to `PyModule_AddObject'
/tmp/ccfPRfYo.o: In function `SWIG_Python_TypeCache':
shapesModule_wrap.cxx:(.text+0x2936): undefined reference to `PyDict_New'
/tmp/ccfPRfYo.o: In function `SWIG_Python_TypeQuery':
shapesModule_wrap.cxx:(.text+0x29a1): undefined reference to `PyDict_GetItem'
shapesModule_wrap.cxx:(.text+0x29bd): undefined reference to `PyCapsule_GetPointer'
shapesModule_wrap.cxx:(.text+0x2a11): undefined reference to `PyCapsule_New'
shapesModule_wrap.cxx:(.text+0x2a2d): undefined reference to `PyDict_SetItem'
/tmp/ccfPRfYo.o: In function `SWIG_Python_AddErrMesg':
shapesModule_wrap.cxx:(.text+0x2a95): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2ad0): undefined reference to `PyErr_Fetch'
shapesModule_wrap.cxx:(.text+0x2ae6): undefined reference to `PyObject_Str'
shapesModule_wrap.cxx:(.text+0x2aff): undefined reference to `PyErr_Clear'
shapesModule_wrap.cxx:(.text+0x2b34): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0x2b65): undefined reference to `PyErr_Format'
/tmp/ccfPRfYo.o: In function `SWIG_Python_ArgFail':
shapesModule_wrap.cxx:(.text+0x2bba): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2be8): undefined reference to `PyOS_snprintf'
/tmp/ccfPRfYo.o: In function `SWIG_Python_TypeError':
shapesModule_wrap.cxx:(.text+0x2c56): undefined reference to `PyObject_Str'
shapesModule_wrap.cxx:(.text+0x2c7f): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0x2ca4): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0x2cab): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0x2cc9): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0x2d04): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0x2d1b): undefined reference to `PyErr_Format'
shapesModule_wrap.cxx:(.text+0x2d22): undefined reference to `PyExc_TypeError'
shapesModule_wrap.cxx:(.text+0x2d32): undefined reference to `PyErr_Format'
/tmp/ccfPRfYo.o: In function `SWIG_Python_MustGetPtr':
shapesModule_wrap.cxx:(.text+0x2d71): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_AsVal_double(_object*, double*)':
shapesModule_wrap.cxx:(.text+0x2d8e): undefined reference to `PyFloat_Type'
shapesModule_wrap.cxx:(.text+0x2d9e): undefined reference to `PyFloat_Type'
shapesModule_wrap.cxx:(.text+0x2da6): undefined reference to `PyType_IsSubtype'
shapesModule_wrap.cxx:(.text+0x2dcb): undefined reference to `PyFloat_AsDouble'
shapesModule_wrap.cxx:(.text+0x2dfd): undefined reference to `PyInt_AsLong'
shapesModule_wrap.cxx:(.text+0x2e2c): undefined reference to `PyLong_AsDouble'
shapesModule_wrap.cxx:(.text+0x2e34): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2e57): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_AsVal_long(_object*, long*)':
shapesModule_wrap.cxx:(.text+0x2e85): undefined reference to `PyInt_AsLong'
shapesModule_wrap.cxx:(.text+0x2eae): undefined reference to `PyLong_AsLong'
shapesModule_wrap.cxx:(.text+0x2eb6): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x2ed9): undefined reference to `PyErr_Clear'
/tmp/ccfPRfYo.o: In function `SWIG_From_int(int)':
shapesModule_wrap.cxx:(.text+0x2f25): undefined reference to `PyInt_FromLong'
/tmp/ccfPRfYo.o: In function `_wrap_delete_Shape':
shapesModule_wrap.cxx:(.text+0x2f6b): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_x_set':
shapesModule_wrap.cxx:(.text+0x3065): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_x_get':
shapesModule_wrap.cxx:(.text+0x3189): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3210): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_y_set':
shapesModule_wrap.cxx:(.text+0x3277): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_y_get':
shapesModule_wrap.cxx:(.text+0x339b): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3422): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_move':
shapesModule_wrap.cxx:(.text+0x349e): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x35c3): undefined reference to `Shape::move(double, double)'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_area':
shapesModule_wrap.cxx:(.text+0x361a): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x36aa): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_perimeter':
shapesModule_wrap.cxx:(.text+0x36fc): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x378f): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `Swig_var_Shape_nshapes_set':
shapesModule_wrap.cxx:(.text+0x37f6): undefined reference to `Shape::nshapes'
/tmp/ccfPRfYo.o: In function `Swig_var_Shape_nshapes_get':
shapesModule_wrap.cxx:(.text+0x380f): undefined reference to `Shape::nshapes'
/tmp/ccfPRfYo.o: In function `_wrap_Shape_nshapes_set':
shapesModule_wrap.cxx:(.text+0x384c): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `Shape_swigregister':
shapesModule_wrap.cxx:(.text+0x389e): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_new_Circle':
shapesModule_wrap.cxx:(.text+0x390f): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_Circle_area':
shapesModule_wrap.cxx:(.text+0x3a02): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3a92): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_Circle_perimeter':
shapesModule_wrap.cxx:(.text+0x3ae4): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3b77): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_delete_Circle':
shapesModule_wrap.cxx:(.text+0x3bca): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `Circle_swigregister':
shapesModule_wrap.cxx:(.text+0x3c8c): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_new_Square':
shapesModule_wrap.cxx:(.text+0x3cfd): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `_wrap_Square_area':
shapesModule_wrap.cxx:(.text+0x3df0): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3e80): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_Square_perimeter':
shapesModule_wrap.cxx:(.text+0x3ed2): undefined reference to `PyArg_ParseTuple'
shapesModule_wrap.cxx:(.text+0x3f65): undefined reference to `PyFloat_FromDouble'
/tmp/ccfPRfYo.o: In function `_wrap_delete_Square':
shapesModule_wrap.cxx:(.text+0x3fb8): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `Square_swigregister':
shapesModule_wrap.cxx:(.text+0x407a): undefined reference to `PyArg_ParseTuple'
/tmp/ccfPRfYo.o: In function `swig_varlink_repr':
shapesModule_wrap.cxx:(.text+0x43f8): undefined reference to `PyString_FromString'
/tmp/ccfPRfYo.o: In function `swig_varlink_str':
shapesModule_wrap.cxx:(.text+0x440c): undefined reference to `PyString_FromString'
shapesModule_wrap.cxx:(.text+0x4427): undefined reference to `PyString_FromString'
shapesModule_wrap.cxx:(.text+0x4436): undefined reference to `PyString_ConcatAndDel'
shapesModule_wrap.cxx:(.text+0x444c): undefined reference to `PyString_FromString'
shapesModule_wrap.cxx:(.text+0x445b): undefined reference to `PyString_ConcatAndDel'
shapesModule_wrap.cxx:(.text+0x4476): undefined reference to `PyString_FromString'
shapesModule_wrap.cxx:(.text+0x4485): undefined reference to `PyString_ConcatAndDel'
/tmp/ccfPRfYo.o: In function `swig_varlink_getattr':
shapesModule_wrap.cxx:(.text+0x45b3): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x45cc): undefined reference to `PyExc_NameError'
shapesModule_wrap.cxx:(.text+0x45dc): undefined reference to `PyErr_SetString'
/tmp/ccfPRfYo.o: In function `swig_varlink_setattr':
shapesModule_wrap.cxx:(.text+0x463e): undefined reference to `PyErr_Occurred'
shapesModule_wrap.cxx:(.text+0x4657): undefined reference to `PyExc_NameError'
shapesModule_wrap.cxx:(.text+0x4667): undefined reference to `PyErr_SetString'
/tmp/ccfPRfYo.o: In function `swig_varlink_type':
shapesModule_wrap.cxx:(.text+0x472d): undefined reference to `PyType_Ready'
/tmp/ccfPRfYo.o: In function `SWIG_Python_newvarlink':
shapesModule_wrap.cxx:(.text+0x4769): undefined reference to `PyObject_Malloc'
shapesModule_wrap.cxx:(.text+0x4775): undefined reference to `PyObject_Init'
/tmp/ccfPRfYo.o: In function `SWIG_Python_InstallConstants':
shapesModule_wrap.cxx:(.text+0x496b): undefined reference to `PyDict_SetItemString'
/tmp/ccfPRfYo.o: In function `init_shapes':
shapesModule_wrap.cxx:(.text+0x4c5c): undefined reference to `Py_InitModule4'
shapesModule_wrap.cxx:(.text+0x4c6a): undefined reference to `PyModule_GetDict'
shapesModule_wrap.cxx:(.text+0x4cae): undefined reference to `PyDict_SetItemString'
/tmp/ccfPRfYo.o:(.rodata+0x2a8): undefined reference to `PyObject_GenericGetAttr'
/tmp/ccfPRfYo.o: In function `Shape::Shape()':
shapesModule_wrap.cxx:(.text._ZN5ShapeC2Ev[_ZN5ShapeC5Ev]+0xd): undefined reference to `Shape::nshapes'
shapesModule_wrap.cxx:(.text._ZN5ShapeC2Ev[_ZN5ShapeC5Ev]+0x15): undefined reference to `Shape::nshapes'
/tmp/ccfPRfYo.o: In function `Shape::~Shape()':
shapesModule_wrap.cxx:(.text._ZN5ShapeD2Ev[_ZN5ShapeD5Ev]+0xd): undefined reference to `Shape::nshapes'
shapesModule_wrap.cxx:(.text._ZN5ShapeD2Ev[_ZN5ShapeD5Ev]+0x15): undefined reference to `Shape::nshapes'
/tmp/ccfPRfYo.o: In function `Circle::Circle(double)':
shapesModule_wrap.cxx:(.text._ZN6CircleC2Ed[_ZN6CircleC5Ed]+0x22): undefined reference to `vtable for Circle'
/tmp/ccfPRfYo.o: In function `Square::Square(double)':
shapesModule_wrap.cxx:(.text._ZN6SquareC2Ed[_ZN6SquareC5Ed]+0x22): undefined reference to `vtable for Square'
/tmp/ccfPRfYo.o: In function `Circle::~Circle()':
shapesModule_wrap.cxx:(.text._ZN6CircleD2Ev[_ZN6CircleD5Ev]+0xb): undefined reference to `vtable for Circle'
/tmp/ccfPRfYo.o: In function `Square::~Square()':
shapesModule_wrap.cxx:(.text._ZN6SquareD2Ev[_ZN6SquareD5Ev]+0xb): undefined reference to `vtable for Square'
collect2: error: ld returned 1 exit status

我似乎无法理解问题所在。我是swig的新手,所以请提供一些指示。另外,任何人都知道任何工作示例以及分步教程,也将不胜感激。

非常感谢

更新 - 来自@m7thon 的响应修复了编译错误。我现在在将模块导入 python 时遇到错误 -

Firstly, thanks for the response. that fixed my errors. :)

我在导入时收到以下错误 -

mishra14@mishra14-VirtualBox:~/sdn/cpp_class$ swig -c++ -python shapesModule.i 
mishra14@mishra14-VirtualBox:~/sdn/cpp_class$ g++ -shared shapesModule_wrap.cxx $(python-config --includes) $(python-config --libs) -o _shapes.so
mishra14@mishra14-VirtualBox:~/sdn/cpp_class$ ls
shapes.h   Shapes.h~     shapesModule.i   shapesModule_wrap.cxx  _shapes.so  test.cpp~
shapes.h~  shapes.h.gch  ShapesModule.i~  shapes.py              test.cpp
mishra14@mishra14-VirtualBox:~/sdn/cpp_class$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shapes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shapes.py", line 28, in <module>
    _shapes = swig_import_helper()
  File "shapes.py", line 24, in swig_import_helper
    _mod = imp.load_module('_shapes', fp, pathname, description)
ImportError: ./_shapes.so: undefined symbol: _ZN5Shape4moveEdd
>>> quit()

我在 _shapes.so 文件上运行了一个 nm,我得到以下符号 -

         U _ZN5Shape4moveEdd

所以我认为它是未定义的。找到了这个 - here 但仍然不确定如何解决这个问题。

【问题讨论】:

我试过了。我仍然得到同样的错误。我认为这是一个链接错误。但我不明白为什么。 我的 makefile 出了点问题。忽略之前的建议。 【参考方案1】:

你显然包含了 python 头文件,但没有链接到 python 库,所以你得到了未解析的符号。

你可以跑

python-config --cflags
python-config --ldflags

为您的系统找到所需的 python 包含目录和库,并将输出添加到您的编译行。分离编译和链接,应该可以使用:

g++ -fPIC -c shapes.cpp
g++ -fPIC -c shapesModule_wrap.cxx $(python-config --cflags)
g++ -shared shapes.o shapesModule_wrap.o $(python-config --ldflags) -o _shapes.so

注意:

您不编译诸如shapes.h 之类的头文件。相反,您需要编译示例中似乎缺少的相应实现文件shapes.cpp。 您的库文件必须命名为_shapes.so 使用distutils(即编写 setup.py 文件)可能更简单

【讨论】:

感谢您的回复。这修复了错误。但是在 python 中导入 .so 文件时出现错误。我已经编辑了带有错误的原始帖子。如果可以的话,请看一下。 我的反应有点快。现已编辑。你的例子不完整(缺少shapes.cpp),所以我自己无法测试。 我想通了。我使用了来自dabeaz.com/SwigMaster 的示例并使用 distutils 创建了一个编译器

以上是关于无法为 c++ python 扩展编译 swig 生成的包装器的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 SWIG 在 Python 中实例化 C++ 类(获取属性错误)

SWIG 将 C++ 扩展到 Python,未定义的枚举

Openshift 无法加载 c++ bson 扩展/找不到模块('swig')

C++ 优化级别会影响 Swig Python 模块性能吗

SWIG 不扩展嵌套宏“错误:输入中的语法错误 (3)”

SWIG:将其参数从 c++ 修改为 python 的函数