Transitive RPATH property in CMake
I am creating a library that I link with several tests and sample programs. The project boasts the cmake property BUILD_SHARED_LIBS
, creating .so
if enabled and creating a file .a
if disabled. The library depends on several other dynamic libraries to be found using an environment variable LIBLOC
(their convention, not mine).
When I create a dynamic version of a library, I can tweak the installation rpath so that the libraries are found correctly. For example:.
add_library(lib ${SRC})
set_target_properties(lib INSTALL_RPATH $LIBLOC)
install(TARGETS lib LIBRARY DESTINATION lib)
And then in each CMakeLists.txt test / example program:
add_executable(test ${SRC})
target_link_libraries(test lib)
set_target_properties(test INSTALL_RPATH $ORIGIN/../lib)
install(TARGETS test RUNTIME bin)
When BUILD_SHARED_LIBS
enabled, the test program is happy:
$ lddtree bin/test
test => bin/test (interpreter => /lib64/ld-linux-x86-64.so.2)
liblib.so => /path/to/install/liblib.so
libdep.so => /path/to/dependency/libdep.so
We can find everything because the rpath in the test $ORIGIN/../lib
that finds liblib.so
and then the rpath to liblib.so
is equal $LIBLOC
to which is set in my environment to /path/to/dependency
.
I would expect that when installing the static library, cmake will read the property INSTALL_RPATH
from my library and put it in the test program so that the rpath in the test is $ORIGIN/../lib;$LIBLOC
. Instead, it $LIBLOC
does not fall into any rpath.
My current solution is unsatisfactory: I set mine INSTALL_RPATH
to BUILD_SHARED_LIBS
in every example or test program like
if (BUILD_SHARED_LIBS)
set_target_properties(test INSTALL_RPATH "$ORIGIN/../lib")
else ()
set_target_properties(test INSTALL_RPATH "$LIBLOC")
endif ()
I now have several goals, all of which should be aware of dependency dependencies. Plus a bunch of smelly statements. Ugh!
Is there a better way in cmake to pass the rpath setting "up" to the first ELF file generated when my libraries are statically compiled?
source to share
No one has answered this question yet
Check out similar questions: