gpt4 book ai didi

c++ - CMake:将包添加到 rpath

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:06 48 4
gpt4 key购买 nike

我有一个用 CMake 构建的动态库 libhelloworld.so,我安装在 /usr/local/lib64 中。我在构建可执行文件的 CMakeLists.txt 文件中链接库:

find_package(HelloWorld REQUIRED)
target_link_libraries(helloexecutable HelloWorld::HelloWorld)

CMake 可以很好地构建可执行文件,但是当我运行它时出现以下错误:

error while loading shared libraries: libhelloworld.so: cannot open shared object file: No such file or directory

我能够通过设置 rpath 来解决问题

set(CMAKE_INSTALL_RPATH "/usr/local/lib64")

但这不是可移植的。

所以我有两个问题:1) CMake 不应该为链接的动态库自动设置 rpath 吗?和 2) 如果 Cmake 可以自动设置 rpath,我该如何告诉它这样做,如果不能,设置 rpath 的更便携的方法是什么?

最佳答案

CMake wiki 中描述了观察到的行为(谢谢,Tsyvarev!)

By default if you don't change any RPATH related settings, CMake will link the executables and shared libraries with full RPATH to all used libraries in the build tree. When installing, it will clear the RPATH of these targets so they are installed with an empty RPATH.

这意味着您需要告诉 CMake 将 RPATH 中自动确定的部分添加到安装 RPATH 中,这些部分指向构建树之外的目录

SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

换句话说,1) 是的,CMake 会自动设置 rpath,但默认情况下仅针对构建树。 2) CMake 也可以为已安装的可执行文件设置 rpath,只需告诉它这样做,如上所示。

关于c++ - CMake:将包添加到 rpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486566/

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