windows下rust使用openssl

静态链接

以vcpkg安装,配置环境变量
| 环境变量名称 | 环境变量地址 | |
| X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR | C:\src\cctvhddomestically_vcpkg\packages\openssl_x64-windows-static | |
| X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR | C:\src\cctvhddomestically_vcpkg\packages\openssl_x64-windows-static\include | |
| X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR| C:\src\cctvhddomestically_vcpkg\packages\openssl_x64-windows-static\lib | |
image
编译时间为03:12:34 2024;

OpenSSL Version: OpenSSL 3.3.0 9 Apr 2024
Built on: built on: Sat Jun  1 03:12:34 2024 UTC
OPENSSL_DIR is not set
X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR directory: C:\src\cctvhddomestically_vcpkg\packages\openssl_x64-windows-static

动态链接

官网下载安装包,配置环境变量
image

出现了

error: failed to run custom build command for `openssl-sys v0.9.103`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.

Caused by:
  process didn't exit successfully: `C:\shared\rustBuild\debug\build\openssl-sys-4f782b80ec2d99e2\build-script-main` (exit code: 101)
  --- stderr
  thread 'main' panicked at C:\Users\Administrator\.cargo\registry\src\rsproxy.cn-0dccff568467c15b\openssl-sys-0.9.103\build/main.rs:263:13:

  Header expansion error:
  Error { kind: ToolExecError, message: "Command \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.39.33519\\\\bin\\\\HostX64\\\\x64\\\\cl.exe\" \"-nologo\" \"-MD\" \"-Z7\" \"-Brepro\" \"-I\" \"C:\\\\Program Files\\\\OpenSSL-Win64\\\\include\\\\openssl\" \"-W4\" \"-E\" \"build/expando.c\" with args cl.exe did not execute successfully (status code exit code: 2)." }

  Failed to find OpenSSL development headers.

原因是: OPENSSL_INCLUDE_DIR 写多了一个openssl
修复后, 如下
image

OpenSSL Version: OpenSSL 3.3.0 9 Apr 2024
Built on: built on: Thu Apr 11 14:00:56 2024 UTC
OpenSSL directory: C:\Program Files\OpenSSL-Win64

使用到的oepnssl地址为 OpenSSL directory: C:\Program Files\OpenSSL-Win64,编译时间为 Thu Apr 11 14:00:56 2024 UTC

C:\Program Files\OpenSSL-Win64\bin>.\openssl.exe version
OpenSSL 3.3.0 9 Apr 2024 (Library: OpenSSL 3.3.0 9 Apr 2024)

总结

use openssl::version;
use openssl::version::version;
use std::env;

fn main() {
   let openssl_version = version();

    let c_flags = version::c_flags();
    println!("c_flags  = {}", c_flags);


    println!("Hello, OpenSSL!");
    println!("OpenSSL Version: {}", openssl_version);
    let build_info = version::built_on();
    println!("Built on: {}", build_info);
 
    // 安装包openssl, 可根据OPENSSL_STATIC=1?,默认是动态链接,为1是静态链接;
    match env::var("OPENSSL_DIR") {
        Ok(dir) => println!("OpenSSL directory: {}", dir),
        Err(_) => println!("OPENSSL_DIR is not set"),
    }

    // 由vcpkg安装而来;目前看来仅有static;
    match env::var("X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR") {
        Ok(dir) => println!("X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR directory: {}", dir),
        Err(_) => println!("X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR is not set"),
    }
方案 动态链接 静态链接 备注
openssl安装包 150 KB (153,600 字节) 未能成功配置静态
vcpkg 4.57 MB (4,801,536 字节) 无动态链接

检查方式:
dumpbin /DEPENDENTS C:\shared\rustBuild\debug\iter-openssl.exe

  • 静态
 Image has the following dependencies:

    api-ms-win-core-synch-l1-2-0.dll
    USER32.dll
    CRYPT32.dll
    WS2_32.dll
    ADVAPI32.dll
    KERNEL32.dll
    ntdll.dll
    VCRUNTIME140.dll
    api-ms-win-crt-string-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-filesystem-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    api-ms-win-crt-time-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-utility-l1-1-0.dll
    api-ms-win-crt-convert-l1-1-0.dll
    api-ms-win-crt-environment-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll

  Summary

        B000 .data
       24000 .pdata
       F6000 .rdata
        B000 .reloc
      369000 .text
  • 动态
    会有一个 依赖libcrypto.dll, 此处不展示了就。

反正吖, openssl坑不少, 推荐这里:
https://blog.xco.moe/posts/rust_build_musl/
https://blog.hcl.moe/archives/2630

posted @ 2024-09-19 11:43  githubchy  阅读(147)  评论(0编辑  收藏  举报