You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

依赖OpenSSL的Rust项目编译错误求助

Windows下Rust项目依赖OpenSSL编译失败问题

环境信息

  • 系统:Windows 10 x64
  • Rust版本:1.68.0
  • Cargo.toml配置:
reqwest = "0.11.14"
  • 尝试操作:使用vcpkg安装OpenSSL 3.0.8和1.1.1t版本,均未解决编译错误

编译错误信息

PS C:\Users\user\Desktop\WebCheckRs> cargo run
   Compiling openssl-sys v0.6.7
   Compiling mio v0.6.23
   Compiling url v0.2.38
   Compiling futures v0.3.26
error: failed to run custom build command for `openssl-sys v0.6.7`

Caused by:
  process didn't exit successfully: `C:\Users\user\Desktop\WebCheckRs\target\debug\build\openssl-sys-696ce7e201a8fc02\build-script-build` (exit code: 101)
  --- stdout
  cargo:rustc-link-search=native=C:\Program Files\vcpkg\installed\x64-windows\lib
  cargo:rustc-link-lib=dylib=ssl32
  cargo:rustc-link-lib=dylib=eay32
  cargo:include=C:\Program Files\vcpkg\installed\x64-windows\include
  TARGET = Some("x86_64-pc-windows-msvc")
  OPT_LEVEL = Some("0")
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  TARGET = Some("x86_64-pc-windows-msvc")
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  CC_x86_64-pc-windows-msvc = None
  CC_x86_64_pc_windows_msvc = None
  HOST_CC = None
  CC = None
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  CFLAGS_x86_64-pc-windows-msvc = None
  CFLAGS_x86_64_pc_windows_msvc = None
  HOST_CFLAGS = None
  CFLAGS = None
  DEBUG = Some("true")
  running: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.35.32215\\bin\\HostX64\\x64\\cl.exe" "/nologo" "/MD" "/Z7" "/I" "C:\\Program Files\\vcpkg\\installed\\x64-windows\\include" "/W4" "/FoC:\\Users\\user\\Desktop\\WebCheckRs\\target\\debug\\build\\openssl-sys-ea090d7f600d0dee\\out\\src/openssl_shim.o" "/c" "src/openssl_shim.c"
  openssl_shim.c
  src/openssl_shim.c(68): warning C4996: 'HMAC_Init_ex': Since OpenSSL 3.0
  src/openssl_shim.c(72): warning C4996: 'HMAC_Update': Since OpenSSL 3.0
  src/openssl_shim.c(76): warning C4996: 'HMAC_Final': Since OpenSSL 3.0
  src/openssl_shim.c(91): warning C4244: 'return': conversion from 'uint64_t' to 'long', possible loss of data
  src/openssl_shim.c(95): warning C4244: 'return': conversion from 'uint64_t' to 'long', possible loss of data
  src/openssl_shim.c(99): warning C4244: 'return': conversion from 'uint64_t' to 'long', possible loss of data
  src/openssl_shim.c(115): warning C4100: 'ctx': unreferenced formal parameter
  src/openssl_shim.c(126): error C2037: left of 'p' specifies undefined struct/union 'dh_st'
  src/openssl_shim.c(127): error C2037: left of 'g' specifies undefined struct/union 'dh_st'
  src/openssl_shim.c(128): error C2037: left of 'q' specifies undefined struct/union 'dh_st'
  src/openssl_shim.c(123): warning C4996: 'DH_new': Since OpenSSL 3.0
  src/openssl_shim.c(137): error C2037: left of 'cert_info' specifies undefined struct/union 'x509_st'
  src/openssl_shim.c(137): error C2037: left of 'cert_info' specifies undefined struct/union 'x509_st'
  src/openssl_shim.c(137): warning C4033: 'X509_get_extensions_shim' must return a value
  exit code: 2

  --- stderr
  thread 'main' panicked at '

  Internal error occurred: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.35.32215\\bin\\HostX64\\x64\\cl.exe" "/nologo" "/MD" "/Z7" "/I" "C:\\Program Files\\vcpkg\\installed\\x64-windows\\include" "/W4" "/FoC:\\Users\\user\\Desktop\\WebCheckRs\\target\\debug\\build\\openssl-sys-ea090d7f600d0dee\\out\\src/openssl_shim.o" "/c" "src/openssl_shim.c" with args "cl.exe" did not execute successfully (status code exit code:2).

  ', C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\gcc-0.3.55\src\lib.rs:1672:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
解决方案

错误根源是openssl-sys v0.6.7版本过旧,和你安装的OpenSSL 3.x/1.1.1版本存在API兼容问题,以下是三种解决方法:

方法一:升级依赖版本

直接更新reqwest到兼容新版openssl-sys的稳定版本:
修改Cargo.toml:

reqwest = "0.12"

执行命令更新依赖并编译:

cargo update
cargo run

新版reqwest会自动适配支持OpenSSL 3.x的openssl-sys版本,解决结构体定义不匹配的问题。

方法二:指定OpenSSL 1.1.1并配置vcpkg

若不想升级reqwest,可通过vcpkg安装兼容旧版openssl-sys的OpenSSL 1.1.1:

  1. 卸载已安装的OpenSSL:
vcpkg remove openssl:x64-windows
  1. 安装带legacy特性的1.1.1版本:
vcpkg install openssl:x64-windows --feature=legacy
  1. 配置环境变量后编译:
set VCPKG_TRIPLET=x64-windows
set OPENSSL_DIR=C:\Program Files\vcpkg\installed\x64-windows
cargo run

方法三:让rust-openssl自动编译适配版本

无需手动安装OpenSSL,让openssl-sys自动下载并编译兼容的版本:
在项目根目录创建.cargo/config.toml,添加内容:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
[env]
OPENSSL_STATIC = "1"

执行清理并重新编译:

cargo clean && cargo run

内容的提问来源于stack exchange,提问作者Alexey Mirano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:24:56