How to build a shared library (.so) without hardcoded full dependency paths?

-Wl,-rpath,~/deps/A/lib:~/deps/B/lib:~/dev/MyApp/bin

This linker option is responsible for saving the path inside the library. You need somehow to remove this.

See with ./configure --help if there's some option that could influence this. Another option is to edit manually the makefile and remove this option.

== edit2 == One more thing

-L~/deps/A/lib -L~/deps/B/lib ~/deps/A/lib/libA.so ~/deps/B/lib/libB.so

If libA.so and libB.so don't have SONAME, linking them like "~/deps/A/lib/libA.so" will also cause saving the path. Soname is set using -Wl,-soname,<soname> linker option when building shared library.

If soname is set in the shared library, linking it using "~/deps/A/lib/libA.so" form is ok.

Like Jan mentioned in the comments, the better way is using "-Llibrary/path -llibrary_name" without rpath.

-L~/deps/A/lib -L~/deps/B/lib -lA -lB

You have to use --prefix value that will be valid in the runtime environment for both packages!

Than you override prefix or DESTDIR (prefix replaces the prefix, DESTDIR is prepended to it, but works more reliably) on the make command-line when installing. Like:

~/dev/A$ ./configure
~/dev/A$ make 
~/dev/A$ make install prefix=~/dev/A-install
~/dev/B$ ./configure --with-A=~/dev/A-install
~/dev/B$ make
~/dev/B$ make install prefix=~/dev/B-install

or (which is preferred and is how all package-building tools use it):

~/dev/A$ ./configure
~/dev/A$ make 
~/dev/A$ make install DESTDIR=~/dev/A-install
~/dev/B$ ./configure --with-A=~/dev/A-install/usr/local
~/dev/B$ make
~/dev/B$ make install prefix=~/dev/B-install

because this way you are installing to ~/dev/A-install/$prefix, so with default prefix ~/dev/A-install/usr/local. The advantage of this later option is, that if you redefine some specific installation paths without refering to prefix (say --sysconfdir=/etc), DESTDIR will still get prepended to it, while it won't be affected by prefix.


When I run ldd libB.so it gives:

libA.so.2 => /home/alex/dev/A-install/lib/libA.so.2

The low-level solution to this problem is to use the option "-soname=libA.so" when you link the libA.so library. By having SONAME defined for a shared object, the linker will not embed absolute paths when linking against that shared object.

The OP is using "configure", so this isn't an easy solution to implement unless he is willing to go into the bowels of the Makefile generated by the configure script.