Missing -rpath-link argument for imported shared dependencies
Used CMake version: 3.21.2 and master branch at c5b91304.
Consider a project with two shared libs that depend on each other:
# src/libs/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(my_libs)
add_library(A SHARED a.cpp)
add_library(B SHARED b.cpp)
target_link_libraries(B PRIVATE A)
install(TARGETS A EXPORT A LIBRARY)
install(TARGETS B EXPORT B LIBRARY)
install(EXPORT A FILE AConfig.cmake DESTINATION lib/cmake/A)
install(EXPORT B FILE BConfig.cmake DESTINATION lib/cmake/B)
We cross-build this library with some embedded Linux toolchain. The important thing is, that a sysroot is set.
Now, we try to build an application against the installed libs:
# src/app/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(my_app)
find_package(B CONFIG REQUIRED)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE B)
This fails, because of a missing -rpath-link
argument.
The -rpath
argument that's added is not sufficient, because the sysroot is be prepended to it internally by the linker.
See QTBUG-86533 for a detailed explanation.
If both projects are built as part of a top-level project, the -rpath-link
argument is correctly added.
Note that this behavior wrt -rpath-link
can be reproduced on desktop Linux already.
Just the linking of my_app does not fail for the reason mentioned above.
I've attached an example to reproduce this issue: cmake-rpath-link-issue.tar.gz
The example specifies an invalid CMAKE_SYSROOT
to simulate a cross-build on Desktop Linux.
Extract the archive and run the reproduce
script.