可以在 Mac OS X 上使用 DYLD_LIBRARY_PATH 吗?还有,它的动态库搜索算法是什么?
我读到一些文章不鼓励使用 DYLD_LIBRARY_PATH,因为动态库的路径应该使用 -install_name、@rpath 和 @loader_path 来修复。
就制作一个同时在 Linux 和 Mac OS X 上运行的程序而言,Mac OS X 的 DYLD_LIBRARY_PATH 与 Linux 的 LD_LIBRARY_PATH 完全相同。而且,我们可以共享(几乎)相同的没有 -install_name 和 @rpath 的 make 文件。
- 在 Mac OS X 上使用 DYLD_LIBRARY_PATH 可以吗?
- 当二进制文件找不到动态库时,Mac OS X 的动态库搜索算法是什么?当前目录-> DYLD_LIBRARY_PATH 目录...?
I read some articles discouraging of the use of DYLD_LIBRARY_PATH, as the the path of dynamic library should be fixed using -install_name, @rpath, and @loader_path.
In terms of making a program that runs both on Linux and Mac OS X, DYLD_LIBRARY_PATH of Mac OS X does exactly what LD_LIBRARY_PATH of Linux. And, we can share (almost) the same make file that doesn't have the -install_name and @rpath.
- Is this OK to use DYLD_LIBRARY_PATH on Mac OS X?
- What's the dynamic library search algorithm with Mac OS X when the binary can't find the dynamic library? current directory -> DYLD_LIBRARY_PATH directories ... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所注意到的,
DYLD_LIBRARY_PATH
的行为类似于其他 *nix 上的LD_LIBRARY_PATH
。但是,您应该查看另一个名为 DYLD_FALLBACK_LIBRARY_PATH 的环境变量。一般来说,这些(在 osx 和 linux 上)仅建议用于开发用途,因为当您使用不具有相同符号表的库覆盖时,它们可能会导致符号查找错误。一个很好的例子是当您尝试使用自定义安装覆盖 VecLib(例如 blas lapack)的默认安装时。如果设置了 DYLD_LIBRARY_PATH,这将导致链接到系统 VecLib 的应用程序中出现符号未找到错误,如果未设置,则相反(自定义应用程序中的符号查找错误)。这是由于系统 blas/lapack 不是 ATLAS 库的完整实现。
DYLD_FALLBACK_LIBRARY_PATH
不会产生这些问题。将库安装到非标准位置时,
DYLD_FALLBACK_LIBRARY_PATH
更加理智。这将在默认路径中提供的库中查找符号,如果在那里找不到该符号,则回退到指定的路径。好处是,此过程不会在针对默认库编译的应用程序中导致符号查找错误。
一般来说,当库安装到非标准位置时,应该指定绝对路径,这可以消除动态查找的歧义。
As you've noted,
DYLD_LIBRARY_PATH
behaves likeLD_LIBRARY_PATH
on other *nix. However, there is another environment variable you should look at calledDYLD_FALLBACK_LIBRARY_PATH
.In general, these are (both on osx and linux) suggested only for development use as they can cause symbol lookup errors when you override with a library that does not have the same symbol table. A good example of this is when you attempt to override the default install of VecLib (e.g. blas lapack) with a custom install. This will cause symbol not found errors in applications linked to the system VecLib if
DYLD_LIBRARY_PATH
is set and the reverse (symbol lookup errors in custom applications) if it is not. This is due to the system blas/lapack not being a full implementation of the ATLAS libs.DYLD_FALLBACK_LIBRARY_PATH
will not produce these problems.When installing libraries to a non-standard location,
DYLD_FALLBACK_LIBRARY_PATH
is much more sane. This will look for symbols in libraries provided in the default paths and if the symbol is not found there, fall back to the specified path.The benefit is that this process will not cause symbol lookup errors in applications compiled against the default libraries.
In general, when libraries are installed to non-standard locations absolute paths should be specified which negates the ambiguity of the dynamic lookup.
DYLD_LIBRARY_PATH
的行为与LD_LIBRARY_PATH
不同。 OS Xdlopen
文档 (https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/dlopen.3.html)指定提供绝对路径时,会首先查找在 DYLD_LIBRARY_PATH 指定的位置:换句话说,如果将
DYLD_LIBRARY_PATH
设置为/Hello
,则以下两个dlopen
调用:都将解析为
/Hello/libfoo .so
.这是相当违反直觉的,并且代表了一个安全漏洞。使用 dlopen 的软件无法保证它加载正确的库(也许在其自己的环境中覆盖 DYLD_LIBRARY_PATH ?)DYLD_LIBRARY_PATH
does not behave likeLD_LIBRARY_PATH
. The OS Xdlopen
documentation (https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/dlopen.3.html) specifies that when providing an absolute path, it will first look in locations specified byDYLD_LIBRARY_PATH
:In other words, if you set
DYLD_LIBRARY_PATH
to/Hello
, the following twodlopen
calls:will both resolve to
/Hello/libfoo.so
. This is quite counter-intuitive, and represents a security vulnerability. Software usingdlopen
has no way to guarantee it is loading the correct libraries (perhaps overrideDYLD_LIBRARY_PATH
in its own environment?)有关动态链接编辑器的环境变量以及它们如何影响动态库搜索的文档,
man dyld
。For documentation on the dynamic link editor's environment variables and how they affect the search for dynamic libraries,
man dyld
.