使用-rpath和$ ORIGIN链接时查找失败 [英] Lookup failure when linking using -rpath and $ORIGIN
问题描述
我正在尝试学习如何将GCC链接器(ld)中的-rpath
选项与$ORIGIN
一起使用.
I'm trying to learn how to use the -rpath
option in GCC's linker (ld) with $ORIGIN
.
我正在尝试我能想到的最简单的示例(请参阅下文),并且我阅读的所有链接似乎都表明我做得正确.
I'm trying the simplest example I can think of (see below), and all the links I read seem to say I'm doing it correctly.
但是,当我运行可执行文件时,除非从$ORIGIN
内部运行共享对象,否则找不到共享对象.
However, when I run the executable it can't find the shared object unless I run it from within $ORIGIN
.
在可执行文件(main.run)上使用readelf -d显示:
Using readelf -d on the executable (main.run) shows:
0x0000000000000001 (NEEDED) Shared library: [lib/foo.so]
...
0x000000000000000f (RPATH) Library rpath: [$ORIGIN]
...
文件结构(相关文件)为:
The file structure (relevant files) is:
-
/make/test/dll_test/
-
main.run
-
lib/
-
foo.so
/make/test/dll_test/
main.run
lib/
foo.so
从dll_test内部执行可以正常工作. 从其他地方执行(/make/test)会出现错误:
Executing from within dll_test works fine. Executing from elsewhere (/make/test) gives the error:
dll_test/main.run:加载时出错 共享库:lib/foo.so:不能 打开共享对象文件:无此文件 或目录
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
,但这不应该有任何影响(我希望).I'm using
-l:foo.so
instead of-lfoo
, but this shouldn't affect anything (I hope).
int foo() { return 1; }
dll_test/src/main.cpp
int foo(); #include <iostream> int main() { std::cout << foo() << std::endl; }
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".
To build, create these files in their respective locations and simply run "sh make.sh" from within dll_test (or wherever the project root is). It should generate "dll_test/main.run".
从dll_test中运行"main.run"应该可以工作(打印1).
从dll_test中运行"main.run"失败.为什么?Running "main.run" from within dll_test should work (prints 1).
Running "main.run" from within dll_test fails. Why?此外,foo.so的路径以[lib/foo.so]的形式存储在main.run中.我可以将其设为[foo.so],以便使用-Wl,-rpath,'$ ORIGIN/lib'吗?
Also, foo.so's path stored in main.run as [lib/foo.so]. Can I get it to be [foo.so] so I can use the -Wl,-rpath,'$ORIGIN/lib' ?
推荐答案
您需要
-Wl,-rpath,'$ORIGIN/lib'
而不是'$ORIGIN'
.edit:您实际上是在输入
-l:foo.so
吗?似乎很奇怪...您应该对-Wl,-rpath,'$ORIGIN/lib' -Llib -lfoo.so
没问题.如果这不起作用,请将-Wl,-soname,libfoo.so
添加到共享库的链接器命令中.我不太肯定这会解决"lib/foo.so"问题,但是值得一试:)edit: Are you actually entering
-l:foo.so
? That seems odd... you should be just fine with-Wl,-rpath,'$ORIGIN/lib' -Llib -lfoo.so
. If that doesn't work, add-Wl,-soname,libfoo.so
to the linker command for your shared library. I'm not positive that this will fix the "lib/foo.so" issue, but it's worth a shot :)这篇关于使用-rpath和$ ORIGIN链接时查找失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
-