Jump to content
  • Advertisement
Sign in to follow this  
TheComet

cmake linking errors

This topic is 1339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by TheComet

Share this post


Link to post
Share on other sites
Advertisement

It's hilarious, I tried to compile and I got:

make[2]: *** No rule to make target `/usr/lib/x82.74-linux-gnu/libboost_python.so', needed by `app/app'.  Stop.

CMake actually thought the "6_6" part in "x86_64" was some kind of version number and replaced it by 2.7. Epic fail. Anyway, have you tried linking boost_python before python? It is my understanding that the normal linker order is left-to-right, i.e. dependencies come last, unless you are using a nonstandard linker like on Windows or some special option such as the "scan twice" option of GNU ld to resolve circular dependencies.

Share this post


Link to post
Share on other sites

CMake actually thought the "6_6" part in "x86_64" was some kind of version number and replaced it by 2.7. Epic fail.

Might that have something to do with the way I'm forcing boost to use python 2.7?
 
        # hack to force boost to use python2.7
        string (REGEX REPLACE "[0-9].[0-9]" "2.7" Boost_PYTHON_LIBRARY ${Boost_PYTHON_LIBRARY})
(See here for related thread)
 
 

Anyway, have you tried linking boost_python before python? It is my understanding that the normal linker order is left-to-right, i.e. dependencies come last, unless you are using a nonstandard linker like on Windows or some special option such as the "scan twice" option of GNU ld to resolve circular dependencies.

It's very strange because the project I linked in the OP compiles fine on my desktop computer. My laptop can't compile it for some reason. wtf. Re-arranging the linking order doesn't do anything, which makes sense because linking order is only relevant when linking against static libraries.

Both are Gentoo, both are amd64... The only difference is that on my laptop boost_python isn't installed so CMake will download that dependency. Edited by TheComet

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!