问题描述
我在C ++中使用OpenCV 2.3到2.4.2在Ubuntu 11.04上开发了一个Python模块。 OpenCV是从源代码构建的。我不是从Ubuntu仓库使用OpenCV版本。
I've been developing a Python module in C++ using OpenCV 2.3 through 2.4.2, on Ubuntu 11.04. OpenCV was built from source. I'm not using the version of OpenCV from the Ubuntu repositories.
我的Python模块编译没有问题,并正确加载在Python。但是,当我在Ubuntu 11.10或12.04上编译这个模块时,当尝试使用Python加载它时,我得到一个带有未定义符号的ImportError。
My Python module compiles with no issues and is loaded in Python properly. However, when I compile this module on Ubuntu 11.10 or 12.04, I get an ImportError with the message "undefined symbol" when trying to load it in Python.
我编译模块:
g++ -fPIC -shared `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy -o mymodule.so mymodule.cpp
这是pkg-config --cflags --libs opencv的输出
This is the output of "pkg-config --cflags --libs opencv"
-I/usr/local/include/opencv -I/usr/local/include /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so
我得到的错误是:
ImportError: /path/to/service/mymodule.so: undefined symbol: _ZN5CvSVMD1Ev
我的理解是未定义符号意味着在任何链接的文库中找不到给定的符号。但是我知道这个符号在libopencv_ml.so中,因为当我运行这个:
My understanding is that "undefined symbol" generally means that the given symbol can't be found in any of the linked libraries. But I know that this symbol is there in libopencv_ml.so because when I run this:
$ nm -g /usr/local/lib/libopencv_ml.so | grep _ZN5CvSVMD1Ev
我得到:
000000000002fd40 T _ZN5CvSVMD1Ev
/ usr / local / lib似乎在动态链接器缓存。
/usr/local/lib seems to be in the dynamic linker cache.
$ cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib
p>
And the so file is there in the cache too.
$ ldconfig -p | grep opencv | grep ml
libopencv_ml.so.2.4 (libc6,x86-64) => /usr/local/lib/libopencv_ml.so.2.4
libopencv_ml.so (libc6,x86-64) => /usr/local/lib/libopencv_ml.so
你可以给我任何线索做错了?在Ubuntu 11.04和11.10之间有什么变化,在运行Python时加载共享库的方式?或者这是OpenCV的问题?
So can you give me any clue what I might be doing wrong? Has something changed between Ubuntu 11.04 and 11.10 in the manner in which shared libraries are loaded when running Python? Or is this a problem with OpenCV?
推荐答案
解决方案是将生成的模块名称放在其他模块之前,在g ++命令行上。
The solution is to put the generated module name before the other modules it depends on, on the g++ command-line.
g++ -fPIC -shared -o mymodule.so mymodule.cpp `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy
gcc手册页说-l选项,
The gcc man page says of the "-l" option,
由于mymodule的名称。因此在它应该链接到的库之前提供,它们都没有实际链接到生成的.so文件。
Since the name of mymodule.so was provided before the libraries it was supposed to be linked to, none of them were actually linked to the generated .so file.
感谢@ J.F.Sebastian指出-l如何工作。
Thanks for @J.F.Sebastian for pointing out how -l works.
这篇关于Python ImportError - 未定义的符号 - 用于自定义C ++模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!