我想在链接时自动将库路径添加到RPATH。看了一点之后,我认为CMAKE_INSTALL_RPATH_USE_LINK_PATH
应该可以完成这项工作。不幸的是,以下MWE无法完成该工作:
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0060 NEW)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
# set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
# set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
find_package(Boost COMPONENTS serialization REQUIRED)
# Setting CMAKE_INSTALL_RPATH explicitly works
# set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${Boost_LIBRARY_DIRS}")
add_executable(app main.cxx)
target_include_directories( app
PUBLIC ${Boost_INCLUDE_DIR})
target_link_libraries( app
PUBLIC Boost::serialization)
install(TARGETS app DESTINATION bin)
上面示例中readelf -d install/bin/app
的输出显示了相关性,但没有RPATH:
0x0000000000000001 (NEEDED) Shared library: [libboost_serialization.so.1.65.1]
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
注释行是到目前为止我尝试过的一些东西。显式设置CMAKE_INSTALL_RPATH
是可行的。但是设置CMAKE_INSTALL_RPATH_USE_LINK_PATH
似乎无效。对于二进制文件,构建文件和已安装的二进制文件都是如此。
我是否误解了CMAKE_INSTALL_RPATH_USE_LINK_PATH
在做什么,或者有人可以发现错误?
CMake 3.13,CentOS 7