VSCode使用CMake添加vcpkg的OpenSSL包

文章目录

    • 前言
    • CMakeLists.txt
    • 日志提示
    • 检查安装
    • 设置Vcpkg环境变量
    • VSCode手动设置
    • 引用
    • 其他方法

前言

最近用着Windows+VSCode进行跨平台开发,vcpkg下载好OpenSSL生成的时候CMake就找不到了

CMakeLists.txt

# SSL
find_package(OpenSSL REQUIRED)
target_link_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)

日志提示

CMake Error at E:/cmake/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
[cmake]   Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the
[cmake]   system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY
[cmake]   OPENSSL_INCLUDE_DIR)

检查安装

PS C:\Users\NueXini> vcpkg list
openssl:x64-windows                               3.0.5#4             OpenSSL is an open source project that provides ...
vcpkg-cmake-config:x64-windows                    2022-02-06#1
vcpkg-cmake-get-vars:x64-windows                  2022-05-10#1
vcpkg-cmake:x64-windows                           2022-07-18

确保vcpkg已经把openssl下载并安装了,然后在 CMakeLists.txt 中添加手动设置一下路径

set(CMAKE_TOOLCHAIN_FILE E:/vcpkg/scripts/buildsystems/vcpkg.cmake)
set(OPENSSL_ROOT_DIR E:/vcpkg/installed/x64-windows)

find_package(OpenSSL REQUIRED)
target_link_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)

设置Vcpkg环境变量


建议设置VCPKG_ROOT的环境变量,这样就不需要在单独写环境了

下面是VCPKG其他可选的环境变量,可以根据自己需求来设置

Options:
  --triplet=<t>                   Specify the target architecture triplet. See 'vcpkg help triplet'.
                                  (default: 'VCPKG_DEFAULT_TRIPLET')
  --host-triplet=<t>              Specify the host architecture triplet. See 'vcpkg help triplet'.
                                  (default: 'VCPKG_DEFAULT_HOST_TRIPLET')
  --overlay-ports=<path>          Specify directories to be used when searching for ports.
                                  (also: 'VCPKG_OVERLAY_PORTS')
  --overlay-triplets=<path>       Specifiy directories containing triplets files.
                                  (also: 'VCPKG_OVERLAY_TRIPLETS')
  --binarysource=<path>           Add sources for binary caching. See 'vcpkg help binarycaching'.
  --x-asset-sources=<path>        Add sources for asset caching. See 'vcpkg help assetcaching'.
  --downloads-root=<path>         Specify the downloads root directory.
                                  (default: VCPKG_DOWNLOADS)
  --vcpkg-root=<path>             Specify the vcpkg root directory.
                                  (default: 'VCPKG_ROOT')

VSCode手动设置

打开VSCode的设置,找到cmake.buildEnvironment,手动添加环境变量
VSCode使用CMake添加vcpkg的OpenSSL包_第1张图片


引用

在代码中只需要 include 进去就好

#include 

其他方法

  • Github
    https://github.com/openssl/openssl

  • openssl.org
    https://www.openssl.org/source/
    https://www.openssl.org/source/old/

下载完把包解压,设置环境变量,或者手动设置目录也可以,有些库对openssl有要求,选择对应版本下载
像cpp-httplib只兼容opensslde 1.1.1 或者 3.0.0

你可能感兴趣的:(C++,vcpkg,vscode,CMake,openssl,lib,vcpkg)