SWIG 链接器:未定义符号:_ZN2cv8fastFreeEPv (cv::fastFree(void*))
Posted
技术标签:
【中文标题】SWIG 链接器:未定义符号:_ZN2cv8fastFreeEPv (cv::fastFree(void*))【英文标题】:SWIG Linker: undefined symbol: _ZN2cv8fastFreeEPv (cv::fastFree(void*)) 【发布时间】:2018-07-12 14:31:11 【问题描述】:在我更改了一些代码后,让我的 SWIG 包装器在 python 中工作时遇到了一些麻烦。我怀疑它与链接器有关。
我的问题涉及this github: 该文件按照自述文件中的说明进行编译,并且可以在 python 中毫无问题地导入。
我对需要 OpenCV 的代码进行了一些更改。在 C++ 中构建时,在修改 CMakeLists 文件以添加一些 OpenCV 库后,不会发生错误并且代码按预期运行。
在进行这些修改后,我再次想将其 SWIG 到 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 => /home/floris/Software/Anaconda3/lib/libstdc++.so.6 (0x00007f4c7b3d0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4c7b032000)
libgcc_s.so.1 => /home/floris/Software/Anaconda3/lib/libgcc_s.so.1 (0x00007f4c7ae20000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4c7ac01000)
libc.so.6 => /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 功能。
感谢阅读。如果您有任何想法,请告诉我。
干杯!
【问题讨论】:
那些需要作为链接器的参数,而不是编译器。我不确定extra_compile_args
是否已传递给链接器,但您应该检查一下。
@JonathonReinhart 我已经编辑了这个问题,因此它包含了一个文件“filter.cpp”的示例
【参考方案1】:
您必须使用参数libraries
和library_dirs
将库和库路径传递给构建过程,即:
ext = Extension(....
libraries=["opencv_xfeatures2d", "opencv_features2d",
"opencv_highgui", "opencv_core"],
library_dirs=['/usr/local/lib']
)
请注意,-l
和 -L
不是必需的 - 由 distutils 处理。
为什么将它们添加到额外的编译标志不起作用?
构建扩展包括以下步骤:
-
构建第一个 cpp 文件,创建一个目标文件。使用编译器标志。
进一步构建 cpp 文件(如果有的话)。使用编译器标志。
将所有目标文件链接在一起,需要库。使用 linker 标志。
如您所见,最后一步需要库,但没有提供它们,因为它们是编译器标志的一部分。另一方面,这些库是在编译期间提供的,但在此阶段不需要。
【讨论】:
非常明确的答案,谢谢。在进一步查看the distutils.core.Extension 之后,我还发现了一个例子。在这与 glib 相关之后,由于 Ubuntu 18.04,我遇到了一个小问题,但这很容易通过执行 conda 更新来解决,如 here以上是关于SWIG 链接器:未定义符号:_ZN2cv8fastFreeEPv (cv::fastFree(void*))的主要内容,如果未能解决你的问题,请参考以下文章
gcc链接错误:未定义对符号'_ZN2cv5flann12SearchParamsC1Eifb'的引用,[重复]
ImportError:使用 SWIG 时 _...so 文件中的未定义符号
Apple Mach-O 链接器错误 - 架构 x86_64 的未定义符号:“_UISceneWillEnterForegroundNotification”和“___isPlatformVersio