I just made a basic example of using ld's -rpath
option with $ORIGIN
here (see 2nd response for a working version). I'm trying to create an example where main.run
links to foo.so
, which in turn links to bar.so
, all using rpath
and $ORIGIN
.
The run-time file-structure is:
- project/
- lib/
- dir/
- sub/
- bar.so
- foo.so
- run/
- main.run (failing to build)
I'm building foo.so using:
g++ -c -o obj/foo.o src/foo.cpp -fPIC
g++ -shared -o lib/dir/foo.so obj/foo.o -Wl,-soname,foo.so -Wl,-rpath,'$ORIGIN/sub' -Llib/dir/sub -l:bar.so
Which builds fine. ldd lib/dir/foo.so
can even find bar.so
.
However, when I try to link main.run
to foo.so
, foo.so
can't find bar.so.
I'm building main.so using:
g++ -c -o obj/main.o src/main.cpp
g++ -o run/main.run obj/main.o -Wl,-rpath,'$ORIGIN/../lib/dir' -Llib/dir -l:foo.so
This works fine if another version of foo.so
is used that doesn't recursively link. (Uncomment lines in make.sh, in project below, to test).
However, using the normal foo.so
I'm getting this error when building main.run
:
/usr/bin/ld: warning: bar.so, needed by lib/dir/foo.so, not found (try using -rpath or -rpath-link)
So my questions are:
- Does
$ORIGIN
within foo.so resolve toproject/lib/dir
(wherefoo.so
is) orproject/run
(wheremain.run
(the executable linking it) is)?
ldd would seem to indicate that it'sproject/lib/dir
, which would seem to be the best way (although I tried assuming both). - How do I get these to link (while preserving relocatability) - preferably without using
-rpath-link
.
You can download the project here. It's as simple as I can make it. 4 short sources and a script.
After extracting, just run ./make.sh
from within project/
.
Note: I'm using -l:
. This shouldn't change anything except that the libraries are named like foo.so
instead of libfoo.so
, and lunk with -l:foo.so
instead of -lfoo
.