I created a project demonstrating the problem I'm having: https://github.com/TheComet93/cmake-sucks
I have a library which links against python and boost_python. The library compiles fine, but if I try to link against my library from an executable, I get these linking errors:
$ make
[ 50%] Built target library
Linking CXX executable app
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyBytes_Size'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_FromString'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_FromFormat'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyBytes_AsString'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_InternFromString'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_FromEncodedObject'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_AsWideChar'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyModule_Create2'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_FromStringAndSize'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_AsUTF8String'
../../cmake-dep/lib/libboost_python.so: undefined reference to `PyUnicode_AsUTF8'
collect2: error: ld returned 1 exit status
app/CMakeFiles/app.dir/build.make:91: recipe for target 'app/app' failed
make[2]: *** [app/app] Error 1
CMakeFiles/Makefile2:160: recipe for target 'app/CMakeFiles/app.dir/all' failed
make[1]: *** [app/CMakeFiles/app.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2
Enabling verbose output reveals the core of the issue:
/usr/bin/c++ CMakeFiles/app.dir/main.cpp.o -o app -L/home/thecomet/documents/programming/c++/cmake-sucks/cmake-dep/lib -rdynamic ../library/liblibrary.a -lpython2.7 ../../cmake-dep/lib/libboost_python.so -Wl,-rpath,/home/thecomet/documents/programming/c++/cmake-sucks/cmake-dep/lib
liblibrary.a is being linked before python2.7 and boost_python, which shouldn't be the case.
I'm stuck at how I'm supposed to fix this.
If you look at the CMakeLists.txt files, you'll see I'm basically just doing:
add_library(library ${files})
target_link_library(library python boost_python)
and:
add_executable(app ${files})
target_link_library(app library)
This is already enough to generate these linker errors.