Python ImportError - undefined symbol - for custom C++ module


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.

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.

This is how I compile the module:

g++ -fPIC -shared `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy -o mymodule.cpp

This is the output of "pkg-config --cflags --libs opencv"

-I/usr/local/include/opencv -I/usr/local/include  /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/ /usr/local/lib/

The error I get is:

ImportError: /path/to/service/ undefined symbol: _ZN5CvSVMD1Ev

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 because when I run this:

$ nm -g  /usr/local/lib/ | grep _ZN5CvSVMD1Ev

I get:

000000000002fd40 T _ZN5CvSVMD1Ev

/usr/local/lib seems to be in the dynamic linker cache.

$ cat /etc/ 
# libc default configuration

And the so file is there in the cache too.

$ ldconfig -p | grep opencv | grep ml (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/

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?

7/25/2012 6:47:19 AM

Accepted Answer

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.cpp `pkg-config --cflags --libs python` `pkg-config --cflags --libs opencv` -I/usr/local/include/opencv2/legacy

The gcc man page says of the "-l" option,

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

Since the name of was provided before the libraries it was supposed to be linked to, none of them were actually linked to the generated .so file.

Thanks for @J.F.Sebastian for pointing out how -l works.

7/26/2012 6:07:16 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow