OpenSSL包在Mac OS X 10.11上无法编译

rap*_*2-h 12 macos rust rust-cargo

我尝试在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
Run Code Online (Sandbox Code Playgroud)

openssl 版似乎没问题:

$ openssl version
OpenSSL 0.9.8zg 14 July 2015
Run Code Online (Sandbox Code Playgroud)

我不知道我要做什么才能使这个安装工作​​并试试Iron.

She*_*ter 18

从rust-openssl版本0.8开始,Homebrew安装的OpenSSL库将被包自动检测到,不需要设置额外的环境变量.

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


这是一个已知问题(也是这个这个),但不是箱子可以解决的问题.

快速解决方案是使用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
Run Code Online (Sandbox Code Playgroud)

如果您已经完成了一个cargo build,则需要先运行cargo clean以清除我们的一些过时的缓存信息.

如果您不想为打开的每个shell设置此项,请将其添加到shell初始化文件(如~/.bash_profile).你可以通过不对版本号进行硬编码来减少它的脆弱性:

export OPENSSL_INCLUDE_DIR=$(brew --prefix openssl)/include
export OPENSSL_LIB_DIR=$(brew --prefix openssl)/lib
Run Code Online (Sandbox Code Playgroud)

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


更长的原因由andrewtj描述:

OpenSSL没有稳定的ABI,因此出于兼容性目的,Apple已经维护了一个与早期ABI兼容的分支.他们在10.7中弃用了OpenSSL,并最终在10.11中删除了标题,以推动OS X应用程序开发人员捆绑他们自己的OpenSSL或使用他们的框架.dylib已被留下,因此尚未更新的应用程序不会中断.你仍然可以链接它们,但是这样做会打开你自己的奇怪的兼容性问题(除非你从早期的OS X版本中获取标题).

  • 我非常确定运行“brew link --force openssl”和安装的“pkg-config”将自动解决此问题。也就是说,默认情况下提供 openssl 库可能不是一个好主意,我不确定。 (2认同)
  • 好吧,在这种情况下,AFAIK`--force`只会覆盖公式中的设置,默认情况下不创建符号链接.因此,这不应该做任何危险的事情,甚至可以通过`brew unlink`恢复.但是,我不知道在`/ usr/local/lib`中提供openssl库的后果. (2认同)

ade*_*rsq 6

使用Brew这样:

brew install openssl
export OPENSSL_INCLUDE_DIR=`brew --prefix openssl`/include
export OPENSSL_LIB_DIR=`brew --prefix openssl`/lib
cargo clean
cargo build
Run Code Online (Sandbox Code Playgroud)