Lookup failure when linking using -rpath and $ORIGIN

旧巷老猫 提交于 2019-12-11 09:17:52

问题


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.

However, when I run the executable it can't find the shared object unless I run it from within $ORIGIN.

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

Executing from within dll_test works fine. Executing from elsewhere (/make/test) gives the error:

dll_test/main.run: error while loading shared libraries: lib/foo.so: cannot open shared object file: No such file or directory

I'm using -l:foo.so instead of -lfoo, but this shouldn't affect anything (I hope).



SOURCE FILES

dll_test/src/foo.cpp


int foo()
    { return 1; }

dll_test/src/main.cpp


int foo();

#include <iostream&gt


int main()
  {
    std::cout &lt&lt foo() &lt&lt std::endl;
  }


BUILD SCRIPT

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



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".

Running "main.run" from within dll_test should work (prints 1).
Running "main.run" from within dll_test fails. Why?

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' ?


回答1:


You need -Wl,-rpath,'$ORIGIN/lib' instead of just '$ORIGIN'.

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 :)



来源:https://stackoverflow.com/questions/6288206/lookup-failure-when-linking-using-rpath-and-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!