- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我更改了一些代码后,我在让 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、核心、..)。其中一个文件的示例是:
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
出现了一大堆 undefined symbol 。
<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 功能。
感谢阅读。如果您有任何想法,请告诉我。
干杯!
最佳答案
您必须使用参数 libraries
和 library_dirs
将库和库路径传递给构建过程,即:
ext = Extension(....
libraries=["opencv_xfeatures2d", "opencv_features2d",
"opencv_highgui", "opencv_core"],
library_dirs=['/usr/local/lib']
)
请注意,-l
和 -L
不是必需的 - 它们由 distutils 处理。
为什么将它们添加到额外的编译标志中不起作用?
构建扩展包括以下步骤:
如您所见,最后一步需要库,但没有提供,因为它们是编译器标志的一部分。另一方面,库是在编译期间提供的,但在这个阶段不需要。
关于python - SWIG 链接器: undefined symbol :_ZN2cv8fastFreeEPv (cv::fastFree(void*)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308292/
我是一名优秀的程序员,十分优秀!