TExcelTAD
Home › Modern CMake (with CTest & CPack) › RPATH for Installed Binaries

RPATH for Installed Binaries

A program linked against a shared library has to find that library when it starts. On Linux the dynamic loader searches, in order: the program's DT_RUNPATH (or the older DT_RPATH), LD_LIBRARY_PATH, the system cache (/etc/ld.so.cache) and the default directories (/lib, /usr/lib). Libraries in /usr/local/lib, /opt/... or your own install prefix are found only if something points there.

CMake manages the RUNPATH for you, with one surprise: the build tree and the install tree get different RUNPATHs.

The problem in one session

bash · try it yourself
mkdir -p greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet LANGUAGES CXX)
include(GNUInstallDirs)
add_library(greet SHARED greet.cpp)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE greet)
install(TARGETS greet app)
CM
echo 'const char* greet() { return "hello from libgreet"; }' > greet.cpp
cat > main.cpp <<'SRC'
#include <cstdio>
const char* greet();
int main() { std::puts(greet()); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null
echo "--- build tree"
readelf -d build/app | grep RUNPATH
./build/app
echo "--- install tree"
cmake --install build --prefix "$HOME/opt/greet" > /dev/null
readelf -d ~/opt/greet/bin/app | grep RUNPATH || echo "(no RUNPATH)"
~/opt/greet/bin/app
echo "exit code: $?"
--- build tree
 0x000000000000001d (RUNPATH)            Library runpath: [/home/learner/greet/build:]
hello from libgreet
--- install tree
(no RUNPATH)
/home/learner/opt/greet/bin/app: error while loading shared libraries: libgreet.so: cannot open shared object file: No such file or directory
exit code: 127

In the build tree, CMake added the absolute path of the build directory so the program runs during development. On install, CMake removed it: an installed binary must not depend on the build directory, which might be deleted, or belong to another machine. With no RUNPATH, the loader looks only in the system directories and fails.

The fix: a relative RUNPATH with $ORIGIN

$ORIGIN in a RUNPATH means "the directory the executable is in". An install tree with bin/ and lib/ side by side needs $ORIGIN/../lib, which works wherever the tree is copied: the install is relocatable.

bash · try it yourself
mkdir -p greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet LANGUAGES CXX)
include(GNUInstallDirs)

# Installed binaries find libraries in ../lib relative to themselves.
file(RELATIVE_PATH rel_lib
  "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}"
  "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH "$ORIGIN/${rel_lib}")

add_library(greet SHARED greet.cpp)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE greet)
install(TARGETS greet app)
CM
echo 'const char* greet() { return "hello from libgreet"; }' > greet.cpp
cat > main.cpp <<'SRC'
#include <cstdio>
const char* greet();
int main() { std::puts(greet()); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null
cmake --install build --prefix "$HOME/opt/greet" > /dev/null
readelf -d ~/opt/greet/bin/app | grep RUNPATH
~/opt/greet/bin/app
echo "--- move the whole tree somewhere else"
mv ~/opt/greet ~/elsewhere && ~/elsewhere/bin/app
 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../lib]
hello from libgreet
--- move the whole tree somewhere else
hello from libgreet

During the install CMake rewrote the RUNPATH inside the installed copy of app (the build-tree copy keeps its build-directory path). The tree still works after being moved, because nothing in it refers to an absolute path.

$ORIGIN must reach the linker literally: in CMake code write it inside a quoted string as shown; CMake escapes it correctly for the build tool.

The knobs

Variable / property Effect
CMAKE_INSTALL_RPATH / INSTALL_RPATH the RUNPATH written into installed binaries
CMAKE_INSTALL_RPATH_USE_LINK_PATH also add the directories of linked libraries outside the project (useful for libraries in /opt found with find_package)
CMAKE_BUILD_WITH_INSTALL_RPATH use the install RPATH already in the build tree (no rewrite at install time)
CMAKE_SKIP_RPATH / CMAKE_SKIP_INSTALL_RPATH no RPATH at all (common for distribution packages that install into /usr/lib)
BUILD_RPATH extra build-tree entries

Other platforms

  • macOS uses install names and @rpath/@loader_path instead of $ORIGIN. CMake sets MACOSX_RPATH on by default; use @loader_path/../lib in INSTALL_RPATH.
  • Windows has no RPATH: DLLs are found next to the executable, in the system directories, or on PATH. That's why Windows projects install DLLs to bin/ (the RUNTIME destination), and install(TARGETS … RUNTIME_DEPENDENCIES …) or
    lt;TARGET_RUNTIME_DLLS:app>
    can copy third-party DLLs next to the program.

Test yourself

Why does an installed binary lose the RUNPATH it had in the build tree?

  1. A CMake bug
  2. CMake removes build-directory paths on install so the installed binary doesn't depend on the build tree
  3. The linker strips it
  4. Because of CMAKE_BUILD_TYPE

What does $ORIGIN/../lib mean in a RUNPATH?

  1. The source directory's lib
  2. The lib directory next to the directory containing the executable
  3. /usr/lib
  4. The current working directory's ../lib

On Windows, where must a program's DLLs usually be installed?

  1. In lib/
  2. Next to the executable in bin/ (the RUNTIME destination), or on PATH
  3. In the RPATH
  4. Anywhere; Windows searches the whole disk