CMake cannot recognize some unnormalized linker search path as "outside the project" path correctly
When linking a shared library outside of project directory and setting the comsuming target's INSTALL_RPATH_USE_LINK_PATH
property to ON
, if the external library path is not normalized, CMake sometimes cannot recognize it as a library outside of the project and thus will not add its path to the runtime install rpath.
Environment:
- Linux x86_64
- cmake 3.18.4
- gcc 8.3.0 & gcc 10.2.0
Steps to reproduce:
-
Prepare the following files:
-
~/temp1.cpp
:int foo(){return 0;}
-
~/proj1/proj1.cpp
:int foo(); int main(){return foo();}
-
~/proj1/CMakeLists.txt
:cmake_minimum_required(VERSION 3.12 FATAL_ERROR) project(proj1) add_executable(proj1 proj1.cpp) target_link_libraries(proj1 PRIVATE ${CMAKE_SOURCE_DIR}/../libtemp1.so) set_target_properties(proj1 PROPERTIES INSTALL_RPATH_USE_LINK_PATH ON) install(TARGET proj1 RUNTIME DESTINATION bin)
-
-
Run command
gcc -shared -o ~/libtemp1.so -fPIC ~/temp1.cpp
to generate a shared library outside ofproj1
directory. -
Run the following command to build proj1:
mkdir -p ~/build cd ~/build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/install ~/proj1 cd ~/build && cmake --build . --config Release cd ~/build && cmake --install . --config Release
Expected behavior:
- Running command
ldd ~/build/proj1 | grep found
will not give any output and will exit with exit code 1. - Running command
ldd ~/install/bin/proj1 | grep found
will not give any output and will exit with exit code 1. - Running command
grep -E 'NEW_RPATH \"\"\)' ~/build/cmake_install.cmake
will not give any output and will exit with exit code 1.
Actual behavior
-
Running command
ldd ~/build/proj1 | grep found
doesn't give any output and exits with exit code 1. -
Running command
ldd ~/install/bin/proj1 | grep found
gives the following output and exits with exit code 0:libtemp1.so => not found
-
Running command
grep -E 'NEW_RPATH \"\"\)' ~/build/cmake_install.cmake
gives the following output and exits with exit code 0:NEW_RPATH "")
Additional Info
If I change line target_link_libraries(proj1 PRIVATE ${CMAKE_SOURCE_DIR}/../libtemp1.so)
in ~/proj1/CMakeLists.txt
to target_link_libraries(proj1 PRIVATE $ENV{HOME}/libtemp1.so)
, CMake's behavior will be the expected one described above.