【教程】windows环境下配置vcpkg作为包管理工具

一、下载

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

为避免后续步骤与我不同,可以与我使用同一版本的vcpkg:

git checkout 2025.03.19

二、配置

进入vcpkg目录,启动power shell(建议),执行

.\bootstrap-vcpkg.bat

顺利完成后,目录下会出现vcpkg.exe文件。

将 vcpkg 与开发环境(例如 Visual Studio 和 MSBuild)进行全局集成,使 vcpkg 安装的库能自动被项目识别和使用

.\vcpkg.exe integrate install

三、搜索和下载

这里以boost为例:

.\vcpkg.exe search boost

asio[coroutine]                           Boost.Coroutine (optional) if you use spawn() to launch coroutines
asio[regex]                               Boost.Regex (optional) if you use any of the read_until() or async_read_un...
async-mqtt               10.1.0           Header-only Asynchronous MQTT communication library for C++17 based on Boo...
async-mqtt[tls]                           Enable TLS support
azmq                     2023-03-23       Boost Asio style bindings for ZeroMQ
beast                    0#2              HTTP/1 and WebSocket, header-only using Boost.Asio and C++11
bext-wintls              0.9.8            Native Windows TLS stream wrapper for use with boost::asio
boost                    1.87.0#1         Peer-reviewed portable C++ source libraries
boost[cobalt]                             Build boost-cobalt
boost[mpi]                                Build with MPI support
boost-accumulators       1.87.0           Boost accumulators module
boost-algorithm          1.87.0           Boost algorithm module
boost-align              1.87.0           Boost align module
boost-any                1.87.0           Boost any module
....

根据第三列的备注即可判断哪个是想安装资源,第一列名字就是他们的key

.\vcpkg.exe install boost

四、验证

4.1 cmake项目

解决方案和项目是否在同一个目录会有不同的配置方式

除此之外,打开项目后顺手设置一下"始终使用CMake预设",方便后续打开cmake项目。(我这里是2022)

验证程序

#include "CMakeProject.h"
#include <boost/algorithm/string.hpp>
#include <boost/version.hpp>
using namespace std;

int main()
{
	std::string text = "hello world";
	cout << "Hello CMake." << endl;

	// 将字符串转换为大写
	boost::to_upper(text);

	std::cout << "Boost version: " << BOOST_LIB_VERSION << std::endl;
	std::cout << "Converted text: " << text << std::endl;

	return 0;
}

4.1.1 同一目录

主要配置命令如下,CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

# 设置自己的 vcpkg 路径
set(VCPKG_PATH "E:/workspace/vcpkg")

project ("CMakeProject")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_P2ATH}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
set(CMAKE_PREFIX_PATH "${VCPKG_PATH}/installed/x64-windows;${CMAKE_PREFIX_PATH}")

find_package(Boost REQUIRED COMPONENTS algorithm)

add_executable (CMakeProject "CMakeProject.cpp" "CMakeProject.h")

target_link_libraries(CMakeProject PRIVATE Boost::algorithm)

4.1.2 不同目录

根目录下的CMakeLists.txt

# 设置自己的 vcpkg 路径
set(VCPKG_PATH "E:/workspace/vcpkg")

project ("CMakeProject")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_PATH}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
set(CMAKE_PREFIX_PATH "${VCPKG_PATH}/installed/x64-windows;${CMAKE_PREFIX_PATH}")

# Include sub-projects.
add_subdirectory ("CMakeProject")

项目中的CMakeLists.txt

find_package(Boost REQUIRED COMPONENTS algorithm)

add_executable (CMakeProject "CMakeProject.cpp" "CMakeProject.h")

target_link_libraries(CMakeProject PRIVATE Boost::algorithm)

4.2 vs项目

这里就以创建了console prj为例(都一样)。

源程序

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/version.hpp>
int main()
{
	std::string text = "hello world";
	std::cout << "Hello CMake." << std::endl;

	// 将字符串转换为大写
	boost::to_upper(text);

	std::cout << "Boost version: " << BOOST_LIB_VERSION << std::endl;
	std::cout << "Converted text: " << text << std::endl;
}

完了。不需要任何配置。

五、报错

我希望大家可以不来看这里...毕竟到这里证明你也遇到问题了..

5.1 无法解压cmake

安装第一步提示需要cmake 3.30.1版本以上,没有则安装,下载后解压失败:

error: Expected cmake-3.30.1-windows-i386/bin/cmake.exe to exist after fetching

尝试无果后,手动在本地环境安装对应版本的cmake,并设置bin到环境变量中,通过。

5.2 部分库安装时提示无法识别代码

检查你的控制台,在启动时是否有:“Active code page: 65001”这样的字符。

有任何字符都会导致vcpkg安装库出现问题。>nul

vcpkg安装库时,可能会将cmd的输出一起捕获并写入了输出文件中,导致编译出现问题。

解决方法:

找到下述文件,查看内容

  1. 注册表编辑器 -> 计算机 -> HKEY_CURRENT_USER -> SOFTWARE -> Microsoft -> Command Processor -> AutoRun
  2. 注册表编辑器 -> 计算机 -> HKEY_LOCAL_MACHINE -> SOFTWARE -> Microsoft -> Command Processor -> AutoRun

如果内容如下,则修改为:chcp 65001>nul,这样既能切为UTF-8又能将输出屏蔽。

5.3 路径过长

安装部分库时可能出现报错:

CMake Error at ports/qt5-webengine/portfile.cmake:6 (message):
  terminating due to E:/workspace/vcpkg_2025_03_19/buildtrees/qt5-webengine
  being too long.
Call Stack (most recent call first):
  scripts/ports.cmake:203 (include)

两种解决办法:

  1. 剪切vcpkg整个文件夹到一个短路径中;
  2. 使用虚拟盘创建新的路径;

这里我建议采用第二种

  • 进入cmd
  • 创建虚拟盘符
    • 这里我以 E:\workspace\vcpkg 为例,将其映射到 虚拟磁盘 A,虚拟磁盘需要是一个不存在的磁盘。
    • subst A: "E:\workspace\vcpkg"
  • 之后进入虚拟磁盘,再进行安装即可
    • cd A:
    • .\vcpkg.exe install qt5-webengine:x64-windows

5.4 内存不足

vcpkg安装并构建qt相关库时,会使用qt官方开发的make 工具,默认使用全部线程,而构建库本身非常庞大的话,就会出现内存不足的情况,提示信息是:

CMake Warning at cripts/cmake/vcpkg_execute_build_process.cmake:65 (message):
  Please ensure your system has sufficient memory.

此时可以临时修改线程数(仅限当前控制台生效)

set VCPKG_MAX_CONCURRENCY=2

posted @ 2025-04-11 13:37  小拳头呀  阅读(1014)  评论(0)    收藏  举报