为什么不在此处传播 DYLD_LIBRARY_PATH?

Why isn't DYLD_LIBRARY_PATH being propagated here?

我有一个简单的 C++ 程序,我正在尝试 运行 链接到我之前构建的 Boost.Thread 库的一个版本。我似乎无法理解 运行time 库路径在 OS X 上的行为方式。

由于我的 Boost 库没有 RPATH-relative install name,我使用 DYLD_LIBRARY_PATH 环境变量来告诉动态链接器在 [=37= 处找到 libboost_thread.dylib ]时间。

如果我 运行 程序直接在我的 (bash) shell:

[~/git/project]$ echo $DYLD_LIBRARY_PATH
/Users/jasonr/git/project/boost/lib
[~/git/project]$ .sconf_temp/conftest_7
[~/git/project]$ # Program runs successfully; this is what I expect.

但是,该程序 运行 作为我正在使用的 autoconf 类框架进行的一系列测试的一部分。它 运行 是 child shell 中使用 sh -c 的程序。如果我尝试这样做,会发生以下情况:

[~/git/project]$ # Make sure the environment variable is exported to child shells.
[~/git/project]$ export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH
[~/git/project]$ # Try to run it in a child shell.
[~/git/project]$ sh -c .sconf_temp/conftest_7
dyld: Library not loaded: libboost_thread.dylib
  Referenced from: /Users/jasonr/git/project/.sconf_temp/conftest_7
  Reason: image not found
Trace/BPT trap: 5

在这种情况下,好像环境变量没有传播到 dyld。为什么会出现这种情况?我更熟悉 LD_LIBRARY_PATH 在 Linux 上的行为,(我认为)应该适用于上面的示例。为了完成这项工作,我还需要做些什么吗?

据推测,您是 运行 El Capitan (OS X 10.11) 或更高版本。这是系统完整性保护的副作用。来自 System Integrity Protection Guide: Runtime Protections 文章:

When a process is started, the kernel checks to see whether the main executable is protected on disk or is signed with an special system entitlement. If either is true, then a flag is set to denote that it is protected against modification. …

… Any dynamic linker (dyld) environment variables, such as DYLD_LIBRARY_PATH, are purged when launching protected processes.

所有系统提供的解释器,包括 /bin/sh,都以这种方式受到保护。因此,当您调用 sh 时,将清除所有 DYLD_* 环境变量。

您可以编写 shell 脚本来设置 DYLD_LIBRARY_PATH 然后执行 .sconf_temp/conftest_7。您可以使用 shell 解释器来执行它——实际上,您必须这样做——并且环境变量会很好,因为清除发生在受保护的可执行文件启动时。基本上,这种方法类似于您问题中的工作示例,但封装在 shell 脚本中。