我在路径 /home/test/lib/libTest.so
中有一个 .so
以及一个名为 myexec
的可执行文件其中RPATH
指向/home/test/lib/
。但是,当我在 myexec
上执行 ldd
时,它说找不到 libTest.so
,我必须 export LD_LIBRARY_PATH
> 为了让它工作。
我已确认:
libTest.so
确实存在于/home/test/lib/
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:
如果您需要更多帮助,请提供:
rpath
的精确参数编译期间使用ldd /path/to/binary
的输出命令readelf -d /path/to/binary
的输出
关于在可执行文件中找不到 C++ 动态链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290222/