RPATH for Dummies

2 minute read.

This is the blog post I wish I had 5 hours ago, here to hopefully save someone else some time.

rpath

MacOS allows embedding paths to dependencies to shared libraries in executables and shared libraries.

List paths with:

otool -L <path-to-the-binary>

Modify the rpath with:

install_name_tool -change <old-path> <new-path> <path-to-the-binary>

-change will keep possible other metadata for version range, which would otherwise be removed with install_name_tool -delete_rpath <old-path> -add_rpath <new-path> <path-to-the-binary>.

Additionally the executable can contain a list of “run paths”/rpaths, that are searched if the path to a shared library contains @rpath.

Special: @loader_path, @rpath, @executable_path

Summarizing this wonderful blog post: “@executable path, @load path and @rpath” 1

@loader_path specifies the path of the loading binary (executable or shared library).

@executable_path specifies the path of the loading executable.

@rpath tells the runtime linker to search the “rpath”, which is a list of search paths embedded in the executable.

CMake RPATH

CMake has two sets of run paths: INSTALL_RPATH and BUILD_RPATH. For running binaries from the build directory, BUILD_RPATH is used while the install target will modify the executable and shared library rpaths to use INSTALL_RPATH.

You can override these with CMAKE_INSTALL_RPATH 2 and CMAKE_BUILD_RPATH 3.

See also “RPATH Handling” documentation of CMake 4.

Footnotes

  1. “@executable path, @load path and @rpath” Blog Post

  2. CMAKE_INSTALL_RPATH

  3. CMAIKE_BUILD_RPATH

  4. RPATH Handling