rpath 的正确使用(相对与绝对) [英] correct usage of rpath (relative vs absolute)

查看:15
本文介绍了rpath 的正确使用(相对与绝对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建二进制文件或库时,指定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 更改为指向 link 的路径,而不是链接 target 的路径.

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 哲学是什么?

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

使用相对路径使可执行文件在从特定目录调用时起作用,这几乎从不你想要的.例如.如果可执行文件在 /app/foo/bin/exe 中并且具有 lib/DT_RUNPATH,并且依赖库在 /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 的系统上,使用 $ORIGIN/../libDT_RUNPATH 将为您提供安装后可以运行的可执行文件任何地方并从任何目录调用,只要保留 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 的正确使用(相对与绝对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆