/usr/bin/ld: warning: libbar.so, needed by ./libfoo.so, not found (try using -rpath or -rpath-link)

HW Yang
1 min readNov 12, 2019

--

This warning happens when our executable try to link against libfoo.so which depends on libbar.so. The compiler, however, cannot find where libbar.so is. Therefore, it tell you to try using -rpath or -rpath-link to resolve this warning.

Let’s produce the warning in the first. When we enter

gcc -shared -fPIC -o bar/libbar.so bar/bar.c
gcc -shared -fPIC -o libfoo.so foo.c -lbar -Lbar
gcc main.c -lfoo -L.

And we will get

/usr/bin/ld: warning: libbar.so, needed by ./libfoo.so, not found (try using -rpath or -rpath-link)

Let’s try -rpath and -rpath-link…

gcc main.c -lfoo -L. -Wl,-rpath=bar/
gcc main.c -lfoo -L. -Wl,-rpath-link=bar/

Both of the two options works because we tell gcc where the libbar.so is.

The differences between -rpath and -rpath-link is that using -rpath bakes the library path into dynamic section so that LD_LIBRARY_PATH is not necessary at run time.

--

--