Proper use of rpath (relative versus absolute)

When creating a binary file or library by specifying rpath , rpath

 -Wl,rpath,<path/to/lib> 

tells the linker where to find the library at runtime.

What is the UNIX philosophy regarding absolute and relative paths here? Is it better to use an absolute path so lib can be found everywhere? Or is it better to make it relative, so copying the entire directory or renaming a higher-level path will not make it impossible to use a binary file?

Update

Using $ORIGIN usually the preferred way to create binaries. For libraries, I like to specify the absolute path, because otherwise you will not be able to reference the library. A symbolic link will change $ORIGIN to indicate the path to the link, not the link.

+5
source share
2 answers

What is the UNIX philosophy regarding absolute and relative paths here?

Using a relative path makes an executable that only works when called from a specific directory, which is almost never needed. For instance. if the executable is located in /app/foo/bin/exe and has DT_RUNPATH of lib/ , and the dependent library is in /app/foo/lib/libfoo.so , then exe will be executed only when called from /app/foo , and not when called from any other directory.

Using the absolute path is much better: you can do cd /tmp; /app/foo/bin/exe cd /tmp; /app/foo/bin/exe and execute the executable. This, however, is not ideal: you cannot easily have multiple versions of the binary (important during development), and you determine to end users where they should install the package.

On systems supporting $ORIGIN , using DT_RUNPATH of $ORIGIN/../lib will give you an executable that works when installed anywhere and is called from any directory if relative paths to bin/ and lib/ are saved.

+5
source

In the case of rpath it makes no sense to use a relative path, since the relative path will refer to the current working directory, NOT to refer to the directory in which the binary / library was found. Therefore, it simply will not work for executable files found in $PATH or in libraries in almost any case.

Instead, you can use the $ORIGIN "special" path to have a path relative to the executable with
-Wl,-rpath,'$ORIGIN' - note that you need quotes around it to avoid interpreting the interpreter as a variable, and if you try to do this in the Makefile, you need $$ to avoid interpreting $ .

+8
source

All Articles