0

I have a project with both target shared libraries and target executables. Using a minimal example for illustration purpose, the layout is as following:

project
    bin
    lib
    build
    CMakeList.txt
    foo
        CMakeList.txt 
        foo.hpp
        foo.cpp
    test_foo
        CMakeList.txt
        test_foo.cpp

Subdirectory foo creates a shared library foo.so. Subdirectory test_foo creates an executable test_foo, which is linked against foo in the build tree. The build is successful. However during installation I can not figure out how to install foo.so to lib, test_foo to bin, while having bin/test_foo linked against lib/foo.so. I used the install command:

install(TARGETS foo LIBRARY DESTINATION lib)
install(TARGETS foo_test RUNTIME DESTINATION bin)

in the respective subdirectory CMakeList.txt of foo and test_foo. foo.so appeared in lib and test_foo appeared in bin. However bin/test_foo is not linked to lib/foo.so. What's the correct way to do this?

Muye
  • 175
  • 1
  • 7
  • "However `bin/test_foo` is not linked to `lib/foo.so`." - What do you mean by "not linked"? Have you got some error? When? – Tsyvarev Mar 30 '19 at 12:36
  • There's no error from Cmake. But `ldd bin/test_foo` shows `libfoo.so` as not found. Executing `bin/test_foo` fails with `error while loading shared libraries: No such file or directory`. – Muye Mar 30 '19 at 12:39
  • So, you actually have your executable **linked** with the library (otherwise `ldd` won't show the corresponded entry at all). What do you miss is a **RPATH** adjustment. RPATH mechanism helps to locate linked libraries at runtime. See that wiki page about handling of RPATH in CMake: https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling. – Tsyvarev Mar 30 '19 at 12:42
  • Thanks a lot for the tip, `RPATH` is indeed the reason. Setting it accordingly solves the issue. – Muye Mar 30 '19 at 12:52

0 Answers0