Marc
Marc

Reputation: 469

No embedded path for shared library when linking with an absolute path

My development environment contains many copies of a shared library. For development purposes I want to pass the absolute, or relative path to the shared library to GCC; 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 the following?

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.

Upvotes: 1

Views: 648

Answers (2)

Marc
Marc

Reputation: 469

A workaround 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.

Upvotes: 1

Employed Russian
Employed Russian

Reputation: 213375

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.

Upvotes: 1

Related Questions