https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
https://cmake.org/pipermail/cmake/2008-January/019290.html
https://www.technovelty.org/linux/exploring-origin.html
https://cmake.cmake.narkive.com/bEic9mqt/rpath-and-origin
在CMake中,事情则有些不同。由于CMake需要包揽软件安装的事宜,因此CMake使用两个变量来控制RPATH:INSTALL_RPATH和BUILD_RPATH。
设置的办法是:
SET_TARGET_PROPERTIES(target PROPERTIES INSTALL_RPATH "$ORIGIN;/another/run/path")
set(CMAKE_SKIP_BUILD_RPATH FALSE) # 编译时加上RPATH
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # 编译时RPATH不使用安装的RPATH
set(CMAKE_INSTALL_RPATH “”) # 安装RPATH为空
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) # 安装的执行文件不加上RPATH
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH “${ORIGIN}/…/lib”)
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH “${ORIGIN}”)
3.14
set(CMAKE_INSTALL_RPATH $ORIGIN)
对于第一个变量CMAKE_SKIP_RPATH,简单的来说,就是强制CMake不在构建期间和安装install期间给你加上它所认为的RPATH.
即
cmake … -DCMAKE_SKIP_RPATH=TRUE
第二个和第三个变量也比较简单,就是分别在构建期间和安装期间不允许CMake给你加入相关RPATH
cmake … -DCMAKE_SKIP_BUILD_RPATH=TRUE
cmake … -DCMAKE_SKIP_INSTALL_RPATH=TRUE
centos系统测试,该方法设置有效,不过EXE中$$ORIGIN还是$ORIGIN需要根据系统不同尝试
SET (CMAKE_SHARED_LINKER_FLAGS
“${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath,‘$ORIGIN/…/xxx’” )
SET (CMAKE_EXE_LINKER_FLAGS
“${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,‘$ORIGIN/…/xxx/’” )
评论区