CMAKE RPATH 不工作 - 找不到共享对象文件

CMAKE RPATH not working - could not find shared object file

每次我 运行 我的程序时,我都试图摆脱设置 LD_LIBRARY_PATH。添加库并将我的可执行文件定位到库后,当我 运行 它告诉我它无法打开共享对象库,没有这样的文件或目录。

在我的 CMakeLists.txt 我有:

add_library(heart SHARED ${HEART_FILES})
add_executable(run ${RUN_FILES})
target_link_libraries(run heart)
set(CMAKE_SKIP_BUILD_PATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "~/person/target/usr/local/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

我为我的库文件夹设置了一个绝对 link 以测试这是否会为我的库创建一个 rpath,但似乎没有。我已经检查并确保共享库确实在 lib 中。 libheart.so 是正在 link 编辑的文件。我还缺少什么?

这是因为你从同一个cmake项目中构建heart运行:

CMAKE_INSTALL_RPATH_USE_LINK_PATH is an interesting and very useful option. When building a target with RPATH, CMake determines the RPATH by using the directories of all libraries to which this target links. Some of these libraries may be located in the same build tree, e.g. libbar.so, these directories are also added to the RPATH. If this option is enabled, all these directories except those which are also in the build tree will be added to the install RPATH automatically. The only directories which may then still be missing from the RPATH are the directories where the libraries from the same project (i.e. libbar.so) are installed to. If the install directory for the libraries is not one of the systems default library directories, you have to add this directory yourself to the install RPATH by setting CMAKE_INSTALL_RPATH accordingly

你可以试试这个:

SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

这里有更多文档cmake rpath handling

编辑:

只有这个应该有效:

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

add_library(heart SHARED ${HEART_FILES})
add_executable(run ${RUN_FILES})
target_link_libraries(run heart)

install(
  TARGETS heart run
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
)

清理您的构建目录,然后:

cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/home/person/target/usr/local ..
make install

在 g++ 行的末尾 Linking CXX executable 运行 你应该看到 -Wl,-rpath,/home/person/target/usr/local/lib

如果您想要一个完全可重定位的包:

set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")

PS:您确定 libheart.so 没有找到吗?

在您的 CMake 文件中,在定义目标之前设置 RPATH。 CMAKE_INSTALL_RPATH必须在调用add_executable()之前定义,否则无效。

我遇到了与原始 post 类似的问题。我创建了链接到外部共享库的可执行文件。这种方法从构建目录编译并执行得很好。但是,安装到单独目录的可执行文件在运行时找不到共享库:

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

为了解决,我

1) 升级到 CMake 3.17

2) 使用了 Craig Scott 的推荐: 设置(CMAKE_INSTALL_RPATH $ORIGIN) 正如他在 talk

中所解释的

3) 直接提到set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 来解决Kitware documention

中第二个常见问题中的这个错误

4) 将所有这些 放在 之前添加 post

中提到的目标

5) 使用“$ORIGIN/../lib”语法而不是 Craig 的 Scott 提到的 $ORIGIN,如 @explo91

总而言之,令我惊讶的是,上面只有目标定义之前的“$ORIGIN/../lib”是必需的(我测试了其他没有解决cannot open shared object file运行时问题的组合).

无论如何,我最终应用的解决方案可能是更好的、细粒度的 CMake 风格,或者至少可能对其他人的 RPATH 之旅有所帮助:

set_target_properties(target_defined_above PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")