windows - 如何在 Windows 开发环境中使用 OpenSSL for Rust

标签 windows visual-studio-code rust openssl rust-cargo

当我在我的 cargo 文件中添加 SSL 依赖项时,我无法构建我的 Rust 项目。 这对 Windows 来说并不陌生,但我想解决这个问题,以便我可以使用 Powershell 和 native Windows 开发来处理我的 Rust 项目。

有问题的依赖如下:

[dependencies.ws]
version = "0.8.0"
features = ["ssl"]

当我运行 cargo build 时,因为我在 Windows 上,所以我收到很多关于 SSL 的错误,所以让我们开始逐步调试。

第一个错误:

PS C:\Users\sam\vcs\project> cargo build
   Compiling openssl-sys v0.9.43
error: failed to run custom build command for `openssl-sys v0.9.43`
process didn't exit successfully: `C:\Users\sam\vcs\project\target\debug\build\openssl-sys-d964f46e4c48d206\build-script-main` (exit code: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR
X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR
X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR
X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR unset
cargo:rerun-if-env-changed=OPENSSL_DIR
OPENSSL_DIR unset
note: vcpkg did not find openssl as libcrypto and libssl: Aborted because VCPKGRS_DYNAMIC is not set
note: vcpkg did not find openssl as ssleay32 and libeay32: Aborted because VCPKGRS_DYNAMIC is not set

好的,让我们设置 OPENSSL_LIB_DIROPENSSL_INCLUDE_DIROPENSSL_DIR

$env:OPENSSL_LIB_DIR="C:\OpenSSL-Win64\lib"
$env:OPENSSL_INCLUDE_DIR="C:\OpenSSL-Win64\include"
$env:OPENSSL_DIR="C:\OpenSSL-Win64"

但是,现在运行 cargo buildcargo run 实际上可以编译,但我认为 SSL 无法正常工作,因为连接到 websocket 的代码路径因错误而失败没有。

我从以下网站安装了最新 (1.1.0j) 完整(非瘦身)版本的 OpenSSL:https://slproweb.com/products/Win32OpenSSL.html

它设置的唯一环境变量是:

OPENSSL_CONF: C:\OpenSSL-Win64\bin\openssl.cfg

但我还添加了 C:\OpenSSL-Win64\bin 到我的 PATH

cargo build 实际编译时,websocket 失败并出现错误 None

on_error block 在我的 Windows 机器上执行,但我的 Linux 机器从不执行此代码块。

为了让我们在同一页上,这是打印语句的输出:

fn on_error(&mut self, err: ws::Error) {
    println!("On Error, {}", err)
}

无不是一个描述性很强的答案。所以我假设我只需要一些证书,因为 OpenSSL 没有附带证书。 我从网上某处下载了一个 cacert.pem 并将其放入 OpenSSL 安装的 certs 文件夹中,但这也没有太大区别。仍然是相同的 None 错误。

在我的 Linux 机器上,我希望 websocket 连接成功并且代码继续到 on_open:

fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
...
}

有人在他们的 Windows 开发环境中解决过这个 OpenSSL 问题吗?


更新 0

按照 Cloud 的建议,我尝试使用 VCPKG。不幸的是,它没有用。

PS C:\Users\sam\vcs\vcpkg> .\vcpkg.exe list
openssl-windows:x64-windows-static                 1.0.2q-2         OpenSSL is an open source project that provides ...
openssl-windows:x86-windows                        1.0.2q-2         OpenSSL is an open source project that provides ...
openssl:x64-windows-static                         0                OpenSSL is an open source project that provides ...
openssl:x86-windows                                0                OpenSSL is an open source project that provides ...

然后我在我的 powershell 窗口中设置变量:

PS C:\Users\sam\vcs\project> $env:OPENSSL_DIR = 'C:\Users\sam\vcs\vcpkg\installed\x64-windows-static'
PS C:\Users\sam\vcs\project> $env:OPENSSL_STATIC = 'Yes'
PS C:\Users\sam\vcs\project> $env:VCPKGRS_DYNAMIC='1'

然后我运行了 cargo clean,然后运行了 cargo build,我仍然遇到同样的错误,我的 websocket 抛出错误 None

这里仍然没有运气。

最佳答案

  1. 克隆vcpkg
  2. 打开您克隆 vcpkg 的目录
  3. 运行./bootstrap-vcpkg.bat
  4. 运行 ./vcpkg.exe install openssl-windows:x64-windows
  5. 运行 ./vcpkg.exe install openssl:x64-windows-static
  6. 运行 ./vcpkg.exe 集成安装
  7. 运行 set VCPKGRS_DYNAMIC=1(或简单地将其设置为您的环境变量)

关于windows - 如何在 Windows 开发环境中使用 OpenSSL for Rust,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912871/

相关文章:

windows - 解释java运行时检测到的 fatal error EXCEPTION_ACCESS_VIOLATION

windows - 在 Windows 7 中从 node.js 命令提示符运行 Hello world

windows - 使用 APR 和 OpenSSL 解决 Tomcat 上的 Heartbleed 问题。编译错误

visual-studio-code - 让 TSLint 遵循我在 JS 文件中的规则

python - 使用Python 3.8在Visual Studio代码中导入统计信息找不到中位数

rust - 如何匹配Arc内的选项?

rust - 从 Rust 中的任务调用闭包

java - 安装 Eclipse Java SE-1.7

git - 有没有办法使用 VSCode 通过远程 SSH 运行 "git difftool"?

rust - 如何重用范围来获取数组的一部分?