Search code examples
cmake

CMake link to external library


How to get CMake to link an executable to an external shared library that is not build within the same CMake project?

Just doing target_link_libraries(GLBall ${CMAKE_BINARY_DIR}/res/mylib.so) gives the error

make[2]: *** No rule to make target `res/mylib.so', needed by `GLBall'.  Stop.
make[1]: *** [CMakeFiles/GLBall.dir/all] Error 2
make: *** [all] Error 2
(GLBall is the executable)

after I copied the library into the binary dir bin/res.

I tried using find_library(RESULT mylib.so PATHS ${CMAKE_BINARY_DIR}/res)

Which fails with RESULT-NOTFOUND.


Solution

  • Set libraries search path first:

    link_directories(${CMAKE_BINARY_DIR}/res)
    

    And then just do

    target_link_libraries(GLBall mylib)