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

前端 未结 1 845
温柔的废话
温柔的废话 2020-12-29 05:00

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 Ubu

相关标签:
1条回答
  • 2020-12-29 05:00

    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.

    0 讨论(0)
提交回复
热议问题