Reputation: 6716
When building a binary or library, specifying the rpath
, i.e.
-Wl,rpath,<path/to/lib>
tells the linker where to find the required library at runtime of the binary.
What is the UNIX philosphy regarding absolute and relative paths here? Is it better to use an absolute path so the lib can be found from everywhere? Or is it better to make it relative so copying an entire directory or renaming a higher level path won't render the binary unusable?
Using $ORIGIN
is usually the preferred way of building binaries. For libraries I like to put in the absolute path, because otherwise you won't be able to link to the library. A symbolic link will change the $ORIGIN
to point to the path of the link and not of the link target.
Upvotes: 34
Views: 24602
Reputation: 7516
From The Linux Programming Interface:
Using $ORIGIN in rpath
Suppose that we want to distribute an application that uses some of its own shared libraries, but we don’t want to require the user to install the libraries in one of the standard directories. Instead, we would like to allow the user to unpack the application under an arbitrary directory of their choice and then immediately be able to run the application. The problem is that the application has no way of determining where its shared libraries are located, unless it requests the user to set
LD_LIBRARY_PATH
or we require the user to run some sort of installation script that identifies the required directories. Neither of these approaches is desirable. To get around this problem, the dynamic linker is built to understand a special string,$ORIGIN
(or, equivalently,${ORIGIN}
), in anrpath
specification. The dynamic linker interprets this string to mean “the directory containing the application.” This means that we can, for example, build an application with the following command:
$ gcc -Wl,-rpath,'$ORIGIN'/lib ...
This presumes that at run time the application’s shared libraries will reside in the subdirectory lib under the directory that contains the application executable. We can then provide the user with a simple installation package that contains the application and associated libraries, and the user can install the package in any location and then run the application (i.e., a so-called “turn-key application”).
Upvotes: 9
Reputation: 213877
What is the UNIX philosphy regarding absolute and relative paths here?
Using relative path makes an executable that only works when invoked from a particular directory, which is almost never what you want. E.g. if the executable is in /app/foo/bin/exe
and has DT_RUNPATH
of lib/
, and a dependent library is in /app/foo/lib/libfoo.so
, then the exe
would only run when invoked from /app/foo
, and not when invoked from any other directory.
Using absolute path is much better: you can do cd /tmp; /app/foo/bin/exe
and have the executable still work. This is still however less than ideal: you can't easily have multiple versions of the binary (important during development), and you dictate to end-users where they must install the package.
On systems that support $ORIGIN
, using DT_RUNPATH
of $ORIGIN/../lib
would give you an executable what works when installed anywhere and invoked from any directory, so long as relative paths to bin/
and lib/
are preserved.
Upvotes: 29
Reputation: 126448
In the case of rpath
, it makes no sense to use a relative path, since a relative path will be relative to the current working directory, NOT relative to the directory where the binary/library was found. So it simply won't work for executables found in $PATH
or libraries in pretty much 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 having the shell interpret it as a variable, and if you try to do this in a Makefile, you need $$
to avoid having make interpret the $
as well.
Upvotes: 55