用vcpkg安装openssl

用vcpkg安装openssl

  • 背景
    • 解决方案
      • 1 安装vcpkg
        • 1.1 下载代码组件
        • 1.1 生成vcpkg.exe
        • 1.2 安装openssl
      • 2 配置环境变量
      • 3 重新编译运行,正常通过

背景

最近学习Rust的时候,有个依赖需要用到Openssl,但是cargo编译的时候提示如下信息:

Caused by:
  process didn't exit successfully: `D:\xxx\target\debug\build\openssl-sys-985d7667cb187e06\build-script-main` (exit code: 101)
  --- stdout
  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: Could not find Vcpkg tree: No vcpkg installation found. Set the VCPKG_ROOT environment variable or run 'vcpkg integrate install'

  --- stderr
  thread 'main' panicked at C:\xxx\.cargo\registry\src\index.crates.io-6f17d22bba15001f\openssl-sys-0.9.98\build\find_normal.rs:190:5:

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  proceed without this knowledge. If OpenSSL is installed and this crate had
  trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
  compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found
  automatically, please open a bug at https://github.com/sfackler/rust-openssl
  and include information about your system as well as this message.

  $HOST = x86_64-pc-windows-msvc
  $TARGET = x86_64-pc-windows-msvc
  openssl-sys = 0.9.98

  It looks like you're compiling for MSVC but we couldn't detect an OpenSSL
  installation. If there isn't one installed then you can try the rust-openssl
  README for more information about how to download precompiled binaries of
  OpenSSL:

  https://github.com/sfackler/rust-openssl#windows

  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

解决方案

1 安装vcpkg

1.1 下载代码组件

用git clone代码或者下载对应的代码zip包

git clone https://github.com/microsoft/vcpkg
1.1 生成vcpkg.exe

执行初始化脚本

.\vcpkg\bootstrap-vcpkg.bat

其他安装命令可以查看 .\vcpkg\README_zh_CN.md

1.2 安装openssl
.\vcpkg\vcpkg install openssl:x64-windows

安装后的文件在.\vcpkg\packages

2 配置环境变量

配置环境变量

X86_64_PC_WINDOWS_MSVC_OPENSSL_LIB_DIR =  安装目录\vcpkg\packages\openssl_x64-windows\lib
X86_64_PC_WINDOWS_MSVC_OPENSSL_INCLUDE_DIR = 安装目录\vcpkg\packages\openssl_x64-windows\include
X86_64_PC_WINDOWS_MSVC_OPENSSL_DIR = 安装目录\vcpkg\packages\openssl_x64-windows

在这里插入图片描述

3 重新编译运行,正常通过

cargo run

用vcpkg安装openssl_第1张图片

你可能感兴趣的:(rust,C/C++,rust)