windows 下 Rust 使用OpenSSL
问题
在cargo.toml 中添加包依赖:
[dependencies]
openssl = { version = "0.10", features = ["vendored"] }
cargo build
之后出现以下错误:
error: failed to run custom build command for `openssl-sys v0.9.68`
Caused by:
process didn't exit successfully: `xxx\openssl-sys-aaee31d2ea6e39db\build-script-main` (exit code: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR
X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR unset
cargo:rerun-if-env-changed=OPENSSL_NO_VENDOR
OPENSSL_NO_VENDOR unset
running "perl" "./Configure" "--prefix=xxx\\target\\debug\\build\\openssl-sys-fa02faf7b01ccae6\\out\\openssl-build\\install" "no-dso" "no-shared" "no-ssl3" "no-unit-test" "no-comp" "no-zlib"
"no-zlib-dynamic" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-engine" "no-asm" "VC-WIN64A"
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }', C:\Users\xxx\.cargo\registry\src\github.com-1ecc6299db9ec823\openssl-src-111.16.0+1.1.1l\src\lib.rs:477:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
提示找不到指定文件找不到, 有点懵。按文档说法, 使用vendored
feature会自动编译源码, 并不要求系统安装openssl。
解决
一通折腾之后(包括去掉“verdored”, 使用vcpkg安装openssl都未成功), 才注意到报错信息里有这么一句:
"perl" "./Configure" "--prefix=xxx\\target\\debug\\build\\openssl-sys-fa02faf7b01ccae6\\out\\openssl-build\\install" "no-dso" "no-shared" "no-ssl3" "no-unit-test" "no-comp" "no-zlib"
说明使用perl
程序进行编译配置。查看我当前环境, 并没有安装perl程序。 从 https://strawberryperl.com/ 下载windows安装包,安装后,重启命令行终端, build成功。