我正在尝试学习如何在 GCC 的链接器 (ld) 中使用 -rpath
选项和 $ORIGIN
。
我正在尝试我能想到的最简单的例子(见下文),我阅读的所有链接似乎都在说我做的是正确的。
但是,当我运行可执行文件时,它无法找到共享对象,除非我从 $ORIGIN
中运行它。
在可执行文件 (main.run) 上使用 readelf -d 显示:
0x0000000000000001 (NEEDED) Shared library: [lib/foo.so]
...
0x000000000000000f (RPATH) Library rpath: [$ORIGIN]
...
文件结构(相关文件)为:
/make/test/dll_test/
main.run
库/
foo.so
从 dll_test 中执行工作正常。 从其他地方 (/make/test) 执行会给出错误:
dll_test/main.run: error while loading shared libraries: lib/foo.so: cannot open shared object file: No such file or directory
我正在使用 -l:foo.so
而不是 -lfoo
,但这应该不会影响任何东西(我希望)。
源文件
dll_test/src/foo.cpp
int foo()
{ return 1; }
dll_test/src/main.cpp
int foo();
#include <iostream>
int main()
{
std::cout << foo() << std::endl;
}
构建脚本
dll_test/make.sh
mkdir -p -v obj
mkdir -p -v lib
g++ -c -o obj/foo.o src/foo.cpp -fPIC
g++ -shared -o lib/foo.so obj/foo.o
g++ -c -o obj/main.o src/main.cpp
g++ -o main.run obj/main.o -Wl,-rpath,'$ORIGIN' -Llib -l:foo.so
要构建,请在各自的位置创建这些文件,然后从 dll_test(或项目根所在的任何位置)运行“sh make.sh”。 它应该生成“dll_test/main.run”。
从 dll_test 中运行“main.run”应该可以工作(打印 1)。
从 dll_test 中运行“main.run”失败。为什么?
此外,foo.so 的路径在 main.run 中存储为 [lib/foo.so]。我可以将它设为 [foo.so] 以便我可以使用 -Wl,-rpath,'$ORIGIN/lib' 吗?
最佳答案
您需要 -Wl,-rpath,'$ORIGIN/lib'
而不仅仅是 '$ORIGIN'
。
编辑:您实际上是在输入 -l:foo.so
吗?这似乎很奇怪……您应该可以使用 -Wl,-rpath,'$ORIGIN/lib' -Llib -lfoo.so
。如果这不起作用,请将 -Wl,-soname,libfoo.so
添加到共享库的链接器命令中。我不确定这会解决“lib/foo.so”问题,但值得一试:)
关于gcc - 使用 -rpath 和 $ORIGIN 链接时查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6288206/