在可执行文件中找不到 C++ 动态链接库

标签 c++ c gcc shared-libraries rpath

我在路径 /home/test/lib/libTest.so 中有一个 .so 以及一个名为 myexec 的可执行文件其中RPATH指向/home/test/lib/。但是,当我在 myexec 上执行 ldd 时,它说找不到 libTest.so,我必须 export LD_LIBRARY_PATH > 为了让它工作。

我已确认:

  1. libTest.so 确实存在于 /home/test/lib/
  2. objdump -x myexec | grep RUNPATH 确实表明 /home/test/lib/ 已内置到可执行文件中。

有人知道为什么在不设置LD_LIBRARY_PATH的情况下找不到库吗?

最佳答案

来自here :

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories

作为提示,您应该小心不要完全替换您的 LD_LIBRARY_PATH。它可能会弄乱系统共享库的路径。 始终使用export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/path .

编辑1:

至于rpath ,确保libTest.so不依赖于任何其他库。作为替代方案,您可以使用 -rpath-link gcc 中的选项而不是 -rpath ,如所见here :

-rpath-link DIR

When using ELF or SunOS, one shared library may require another. This happens when an ld -shared link includes a shared library as one of the input files. When the linker encounters such a dependency when doing a non-shared, non-relocateable link, it will automatically try to locate the required shared library and include it in the link, if it is not included explicitly. In such a case, the -rpath-link option specifies the first set of directories to search.

编辑2:

如果您需要更多帮助,请提供:

  1. rpath 的精确参数编译期间使用
  2. ldd /path/to/binary 的输出命令
  3. readelf -d /path/to/binary 的输出

关于在可执行文件中找不到 C++ 动态链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290222/

相关文章:

c++ - 函数地址不是实际的代码地址

c - 在 C 文件中重新声明不透明结构

c++ - 如果不太可能出现硬崩溃错误,我应该使用吗?

linux - gcc-4.7.2的先决条件 : ppl-0. 11 找不到gmp-4.3.2

c++ - 混合 C/C++ 库

c++ - make_shared() 在给它一个已经创建的对象时调用析构函数两次

c++ - 将输出为速度测试测验的 C/C++ 程序

c++ - 您能否根据是否使用返回值来保证不同的生命周期行为?

c - C 编程中使用 strstr 函数时指针的使用

检查数字中的偶数或奇数 `1` 位