为什么在Linux中加载共享库时安装的程序有错误(cmake) [英] Why the installed program has error when loading shared library in linux (cmake)

查看:116
本文介绍了为什么在Linux中加载共享库时安装的程序有错误(cmake)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于如何安装一个构建的可执行程序与cmake当它依赖于一些外部库。假设我的可执行文件是 abc ,它依赖于两个外部库: lib1.so lib2.so 。代码的结构如下:

I have a question related to how to install a built executable program with cmake when it relies on some external libraries. Suppose my executable is abc, and it relies on two external libraries: lib1.so and lib2.so. The structure of the codes are as follows:

-.........
  |----bin (lib1.so lib2.so)
  |----include(lib1.h lib2.h)
  |----src(main.cpp)

使用以下cmake命令安装可执行程序时:

When the executable program is installed using the following cmake commands:

INSTALL(TARGETS ${Exe_Name}
    RUNTIME DESTINATION Path to bin
    LIBRARY DESTINATION Path to bin)

我期望可执行程序将在 lib1.so lib2.so 。但是,当我在安装文件夹中执行构建的程序时,我遇到以下错误:

I expect that the executable program will be in the same directory with lib1.so and lib2.so. However, when I execute the built program in the installation folder, I met the following error:

error while loading shared libraries: lib1 can not open shared object file No such file or directory

如果我使用ldd检查可执行文件,找到 lib1.so lib2.so未找到。在搜索可能的解决方案后,我发现如果我以这种方式调用可执行文件,然后它工作:

If I use ldd to check the executable, I found lib1.so and lib2.so not found. After searching for possible solutions, I found if I call the executable in this way, then it worked:

LD_LIBRARY_PATH=./ ./my_program_run

然后我的问题是如何让我的可执行程序知道共享库的位置cmake当它安装?谢谢。

Then my question is how I can let my executable program knows the locations of the shared libraries with cmake when it is installed? Thanks.

推荐答案

这最好用最终可执行文件的RPATH解决。 RPATH是可执行文件本身的硬编​​码搜索路径,允许使用字符串 $ ORIGIN ,它在运行时扩展到可执行文件的位置。请参阅以下参考资料: http://man7.org/linux/man -pages / man8 / ld.so.8.html

This is best solved this with the RPATH of the final executable. RPATH is a hardcoded search path for the executable itself, and allows the use of the string $ORIGIN, which expands to the location of the executable at runtime. See this reference: http://man7.org/linux/man-pages/man8/ld.so.8.html

CMake 二进制在安装时,避免二进制拾取你的开发树周围的库。但它也提供了一个简单的方法来修改安装rpath正是出于这个原因。这是简短的答案:

CMake strips the rpath of a binary at installation time, to avoid the binary picking up libraries littered around your development tree. But it also provides a simple way to modify the installation rpath for exactly this reason. Here's the short answer:

IF(UNIX)
  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:\$ORIGIN/../bin:\$ORIGIN")
ENDIF()

示例追加到现有的rpath,并向搜索路径添加 ../ bin

This particular example appends to the existing rpath, and adds . and ../bin to the search path, all relative to the location of the binary.

一些开发者声称,调整二进制文件的RPATH不是一个好主意。在理想的世界中,所有的库都会驻留在系统库目录中。但是,如果你把这个极端,你最终与Windows(至少是较旧的),其中c:\windows \system32充满了垃圾,来自谁知道在哪里,可能或可能不会与其他软件等。使用rpath和在一个地方安装一切似乎是一个很好的解决方案。

Some developers claim that adjusting the RPATH of the binary is not a good idea. In the ideal world, all the libraries would live in the system library directories. But if you take this to the extreme, you end up with Windows (at least the older ones), where c:\windows\system32 is full of junk that came from who knows where, and may or may not conflict with other software, etc. Using rpath and installing everything in one place seems like a great solution.

这篇关于为什么在Linux中加载共享库时安装的程序有错误(cmake)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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