SWIG链接器:未定义的符号:_ZN2cv8fastFreeEPv(cv :: fastFree(void *))

时间:2018-07-12 14:31:11

标签: python c++ opencv swig

更改代码后,让SWIG包装器在python中工作时遇到了一些麻烦。我怀疑这与链接器有关。

我的问题与this github有关: 该文件按照自述文件中的说明进行编译,可以在python中导入而不会出现问题。

我对需要OpenCV的代码进行了一些更改。在C ++中构建时,在修改CMakeLists文件以添加一些OpenCV库之后,也不会发生错误并且代码可以按预期运行。

进行了这些修改之后,我再次希望将其切换为python,但是出现了很多链接器错误。谷歌搜索后,我已经在SWIG setup.py中添加了以下参数,您可以找到here

extra_compile_args=['-L/usr/local/lib -lopencv_xfeatures2d -lopencv_features2d 
                     -lopencv_highgui -lopencv_core','-msse3'],

include_dirs=[numpy.get_include(), 
              '/usr/local/include/opencv',
              '/usr/local/include'])])

应按正确的顺序添加正确的包含/库文件夹以及使用的库(xfeatures,core,..)。其中一个文件的示例是:

gcc -pthread -B /home/floris/Software/Anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/floris/Software/Anaconda3/lib/python3.6/site-packages/numpy/core/include -I/usr/local/include/opencv -I/usr/local/include -I/home/floris/Software/Anaconda3/include/python3.6m -c filter.cpp -o build/temp.linux-x86_64-3.6/filter.o -L/usr/local/lib -lopencv_xfeatures2d -lopencv_features2d -lopencv_highgui -lopencv_core -msse3

通过发出

找到订单
pkg-config --cflags --libs opencv 

命令,并以相同顺序获取所需的库。

最后,SWIG命令不会产生任何错误。在python中加载假定生成的viso2模块时(将模块的位置添加到sys.path之后),会产生错误:

undefined symbol: _ZN2cv8fastFreeEPv

可以通过c ++ filt成为cv :: fastFree(void *)

运行时

ldd -r _viso2......linux.so 

显示大量未定义符号。

<floris@OPTIMUSPRIME:/magatank/DataSets/LibViso2_python/pyviso2_gitclone/src$ ldd -r _viso2.cpython-36m-x86_64-linux-gnu.so 
    linux-vdso.so.1 (0x00007ffdfd7bc000)
    libstdc++.so.6 =&gt; /home/floris/Software/Anaconda3/lib/libstdc++.so.6 (0x00007f4c7b3d0000)
    libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4c7b032000)
    libgcc_s.so.1 =&gt; /home/floris/Software/Anaconda3/lib/libgcc_s.so.1 (0x00007f4c7ae20000)
    libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4c7ac01000)
    libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4c7a810000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f4c7b9c1000)
undefined symbol: PyExc_ValueError  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyBool_Type   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_IndexError  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_SyntaxError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyCapsule_Type    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PySlice_Type  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _Py_NoneStruct    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_AttributeError  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_IOError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_MemoryError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyType_Type   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_OverflowError   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_ZeroDivisionError   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_NotImplementedError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_RuntimeError    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _Py_NotImplementedStruct  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_StopIteration   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_TypeError   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_GenericGetAttr   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyFloat_Type  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_SystemError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyExc_ImportError (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyTuple_SetItem   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyLong_AsLong (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv8fastFreeEPv    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_GetAttr  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyImport_AddModule    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Call (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Init (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyInstanceMethod_New  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Str  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_GetAttrString    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Restore (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyType_IsSubtype  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyModule_GetDict  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyModule_AddObject    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv7noArrayEv  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyTuple_New   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_SetAttr  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_IsInstance   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyEval_RestoreThread  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv11xfeatures2d4SIFT6createEiiddd (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyDict_SetItemString  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyUnicode_FromFormat  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyLong_AsDouble   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_SetObject   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _PyObject_New (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Format  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyFloat_FromDouble    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyFloat_AsDouble  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyUnicode_DecodeUTF8  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyImport_ImportModule (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyUnicode_FromString  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv6String10deallocateEv   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PySequence_Check  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv5errorEiRKNS_6StringEPKcS4_i    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Clear   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv6String8allocateEm  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: Py_DecRef (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyCapsule_Import  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyLong_FromVoidPtr    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_SetString   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyEval_InitThreads    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyCapsule_New (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyArg_ParseTuple  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv3MatC1ERKS0_RKNS_5Rect_IiEE (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyGILState_Release    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyCapsule_GetPointer  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Free (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyType_Ready  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Size (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyEval_SaveThread (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PySequence_GetItem    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PySequence_Size   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyLong_FromLong   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_WriteUnraisable (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_IsTrue   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyModule_Create2  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv3Mat20updateContinuityFlagEv    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Occurred    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Fetch   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_CallFunctionObjArgs  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PySlice_GetIndices    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyCallable_Check  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyLong_FromUnsignedLong   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyArg_UnpackTuple (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyGILState_Ensure (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyObject_Malloc   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyErr_Print   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyUnicode_Concat  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZNK2cv3Mat6copyToERKNS_12_OutputArrayE   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv3MatC1ERKS0_RKNS_5RangeES5_ (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: _ZN2cv3Mat10deallocateEv  (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyUnicode_AsUTF8String    (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyBytes_AsStringAndSize   (./_viso2.cpython-36m-x86_64-linux-gnu.so)
undefined symbol: PyBool_FromLong   (./_viso2.cpython-36m-x86_64-linux-gnu.so)

我添加的唯一功能是OpenCV功能。

感谢您的阅读。如果您有个主意,请告诉我。

干杯!

1 个答案:

答案 0 :(得分:0)

您必须使用参数librarieslibrary_dirs将库和库路径传递给构建过程,即:

ext = Extension(....
                libraries=["opencv_xfeatures2d", "opencv_features2d", 
                           "opencv_highgui", "opencv_core"],
                library_dirs=['/usr/local/lib']
                )

请注意,不需要-l-L-distutils会照顾它们。

为什么不将它们添加到额外的编译标志中起作用?

建筑扩展包括以下步骤:

  1. 构建第一个cpp文件,创建一个目标文件。使用编译器标志。
  2. 构建其他cpp文件(如果有的话)。使用编译器标志。
  3. 将所有目标文件链接在一起,需要库。使用链接器标志。

如您所见,在最后一步中需要使用库,但未提供它们,因为它们是编译器标志的一部分。另一方面,库是在编译过程中提供的,但在此阶段不需要。