CMake and RPATH

From K-3D

Jump to: navigation, search


When loading executables on GNU/Linux and other Posix systems, the runtime linker looks for shared libraries in several locations:

  • Directories specified by an executable's RPATH
  • Directories specified by the LD_LIBRARY_PATH environment variable
  • Directories specified by /etc/

An RPATH is a directory (or list of directories) that is linked directly into an executable's binary.

By default, cmake creates binaries with their RPATH pointing to the appropriate places in the build tree, so they can be run without being installed. When you do an install, all of the executables are relinked so that they contain their installation RPATH. By default, the installation RPATH is empty, so you have to modify /etc/ or set LD_LIBRARY_PATH. This isn't normally an issue for end-users, because /etc/ is automatically updated by packaging systems like rpm or portage.

You can use the scanelf utility to see the differences:

# Scan the built binary ...
$ scanelf -r k3d-build/bin/k3d
ET_EXEC /home/tshead/k3d-build/lib bin/k3d

# Scan the installed binary ...
$ scanelf -r /usr/local/k3d/bin/k3d
ET_EXEC   -   /usr/local/k3d/bin/k3d

For Developers

It's possible to explicitly specify the RPATH during installation, although there are arguments against doing it (which is why the default behavior works the way it does). For developers, the recommended procedure is to use the default cmake behavior and run K-3D from the build tree for development / testing. If you install K-3D, you will have to configure /etc/ or LD_LIBRARY_PATH to find the shared libraries, just like your users would.

For Distributors / Users

If you are a distributor or a user, you probably just want to build and install K-3D without relinking. To avoid the relinking step, set CMAKE_SKIP_RPATH "ON" when you configure the build. Your K-3D binaries will be built without any RPATH and "make install" won't have to spend time relinking. After installing the software, you may have to configure /etc/ or set LD_LIBRARY_PATH to run the program.

Personal tools