Search code examples
c++ccmakeshared-librariesrpath

Rpath/Runpath handle with dependent shared libraries in CMAKE


I will put you in context:

I have 2 third party shared libraries: libA.so and libB.so. My program contains only calls to libA.so symbols. The libA.so internaly needs to call to libB.so. If I readelf LibA.so it has a RunPath pointing to a path from the third party developer that doesn't exsist in my system.

My program builds with cmake. I use find_library(A) and find_libary(B) and then add them to my program executable target with target_link_libraries(my_executable PUBLIC A B)

With this setup the generated executable contains a correct RUNPATH for both libA.so and libB.so. The problem is that when running the executable the libB.so file is not found.

I need to keep the .so files in the place they are. Setting up LD_LIBRARY_PATH env variable or moving to a ldd valid folder is not an option. I have tried these solutions and they work btw.

How could I make the executable to find libB.so in this case.

Thanks in advance.


Solution

  • You can change RPATH with a tool named patchelf:

    sudo apt install patchelf
    patchelf --set-rpath \$ORIGIN libMyLibrary.so
    

    Note that $ORIGIN here means "search in the folder where this library located". You can also combine values like this: \$ORIGIN:\$ORIGIN/../lib. This is convenient if you want to keep unix-like installation structure (executables in bin, libraries in lib).