SharedLibraryExample.tbz (1.1 KB)
Please find the attached SharedLibraryExample project with CMAKE_INSTALL_RPATH simply set to $ORIGIN.
To configure:
cmake -B build_x86_64/Debug -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_INSTALL_PREFIX=build_x86_64/Debug/install -DCMAKE_BUILD_TYPE=Debug
To build:
cmake --build build_x86_64/Debug
Quick checks give:
$ readelf -d build_x86_64/Debug/libShared.so | grep PATH
0x000000000000001d (RUNPATH) Library runpath: [:::::::]
$ readelf -d build_x86_64/Debug/Example | grep PATH
0x000000000000001d (RUNPATH) Library runpath: [/home/<user>/SharedLibraryExample/build_x86_64/Debug:]
file(RPATH_CHECK statements in cmake_install.cmake read as:
file(RPATH_CHECK
FILE "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib64/libShared.so"
RPATH "\$ORIGIN")
file(RPATH_CHECK
FILE "$ENV{DESTDIR}/home/<user>/SharedLibraryExample/build_x86_64/Debug/Example"
RPATH "\$ORIGIN")
Trying to perform install will thus fail, as expected and currently set RUNPATH differ and RPATH_CHECK ends up deleting the Example binary:
$ cmake --build build_x86_64/Debug -t install
[ 50%] Built target Shared
[100%] Built target Example
Install the project...
-- Install configuration: "Debug"
-- Installing: /home/<user>/SharedLibraryExample/build_x86_64/Debug/install/lib64/libShared.so
-- Set non-toolchain portion of runtime path of "/home/<user>/SharedLibraryExample/build_x86_64/Debug/install/lib64/libShared.so" to "$ORIGIN"
-- Installing: /home/<user>/SharedLibraryExample/build_x86_64/Debug/install/include/SharedLibrary/SharedLibrary.h
CMake Error at cmake_install.cmake:87 (file):
file INSTALL cannot find
"/home/<user>/SharedLibraryExample/build_x86_64/Debug/Example": No such file
or directory.
gmake: *** [Makefile:100 : install] Error 1
As explained in my previous post, skipping RUNPATH info and passing them as link options is actually the only solution giving me something working. To this end, in the CMakeLists.txt file provided in the SharedLibraryExample project:
- comment out ln. 7:
#set(CMAKE_INSTALL_RPATH $ORIGIN)
- uncomment ln. 10:
set(CMAKE_SKIP_RPATH TRUE)
- uncomment ln. 48:
target_link_options(Example PRIVATE "LINKER:-rpath,$ORIGIN")
Re-run configure:
cmake -B build_x86_64/Debug -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_INSTALL_PREFIX=build_x86_64/Debug/install -DCMAKE_BUILD_TYPE=Debug
Build and install at once:
$ cmake --build build_x86_64/Debug -t install
[ 25%] Building CXX object CMakeFiles/Shared.dir/src/SharedLibrary.cpp.o
[ 50%] Linking CXX shared library libShared.so
[ 50%] Built target Shared
[ 75%] Building CXX object CMakeFiles/Example.dir/src/main.cpp.o
[100%] Linking CXX executable Example
[100%] Built target Example
Install the project...
-- Install configuration: "Debug"
-- Installing: /home/<user>/SharedLibraryExample/build_x86_64/Debug/install/lib64/libShared.so
-- Installing: /home/<user>/SharedLibraryExample/build_x86_64/Debug/install/include/SharedLibrary/SharedLibrary.h
No problem this time.
Checks give:
$ readelf -d build_x86_64/Debug/libShared.so | grep PATH
$ readelf -d build_x86_64/Debug/Example | grep PATH
0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN]
cmake_install.cmake is empty of any RPATH_CHECK statement. And of course, binary is working fine 
$ ./build_x86_64/Debug/Example
Hello!
Hope this helps.