gpt4 book ai didi

cmake - cpack(创建 RPM)忽略 CMAKE_INSTALL_RPATH(将 RPATH 设置为空字符串 - 而不是显式值)

转载 作者:行者123 更新时间:2023-12-02 03:03:03 25 4
gpt4 key购买 nike

我正在尝试使用 cpack 进行一个相当简单的实验。目的是使用一个简单的可执行文件创建一个 RPM 安装程序(使用 cpack),该可执行文件使用外部第 3 方共享库(称为 libSomeSharedLib.so)。

我希望 RPM 安装的结构是

opt
|_cmakeFindPackageTest
|_bin
|_cmakeFindPackageTest (an executable)
|_lib
|_libSomeSharedLib.so (the shared library)

我希望可执行文件的 RPATH 为 /opt/cmakeFindPackageTest/lib (以确保它使用已安装的共享库)。

完整的 CMakeLists.txt 粘贴在底部,但请注意设置了以下属性

 15 SET(CMAKE_INSTALL_PREFIX "/opt/cmakeFindPackageTest")
16 SET( INSTALL_DIR_BIN ${CMAKE_INSTALL_PREFIX}/bin )
17 SET( INSTALL_DIR_LIB ${CMAKE_INSTALL_PREFIX}/lib )
18
19 SET(CMAKE_INSTALL_RPATH "\${INSTALL_DIR_LIB}")
20 SET(CMAKE_SKIP_BUILD_RPATH TRUE)
21 SET(CMAKE_SKIP_INSTALL_RPATH FALSE)

据我了解,第 17,19,20 行应该导致 cpack 将可执行文件的 RPATH 设置为 /opt/cmakeFindPackageTest/lib

但是...

当我构建项目(从 clean 开始)并运行 cpack 来生成 RPM 时,我在输出中看到了这一点

bash-4.2$ cpack -V -G RPM
CPack: Enable Verbose
... irrelevant looking output ommitted ...
CPack Verbose: Set runtime path of "/local/bfarnham/workspace/OPC-UA/CMake_examples/cmakeFindPackageTest/build/_CPack_Packages/Linux/RPM/cmakeFindPackageTest-0.0.0-Linux/opt/cmakeFindPackageTest/bin/cmakeFindPackageTest" to ""

RPATH 被设置为空! 呃?用readelf检查过 - 果然如此

bash-4.2$ readelf -d ./_CPack_Packages/Linux/RPM/cmakeFindPackageTest-0.0.0-Linux/opt/cmakeFindPackageTest/bin/cmakeFindPackageTest | grep --context=1 -i RPATH
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000f (RPATH) Library rpath: []
0x000000000000000c (INIT) 0x400848

最后,问题是:如何让 cpack 在 RPM 中设置此可执行文件的 RPATH?

================完整的CMakeLists.txt ================

  1 cmake_minimum_required( VERSION 3.0 )
2 project( cmakeFindPackageTest CXX )
3 set (CMAKE_CXX_STANDARD 11)
4
5 list( INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake )
6 message( STATUS "CMAKE_MODULE_PATH [${CMAKE_MODULE_PATH}]" )
7 find_package( SomeSharedLib 1.0 REQUIRED MODULE )
8 message( STATUS "SomeSharedLib_INCLUDE_DIR [${SomeSharedLib_INCLUDE_DIR}] SomeSharedLib_LIBRARY [${SomeSharedLib_LIBRARY}]" )
9
10 add_executable( cmakeFindPackageTest src/main.cpp )
11 target_link_libraries( cmakeFindPackageTest SomeSharedLib::SomeSharedLib )
12
13 # =============================== INSTALL DETAILS BELOW THIS POINT ==========================
14
15 SET(CMAKE_INSTALL_PREFIX "/opt/cmakeFindPackageTest")
16 SET( INSTALL_DIR_BIN ${CMAKE_INSTALL_PREFIX}/bin )
17 SET( INSTALL_DIR_LIB ${CMAKE_INSTALL_PREFIX}/lib )
18
19 SET(CMAKE_INSTALL_RPATH "\${INSTALL_DIR_LIB}")
20 SET(CMAKE_SKIP_BUILD_RPATH TRUE)
21 SET(CMAKE_SKIP_INSTALL_RPATH FALSE)
22 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
23
24 SET(CPACK_PACKAGE_NAME cmakeFindPackageTest )
25 SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "test")
26 SET(CPACK_PACKAGE_VENDOR "TEST_VENDOR")
27 SET(CPACK_PACKAGE_RELOCATABLE FALSE)
28 SET(CPACK_PACKAGE_VERSION_MAJOR "0")
29 SET(CPACK_PACKAGE_VERSION_MINOR "0")
30 SET(CPACK_PACKAGE_VERSION_PATCH "0")
31
32 install( TARGETS
33 cmakeFindPackageTest
34 RUNTIME DESTINATION ${INSTALL_DIR_BIN} )
35
36 install( FILES
37 ${SomeSharedLib_LIBRARY}
38 DESTINATION ${INSTALL_DIR_LIB} )
39
40 include( CPack )

最佳答案

我通过显式设置可执行目标的 INSTALL_RPATH 属性值修复了此问题。因此,在调用 install

之后添加 set_target_properties
install( TARGETS 
cmakeFindPackageTest
RUNTIME DESTINATION ${INSTALL_DIR_BIN} )
set_target_properties( cmakeFindPackageTest PROPERTIES INSTALL_RPATH "/opt/cmakeFindPackageTest/lib" )

从 cmake/cpack 文档来看,设置 CMAKE_INSTALL_RPATH 变量似乎应该有效。 CMAKE_INSTALL_RPATH 文档读取

The rpath to use for installed targets.

A semicolon-separated list specifying the rpath to use in installed targets (for platforms that support it). This is used to initialize the target property INSTALL_RPATH for all targets.

关于cmake - cpack(创建 RPM)忽略 CMAKE_INSTALL_RPATH(将 RPATH 设置为空字符串 - 而不是显式值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59649947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com