Search code examples
c++linkerdependenciesldrpath

C++ linker options for RPATH of linked library


I have a MyApp project which is dependent of lib1.so library. lib1.so has own dependency lib2.so:

MyApp -> lib1.so -> lib2.so

I want to run MyApp with all dependencies in one directory.
For lib1.so the solution is -rpath key in linker options:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath='$ORIGIN'")

But it does not work for lib2.so - the application does not find it.
It seems lib1.so do not use rpath of application.
Is there any way to specify rpath for lib1.so?


Solution

  • Is there any way to specify rpath for lib1.so?

    A couple of options are available:

    1. Link MyApp with both lib2.so and lib1.so, so that it loads lib2.so using MyApp's rpath before loading lib1.so.
    2. patchelf --set-rpath '$ORIGIN' lib1.so to make lib1.so use $ORIGIN when looking for lib2.so.