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

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


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

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 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.

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