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.
下载安装完成后,会在vcpkg根目录下多出一个vcpkg.exe文件。
为便于调出vcpkg,将vcpkg放到机器的环境变量中。
二、VCPKG安装C/C++库
通过install命令安装所需要的C/C++库(默认为x86版本,可以通过triplet参数指定为x64):
vcpkg install curl:x64-windows
安装的包在vcpkg目录下downloads文件夹里面。
三、通过NuGet在项目中集成
考虑到在VC项目中引用安装的包,可以通过vcpkg integrate install安装至全局,对于开源项目,这种方式官方是推荐的,它适用于 Visual Studio 开发环境和 msbuild 命令行。但如只考虑本项目,可通过如下命令完成:
vcpkg integrate project
这时候会在 <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包管理器->程序包管理器设置”:
在NuGet包管理器的“程序包源”中添加之前生成的vcpkg.F.vcpkg.1.0.0.nupkg文件。
引入后,在代码中“开箱即用”:
// 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;
}
可正确编译链接运行,运行效果如下:
之前需要手动处理的链接库、生成事件(如dll拷贝等),均“一键成型”。