How to Link to Shared Lib from Shared Lib with Relative Path

CMake: Linking shared lib with other libs with relative path for distribution

I've made some more tests and actually, the use of SET (CMAKE_SHARED_LINKER_FLAGS "-Wl,-rpath,$ORIGIN/libs") solved the problem.

relative paths for shared libraries


How I can make the linking of the libraries relative to the parent program folder

Depends on your operating system. On Linux, this will probably work:

g++ -shared -o wrapper.so -Wl,-rpath='$ORIGIN' src/wrapper.o one.so two.so

Note: single quotes are important in above command.

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


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.

How to link a shared library with CMake with relative path

From the documentation:

By default if you don't change any RPATH related settings, CMake will link the executables and shared libraries with full RPATH to all used libraries in the build tree.

This is the behaviour you are seeing.

However, there are a number of ways to change this to match the behaviour you require.

Some examples from the above linked docs:

# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

# the RPATH to be used when installing
SET(CMAKE_INSTALL_RPATH "")

# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)

Using the above you'll likely want to set CMAKE_INSTALL_RPATH and then distribute the installed binary.

If you want to distribute from the binary in your build tree, it is also possible to bypass CMake's rpath handling and modify the rpath directly using linker flags:

set_target_properties(game PROPERTIES LINK_FLAGS "-Wl,-rpath,./")


Related Topics



Leave a reply



Submit