VCPKG配合NuGet在项目中使用包

Vcpkg 可帮助我们在 Windows、 Linux 和 MacOS 上管理 C 和 C++ 库。 查阅该项目的资料,我们可以发现它的应用让开发变得更加简捷。

 

一、VCPKG安装

1 仓库克隆

使用git工具,将vcpkg仓库克隆到本地。

git clone https://github.com/microsoft/vcpkg

 

2 环境配置

用管理员身份运行命令行窗口,进入本地vcpkg目录,执行bootstrap-vcpkg.bat,安装vckpg。

Downloading https://github.com/microsoft/vcpkg-tool/releases/download/2024-01-11/vcpkg.exe -> F:\vcpkg\vcpkg.exe (using IE proxy: 127.0.0.1:7890)... done.

Validating signature... done.

 

vcpkg package management program version 2024-01-11-710a3116bbd615864eef5f9010af178034cb9b44

 

See LICENSE.txt for license information.

Telemetry

---------

vcpkg collects usage data in order to help us improve your experience.

The data collected by Microsoft is anonymous.

You can opt-out of telemetry by re-running the bootstrap-vcpkg script with -disableMetrics,

passing --disable-metrics to vcpkg on the command line,

or by setting the VCPKG_DISABLE_METRICS environment variable.

 

c07aaf53d1b54202a3c6bc2d0dd5c075.png

 

下载安装完成后,会在vcpkg根目录下多出一个vcpkg.exe文件。

7175740ed9f34bd3a09ca0a5e5ae5f1d.png

 

为便于调出vcpkg,将vcpkg放到机器的环境变量中。

9a23b8a86c22402d88da2c22253b6f51.png

 

二、VCPKG安装C/C++库

通过install命令安装所需要的C/C++库(默认为x86版本,可以通过triplet参数指定为x64):

vcpkg install curl:x64-windows

fe143d08b2c74750a8d998f38a7fd9c6.png

安装的包在vcpkg目录下downloads文件夹里面。

b63a28bd6da54494928aa69082ecadd7.png

三、通过NuGet在项目中集成

考虑到在VC项目中引用安装的包,可以通过vcpkg integrate install安装至全局,对于开源项目,这种方式官方是推荐的,它适用于 Visual Studio 开发环境和 msbuild 命令行。但如只考虑本项目,可通过如下命令完成:

vcpkg integrate project

599ae8a5950047ef81828f627d347018.png

这时候会在 <vcpkg_dir>\scripts\buildsystems 目录下,生成 nuget 配置文件。其中 <vcpkg_dir> 是指 vcpkg 实际所在目录。

 

With a project open, go to Tools->NuGet Package Manager->Package Manager Console and paste:

 Install-Package "vcpkg.F.vcpkg" -Source "F:\"

 

我们可以通过上面的操作方式在命令行中集成,当然也可以通过NuGet包管理界面可视化地添加路径引用——“工具->NuGet包管理器->程序包管理器设置”:

5cee1073229d4f08b32207910556cf4d.png

 

86e8237fcb274ad48c3e4bd212291122.png

 

在NuGet包管理器的“程序包源”中添加之前生成的vcpkg.F.vcpkg.1.0.0.nupkg文件。

1eca52dbeb434222991053c9fc566b29.png

66b8a81d30084fa2b4f96c836af6ee3a.png

 

引入后,在代码中“开箱即用”:

 

// CURL
#include <curl/curl.h>
// STL
#include <sstream>
#include <cerrno>
#include <cstdio>
#include <cstring>

size_t write_callback(char* contents, size_t size, size_t nmemb, void* output_buffer_void) {
    auto real_size = size * nmemb;
    auto output_buffer = static_cast<std::string*>(output_buffer_void);
    output_buffer->append((char*)contents, real_size);
    return real_size;
}

int main(int argc, char** argv) {
    // Init
    curl_global_init(CURL_GLOBAL_DEFAULT);
    CURL* curl = curl_easy_init();

    // Download from an HTTPS url
    const char* url = "https://example.com/";
    curl_easy_setopt(curl, CURLOPT_URL, url);

    // Do a GET
    curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);

    // Save output to a std::string
    std::string output_buffer;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output_buffer);

    // Save error to a buffer
    char error_buffer[CURL_ERROR_SIZE];
    error_buffer[0] = '\0';
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);

    // Execute the request
    long http_code = 0;
    CURLcode res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

    if (res == CURLE_OK) {
        printf("Request was succesful with status %d, output:\n%s",
            http_code, output_buffer.c_str()
        );
    }
    else {
        printf("Request failed: %s", error_buffer);
    }

    // Cleanup
    curl_easy_cleanup(curl);
    curl_global_cleanup();
    return 0;
}

 

 

可正确编译链接运行,运行效果如下:

facc41be9b434186b13a029a7854d8d4.png

 

之前需要手动处理的链接库、生成事件(如dll拷贝等),均“一键成型”。

a94c295a449f44aa8e1fcdda902598c9.png

 

 

  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Humbunklung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值