My dev environment contains many copies of a shared library. For dev purposes I want to pass to gcc the absolute, or relative path to the shared library; so I do not have lots of -L entries making it easy to pick up the wrong version.
e.g. gcc -o foobar foo.cpp /dependencies/fred/libbar.so
However, I do not want the path /dependencies to be used as the lookup location for the shared library to be placed in the executable. Is there an option so the command above is equivalent to:
gcc -o foobar foo.cpp -L /dependencies/fred -l bar
I am looking for something that is the opposite of -rpath, which includes a path.
CodePudding user response:
Is there an option so the command above is equivalent to
Yes: when linking libbar.so
you should set its SONAME
:
gcc -fPIC -shared -o libbar.so bar.o -Wl,--soname=libbar.so
Once you've done so, gcc -o foobar foo.cpp /dependencies/fred/libbar.so
will record the SONAME
instead of the absolute path in the executable, and you will have achieved desired behavior.
You can examine the "what's recorded in the executable" with:
readelf -d foobar | grep NEEDED
.
CodePudding user response:
A work around is to use patchelf.
Find the paths using
readelf -d foobar
replace them with patchelf
patchelf --replace-needed /dependencies/fred/libbar.so libbar.so foobar
Then use
readelf -d foobar
to confirm that the path has been replaced.