OpenSSL crate 在 Mac OS X 10.11 上编译失败

OpenSSL crate fails compilation on Mac OS X 10.11

我尝试在 Mac OS X 10.11.2 上安装 Rust 的 Iron 框架,但是当我 运行 cargo buildcargo run 关于编译 openssl 的内容:

failed to run custom build command for `openssl-sys-extras v0.7.4`
Process didn't exit successfully: `/xxx/rust/hello/target/debug/build/openssl-sys-extras-413d6c73b37a590d/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("x86_64-apple-darwin")
OPT_LEVEL = Some("0")
PROFILE = Some("debug")
TARGET = Some("x86_64-apple-darwin")
debug=true opt-level=0
HOST = Some("x86_64-apple-darwin")
TARGET = Some("x86_64-apple-darwin")
TARGET = Some("x86_64-apple-darwin")
HOST = Some("x86_64-apple-darwin")
CC_x86_64-apple-darwin = None
CC_x86_64_apple_darwin = None
HOST_CC = None
CC = None
HOST = Some("x86_64-apple-darwin")
TARGET = Some("x86_64-apple-darwin")
HOST = Some("x86_64-apple-darwin")
CFLAGS_x86_64-apple-darwin = None
CFLAGS_x86_64_apple_darwin = None
HOST_CFLAGS = None
CFLAGS = None
running: "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-m64" "-fPIC" "-o" "/xxx/rust/hello/target/debug/build/openssl-sys-extras-413d6c73b37a590d/out/src/openssl_shim.o" "-c" "src/openssl_shim.c"
ExitStatus(Code(1))


command did not execute successfully, got: exit code: 1



--- stderr
src/openssl_shim.c:1:10: fatal error: 'openssl/hmac.h' file not found
#include <openssl/hmac.h>
     ^
1 error generated.
thread '<main>' panicked at 'explicit panic', /xxx/.cargo/registry/src/github.com-0a35038f75765ae4/gcc-0.3.21/src/lib.rs:772

openssl 版本似乎还可以:

$ openssl version
OpenSSL 0.9.8zg 14 July 2015

我不知道我必须做什么才能使此安装工作并尝试使用 Iron。

从 rust-openssl 版本 0.8 开始,Homebrew-installed OpenSSL 库将被 crate 自动检测,无需设置额外的环境变量。

如果您需要支持之前的版本或选择不使用 Homebrew,请继续阅读。


这是一个 known issue (also this and this),但不是板条箱可以修复的。

快速解决方案是使用 Homebrew 安装 OpenSSL,然后通过设置 OPENSSL_INCLUDE_DIROPENSSL_LIB_DIR 环境变量明确指向找到 OpenSSL 的目录:

OPENSSL_INCLUDE_DIR=/usr/local/Cellar/openssl/1.0.2e/include \
OPENSSL_LIB_DIR=/usr/local/Cellar/openssl/1.0.2e/lib \
cargo build

如果您已经完成了 cargo build,您需要先 运行 cargo clean 清除我们一些陈旧的缓存信息。

如果您不想为您打开的每个 shell 设置它,请将它添加到您的 shell 初始化文件(如 ~/.bash_profile)。您可以通过不 hard-coding 版本号来使其不那么脆弱:

export OPENSSL_INCLUDE_DIR=$(brew --prefix openssl)/include
export OPENSSL_LIB_DIR=$(brew --prefix openssl)/lib

如果您不想使用 Homebrew,请遵循相同的过程,但使用您的 OpenSSL 副本的适当路径。


长的理由好described by andrewtj:

OpenSSL doesn't have a stable ABI so for compatibility purposes Apple have maintained a fork that's compatible with one of the earlier ABIs. They deprecated OpenSSL in 10.7 and finally dropped the headers in 10.11 to push OS X app developers toward bundling their own OpenSSL or using their frameworks. The dylibs have been left around so apps that haven't been updated don't break. You can still link against them but you're opening yourself up to odd compatibility issues by doing so (unless you grab the headers from an earlier OS X release).

如果您安装了自制软件的 openssl,只需将以下内容添加到您的 Cargo.toml:

[target.x86_64-apple-darwin.openssl-sys]
rustc-link-search = [ "/usr/local/opt/openssl/lib" ]
rustc-link-lib = [ "ssl", "crypto" ]
include = [ "/usr/local/opt/openssl/include" ]

然后是cargo clean && cargo build。不要通过在库加载路径中引入不兼容的 openssl 来破坏 OS X,并且当你想要构建时不要忘记 set/unset 环境变量(或者当不在 Rust 上工作时污染你的 shell env东西)。总而言之,这是一种更快乐、更少愤怒的方法。

我无法将此答案添加到 my own question where it belongs (because it depends on homebrew), because Shepmaster 决定应该关闭,但我会在这里回答 link 那个问题。

Brew这样使用:

brew install openssl
export OPENSSL_INCLUDE_DIR=`brew --prefix openssl`/include
export OPENSSL_LIB_DIR=`brew --prefix openssl`/lib
cargo clean
cargo build

对 MacPorts 的回答:

sudo port install openssl
export OPENSSL_INCLUDE_DIR=/opt/local/include
export OPENSSL_LIB_DIR=/opt/local/lib
cargo clean
cargo build