正确使用rpath(相对vs绝对) [英] correct usage of rpath (relative vs absolute)
问题描述
在构建二进制文件或库时,指定 rpath
,即
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.
在这里,绝对和相对路径的UNIX哲学是什么?使用绝对路径会更好吗,因此lib可以从任何地方找到?或者更好地使它成为相对的,以便复制整个目录或重命名更高级别的路径不会导致二进制不可用?
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?
使用 $ ORIGIN
通常是构建二进制文件的首选方式。对于我喜欢放在绝对路径中的图书馆,否则你将无法链接到图书馆。符号链接会将 $ ORIGIN
更改为指向链接的路径,而不是链接目标的路径。
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.
推荐答案
在这里,绝对路径和相对路径的UNIX哲学是什么? $ b
What is the UNIX philosphy regarding absolute and relative paths here?
使用相对路径创建一个可执行文件,仅当从特定目录调用时,几乎不会你想要什么。例如。如果可执行文件位于 / app / foo / bin / exe
中并且< lib /
,并且依赖库位于 /app/foo/lib/libfoo.so
中,则 exe
只会在从
/ app / foo
调用时运行,而不是从任何其他目录调用时运行。
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.
使用绝对路径好得多:你可以做 cd / tmp; / app / foo / bin / exe
并让可执行文件仍然有效。然而,这仍然不太理想:您不能轻易拥有多个版本的二进制文件(在开发过程中很重要),并且您决定向最终用户安装软件包。
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.
在支持 $ ORIGIN
的系统上,使用 DT_RUNPATH
$ ORIGIN /。 ./lib
会给你一个可执行文件,它可以在任何地方安装并从任何目录调用,只要相对路径 bin /
和 lib /
被保留。
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.
这篇关于正确使用rpath(相对vs绝对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!