在windows环境中使用vcpkg+cmake完成类linux的c++开发环境配置

由于众所周知的原因,在 windows 下进行 c++开发是一件极其痛苦的事情。我也多次因为无法在 windows 下配置开发环境而转向 linux 开发。我在最近移植我的 linux 应用时,发现了一个在 windows 下非常方便的 c++安装包管理器。这个就是 vcpkg。通过使用 vcpkg,可以在 windows 上实现 linux 一样的开发环境。

本篇文章旨在为各位提供一个新的环境配置思路。我本人也是刚开始研究这个软件,难免会有众多疏忽。

个人博客:http://www.ghost-him.com/

1. 安装 vcpkg 与 cmake

首先要安装 vcpkg 与 cmake,安装的过程已经有很多人写了相应的文章了,这里就不赘述了。

安装对应的库

安装库的命令是

复制代码
vcpkg install [库的名字]

这里给几个示例:

安装 trantor
bash 复制代码
vcpkg install trantor
安装 openssl
bash 复制代码
vcpkg install openssl

整合第三方库

使用命令 vcpkg integrate install。这个命令的作用是:配置vcpkg以便在Visual Studio项目中自动包含库文件。执行该命令后,通过vcpkg安装的库将被集成到Visual Studio的项目中,使得开发者无需手动配置库路径,可以直接在项目中包含和使用这些库。这简化了项目配置过程,提高了开发效率。

在运行后,可以看到提示:

复制代码
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/software/vcpkg-2024.01.12/scripts/buildsystems/vcpkg.cmake"

All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.

这里的意思就是,如果我们使用 cmake。则在构建时,应该添加上 -DCMAKE_TOOLCHAIN_FILE=C:/software/vcpkg-2024.01.12/scripts/buildsystems/vcpkg.cmake

。** 每个人的 vcpkg 的安装目录均不同,所以要按照自己的提示来写!**

修改 CMakeLists.txt

每次添加完一个第三方库都要在 cmake 中添加 vcpkg 所给出的安装信息。

以 openssl 为例。

运行 vcpkg install openssl。以后,可以看到给出的信息:

复制代码
PS C:\software\vcpkg-2024.01.12> .\vcpkg.exe install openssl
warning: In the September 2023 release, the default triplet for vcpkg libraries changed from x86-windows to the detected host triplet (x64-windows). For the old behavior, add --triplet x86-windows . To suppress this message, add --triplet x64-windows .
Computing installation plan...
The following packages are already installed:
    openssl:x64-windows@3.2.0#2
openssl:x64-windows is already installed
Total install time: 275 us
The package openssl is compatible with built-in CMake targets:

    find_package(OpenSSL REQUIRED)
    target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)

PS C:\software\vcpkg-2024.01.12>

这里的意思是如果使用 cmake,则要添加

复制代码
find_package(OpenSSL REQUIRED)
target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)

(这里可以发现,使用 cmake 与 linux 基本一致,很方便,也不用自己去设置环境变量)

编译运行

复制代码
cmake -B build -DCMAKE_TOOLCHAIN_FILE=C:/software/vcpkg-2024.01.12/scripts/buildsystems/vcpkg.cmake

cmake --build build

完成。

从头到尾的一个示例

安装 openssl(其它库的安装与这个库的安装流程一致)

复制代码
vcpkg install openssl

整合

复制代码
vcpkg integrate install

修改

复制代码
cmake_minimum_required(VERSION 3.5)
project(vcpkgTest)

# Create the library
add_executable(common main.cpp)

# Find required packages
find_package(OpenSSL REQUIRED)

# Link with found packages
target_link_libraries(common PRIVATE OpenSSL::SSL OpenSSL::Crypto)

编写测试代码

main.cpp:(生成一对 ecc 公钥与私钥)

cpp 复制代码
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <iostream>
int main()
{
    std::string publicKey, privateKey;
    EC_KEY *ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); // 使用P-256曲线
    if (ecKey != nullptr && EC_KEY_generate_key(ecKey))
    {
        BIO *pub = BIO_new(BIO_s_mem());
        BIO *priv = BIO_new(BIO_s_mem());
        if (PEM_write_bio_EC_PUBKEY(pub, ecKey) && PEM_write_bio_ECPrivateKey(priv, ecKey, nullptr, nullptr, 0, nullptr, nullptr))
        {
            size_t pubLength = BIO_pending(pub);
            size_t privLength = BIO_pending(priv);
            publicKey.resize(pubLength + 1);
            privateKey.resize(privLength + 1);
            BIO_read(pub, (void *)publicKey.c_str(), pubLength);
            BIO_read(priv, (void *)privateKey.c_str(), privLength);
            
            publicKey[pubLength] = '\0';
            privateKey[privLength] = '\0';
        }
        BIO_free_all(pub);
        BIO_free_all(priv);
    }
    EC_KEY_free(ecKey);
    std::cout << publicKey << " | " << privateKey << std::endl;
    return 0;
}

构建

复制代码
cmake -B build -DCMAKE_TOOLCHAIN_FILE=C:/software/vcpkg-2024.01.12/scripts/buildsystems/vcpkg.cmake

编译

复制代码
cmake --build build
相关推荐
pofenx几秒前
使用nps创建隧道,进行内网穿透
linux·网络·内网穿透·nps
Ronin3051 分钟前
【Linux系统】单例式线程池
linux·服务器·单例模式·线程池·线程安全·死锁
desssq24 分钟前
ubuntu 18.04 泰山派编译报错
linux·运维·ubuntu
Lzc77428 分钟前
Linux的多线程
linux·linux的多线程
清风笑烟语29 分钟前
Ubuntu 24.04 搭建k8s 1.33.4
linux·ubuntu·kubernetes
Dovis(誓平步青云)1 小时前
《Linux 基础指令实战:新手入门的命令行操作核心教程(第一篇)》
linux·运维·服务器
好名字更能让你们记住我1 小时前
MYSQL数据库初阶 之 MYSQL用户管理
linux·数据库·sql·mysql·adb·数据库开发·数据库架构
呱呱巨基2 小时前
C/C++ 内存管理
c++·笔记·学习
半桔2 小时前
【网络编程】TCP 服务器并发编程:多进程、线程池与守护进程实践
linux·服务器·网络·c++·tcp/ip
维尔切2 小时前
Shell 脚本编程:函数
linux·运维·自动化