CMAKE RPATH不起作用-找不到共享对象文件 [英] CMAKE RPATH not working - could not find shared object file

查看:589
本文介绍了CMAKE RPATH不起作用-找不到共享对象文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get rid of setting LD_LIBRARY_PATH everytime time I run my program. After adding in the library and targeting my executable to the library, when I run it tells me it can not open shared object library, no such file or directory.

在我的CMakeLists.txt中,我有:

In my CMakeLists.txt I have:

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)

我设置了到我的库文件夹的绝对链接,以测试这是否会创建到我的库的rpath,并且似乎没有.我已经检查并确保共享库确实在lib中. libheart.so是被链接的文件.我还想念什么?

I set an absolute link to my library folder to test out whether this would create an rpath to my library and it seems like there isn't. I have checked and made sure that the shared library is indeed in lib. libheart.so is the file that is being linked. What else am I missing?

推荐答案

这是因为您是从同一个cmake项目中构建 heart run 的:

It is because you build heart and run from the same cmake project:

CMAKE_INSTALL_RPATH_USE_LINK_PATH是一个有趣且非常有用的选项.当使用RPATH构建目标时,CMake通过使用该目标链接到的所有库的目录来确定RPATH.这些库中的某些库可能位于同一构建树中,例如libbar.so,这些目录也被添加到RPATH中. 如果启用了此选项,那么所有这些目录(除了那些也在构建树中的目录)都将自动添加到安装RPATH中.然后,RPATH可能仍然缺少的唯一目录是安装了来自同一项目(即libbar.so)的库的目录.如果这些库的安装目录不是系统默认的库目录之一,则必须通过相应地设置CMAKE_INSTALL_RPATH将此目录自己添加到安装RPATH中

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处理

这仅适用于

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
)

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

Clean your build directory and then:

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

在g ++行的结尾处链接CXX可执行程序运行,您应该会看到 -Wl,-rpath,/home/person/target/usr/local/lib

At the end of the g++ line Linking CXX executable run you should see like -Wl,-rpath,/home/person/target/usr/local/lib

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

If you want a fully relocatable package:

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

PS:您是否发现没有找到 libheart.so 吗?

PS: are you sur that it is libheart.so that is not found ?

这篇关于CMAKE RPATH不起作用-找不到共享对象文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆