'Can you relink/modify relative shared library look up paths?

I am running into the following situation. Project A has libraries A1, A2, A3... That follow their own directory structure. For example:

Libaries/
|
|--Dir1/
|  |
|  |--A1.so
|  |--A2.so
|
|--Dir2/
|  |--A3.so
|  |--A4.so

In this case Project A compiles just fine. The libraries of project A are dependencies for project B and a script (that I have no control over) copies them onto B's directory in a flat hierarchy i.e this

BLibraries/
|--A1.so
|--A2.so
|--A3.so
|--A4.so

So the relative paths are no longer the same.

B loads symbols from these libraries dynamically through dlopen. In this case, If A1 needs symbols from A2, B they appear as undefined and so B fails to load A1.

Is there a way I can poke the files Ai.so and tell them "Hey that other library whose symbols you need is actually over here now"?



Solution 1:[1]

Is there a way I can poke the files Ai.so and tell them "Hey that other library whose symbols you need is actually over here now"?

You can use patchelf to do that, but you shouldn't.

Since you control how A*.so is built, you should set their RPATH so that it works "out of the box". Adding -rpath=/path/to/BLibraries to A*.so link command is probably all that's needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Employed Russian