VCPKG编译和管理MSVC的库

编译环境搭建

  1. 点击MSVC编译环境单独下载打开页面,可安装不含臃肿Visual Studio IDE的命令行编译环境

  2. 拖到后面 【适用于Visual Studio 20XX的工具】 下的 【Visual Studio 20XX 生成工具】 点击下载

  3. 如下配置是VCPKG所需Visual Studio的必须组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 可用命令行来自动化下载
vs_BuildTools.exe --config "C:\vcpkg.vsconfig"

# 也可手动打开vs_BuildTools.exe下载,主要是要包含以下组件vcpkg.vsconfig内容如下:
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.VisualStudio.Component.CoreBuildTools",
"Microsoft.VisualStudio.Workload.MSBuildTools",
"Microsoft.VisualStudio.Component.Windows10SDK",
"Microsoft.VisualStudio.Component.VC.CoreBuildTools",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"Microsoft.VisualStudio.Component.VC.Redist.14.Latest",
"Microsoft.VisualStudio.Component.TextTemplating",
"Microsoft.VisualStudio.Component.VC.CoreIde",
"Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core",
"Microsoft.VisualStudio.Component.Windows11SDK.22621",
"Microsoft.VisualStudio.Workload.VCTools"
]
}

# 为了防止后续显示问题,最好装个英文语言包

获取源码及编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 默认CMD会自动检测代理,也可手动为CMD添加代理
# set http_proxy=http://127.0.0.1:7890
# set https_proxy=http://127.0.0.1:7890

# 获取vcpkg源码树
git clone https://github.com/microsoft/vcpkg
cd vcpkg

# 将会下载vcpkg.exe到 $VCPKG_ROOT 目录。不需要设置VS相关,vcpkg会自己到默认路径找
.\bootstrap-vcpkg.bat

# 默认是安装32位的库,在本机需要安装x64的库,因此需要添加一个系统变量重新指定默认的TRIPLET
# 示例:安装glib的几种方式,TRIPLET的值可 vcpkg help triplet 查看:
vcpkg.exe install glib --triplet x64-windows
vcpkg.exe install glib:x64-windows
vcpkg.exe install glib:x64-windows-static

# vcpkg的其他命令
vcpkg list // 列出已安装包
vcpkg help triplet // 查看支持的架构
vcpkg remove [search term] // 移除已安装包
vcpkg remove xxxx:x64-windows // 指定卸载平台的库
vcpkg search [search term] // 查找 vcpkg 中包含的库
vcpkg integrate install // 在Visual Studio中全局使用
vcpkg integrate remove // 从全局中移除
vcpkg integrate project // 集成到工程
vcpkg update xxx // 更新已经安装的库
vcpkg remove --outdated // 移除所有旧版本库
vcpkg export xxx --7zip // 导出已经安装的库为7zip压缩包
vcpkg export xxx --raw // 导出一个未压缩的文件夹
vcpkg export xxx --output= // 指定输出文件的名称,此参数需和导出格式的参数一同使用
vcpkg export xxx --output-dir= // 指定文件的输出目录,此参数需和导出格式的参数一同使用

# 其他 install 命令可选参数
--triplet=<t> // 指定目标的triplet,和在库名称后面以“:xxx”功能一致
--host-triplet=<t> // 指定主机的triplet
--debug // 启用debug
--vcpkg-root=<path> // 指定vcpkg-root的路径,即vcpkg.exe所在的路径
--overlay-triplets=<path> // 指定triplets文件所在的路径
--overlay-ports=<path> // 指定port文件所在用的目录
--binarysource=<path> // 指定binary cache的路径,而不是使用默认的 %USERPROFILE%\AppData\Local\vcpkg\archives
--downloads-root=<path> // 指定downloads-root的路径,默认.\vcpkg\downloads
--only-downloads // 只下载资源包但是不构建
--recurse // 允许在安装过程中删除软件包
--keep-going // 安装命令中包含多个库时,一个库安装失败时整条命令不会终止,而是继续安装下一个库
--editable // 使下载下来的库的原文件可被编辑
--clean-after-build // 在每一个库构建完成后清理buildtrees下库的源文件, packages 和 downloads 下的库相关的文件
--clean-buildtrees-after-build // 在每一个库构建完成后清理 buildtrees 文件夹
--clean-packages-after-build // 在每一个库构建完成后清理 packages 文件夹
--clean-downloads-after-build // 在每一个库构建完成后清理 downloads 文件夹

其他环境变量

变量名称 变量作用
VCPKG_TARGET_ARCHITECTURE 指定生成库对应的目标机器的架构
VCPKG_CRT_LINKAGE 指定生成库时使用的MSVC CRT 链接方式(/MT /MD static/dynamic)
VCPKG_LIBRARY_LINKAGE 指定生成库的类型(dynamic/static)
VCPKG_CMAKE_SYSTEM_NAME 指定目标平台类型
VCPKG_CMAKE_SYSTEM_VERSION 指定目标平台的版本
VCPKG_CHAINLOAD_TOOLCHAIN_FILE 指定要使用的备用 CMake toolchain文件
VCPKG_CXX_FLAGS 设置额外的编译选项
VCPKG_LINKER_FLAGS 设置额外的链接选项
VCPKG_CMAKE_CONFIGURE_OPTIONS 设置额外的CMake 配置选项
VCPKG_MAKE_CONFIGURE_OPTIONS 设置额外的automake/autoconf 配置选项
VCPKG_DEP_INFO_OVERRIDE_VARS 设置覆盖默认triplet文件中的设置选项
VCPKG_VISUAL_STUDIO_PATH 指定使用的Visual Studio的路
VCPKG_PLATFORM_TOOLSET 指定要使用的C/C++编译器工具集 v140/v141/v142
1
2
3
4
5
6
7
8
9
10
# 这个环境变量可指定Visual Studio的路径,默认即可
ENV VCPKG_VISUAL_STUDIO_PATH="C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools"

# 要在x64-windows triplet下使用VS2017的工具集,可以添加如下内容到 triplets/x64-windows.cmake文件中:
set(VCPKG_PLATFORM_TOOLSET v141)

# 可以通过在对应port的portfile.cmake文件中添加如下内容来添加额外的编译选项
# 注意:如果您手动设置了VCPKG_CXX_FLAGS,那么您必须同时设置VCPKG_C_FLAGS
set(VCPKG_CXX_FLAGS "/arch:SSE2 ${VCPKG_CXX_FLAGS}")
set(VCPKG_C_FLAGS "/arch:SSE2 ${VCPKG_C_FLAGS}")