0

I have the following app being built and linked using CMake.

add_executable(my_node main.cpp)    
target_link_libraries(my_node dynamic_lib my_dependency)

The source of my_node DOES NOT use dynamic_lib directly. my_dependency requires dynamic_lib to run.

Using readelf and ldd, I can see that dynamic_lib is not found on my_node; my app fails to launch. I can launch the app by instead setting LD_LIBRARY_PATH.

LD_LIBRARY_PATH="/path/to/dynamic_lib_dir" bash -c './my_node'

Alternatively, if I use classes from dynamic_lib in my_node source, it all works. I think I'm missing some CMake configuration here to force my_node to directly link with dynamic_lib. How can I bundle all of this up neatly?

Chris
  • 387
  • 5
  • 16
  • Does this answer your question? [C/cmake - how to add a linker flag to an (unused) library when the library is specified in TARGET\_LINK\_LIBRARIES?](https://stackoverflow.com/questions/17470350/c-cmake-how-to-add-a-linker-flag-to-an-unused-library-when-the-library-is-sp) – Kevin Dec 12 '19 at 18:17
  • I do not believe so. I have tried in Cmake and directly with c++. The resulting binary cannot resolve my lib. `/usr/bin/c++ ... -Wl,--whole-archive /path/to/dynamic_lib.so -Wl,--no-whole-archive \ ` ldd still declares `dynamic_lib.so => not found` – Chris Dec 12 '19 at 19:28
  • Since only `my_dependency` directly needs `dynamic_lib`, why not link `dynamic_lib` to `my_dependency` instead? – Kevin Dec 12 '19 at 19:43
  • 1
    @Chris: if `ldd` says that dynamic_lib is not found, then it is linked to your executable, but then at runtime it can't be resolved unless you use the environment variable $LD_LIBRARY_PATH. This is what solves RPATH. See https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling – Former contributor Dec 12 '19 at 20:20
  • 1
    The title is misleading: the library is actually **linked** into your executable. As result, `ldd` shows entry for the library. The reason, why corresponded library file is not found, is RPATH settings. See there for more details: https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling – Tsyvarev Dec 12 '19 at 20:28

0 Answers0