以下示例将提供在自定义工程中使用 vcpkg 中 fmt、sqlite3、zlib 的固定版本。
Manifest模式 - CMake 工程
1. 示例根目录: E:/test_manifest,文件目录结构如下:
E:/
|--test_manifest/
| ----build/
| ----CMakeLists.txt
| ----main.cpp
| ----vcpkg.json
2. 需在 vcpkg 目录中打开 powershell 并执行 install 命令来获取 CMAKE_TOOLCHAIN_FILE
./vcpkg integrate install
输出:
Applied user-wide integration for this vcpkg root. CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=E:/src/test/vcpkg/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.
3. 获取 builtin-baseline,builtin-baseline 指的是 vcpkg 仓库包含的 commit id,可以通过命令行来获取 vcpkg 所需的 commit id。
获取 vcpkg 最新的 commit id:
git rev-parse HEAD
获取某个库历史版本的 commit id:
./vcpkg x-history PORT_NAME
假如我们需要 "8.1.0#0" 的 fmt,那就在输出列表中选择 "8.1.0#0" 对应的 commit id: "b87c730e96fc6e7c9f55bb1cf31f58a838073b68"。
E:/src/test/vcpkg>./vcpkg x-history fmt
Version Date VCPKG Commit
9.0.0#0 2022-08-04 e2a71489b17ae1f5fd29d5fc74c2cdb03fd83f9e 8.1.1#2 2022-07-05 5dd5017f144d4412ac42c5506a31006b6ac16600 8.1.1#1 2022-03-02 45346012195355162ffb26a43690b6c76d8bf3b9 8.1.1#0 2022-01-27 a4f58019f65b1d810f7d668832e73a2ee59b599f 8.1.0#0 2022-01-05 b87c730e96fc6e7c9f55bb1cf31f58a838073b68 8.0.1#0 2021-09-07 e67f76161cc087650ede0d8b834312b5a430d675 7.1.3#5 2021-07-01 9a7d5a29dd0e066ff4a7dbeba4bd8af4daae8b60
...
4. 创建文件 vcpkg.json/CMakeLists.txt/main.cpp,本示例使用fmt、sqlite3和zlib,实现打印各端口版本的功能。
vcpkg.json
{ "name": "test-manifest", "version": "1.0.0", "description": "Test manifest using the specified version. ", "dependencies": [ "fmt", { "name": "sqlite3", "version>=": "3.37.2#1" }, { "name": "zlib", "version>=": "1.2.11#10" } ], "builtin-baseline": "b87c730e96fc6e7c9f55bb1cf31f58a838073b68", "overrides": [ { "name": "zlib", "version": "1.2.11#11" } ] }
注: builtin-baseline 为 34f072dfcb8b9356e6ed2c74947f87390ddb2706 的提交中,提交日期为 "2022-01-05" ,各依赖项的版本如下:
- fmt 的 version 是 "8.1.0#0";
- sqlite3 的 version 是 "3.37.0#1";
- zlib 的 version 是 "1.2.11#13"。
CMakeLists.txt
cmake_minimum_required(VERSION 3.18) project(test-manifest CXX) add_executable(main main.cpp) find_package(fmt CONFIG REQUIRED) find_package(ZLIB REQUIRED) find_package(unofficial-sqlite3 CONFIG REQUIRED) target_link_libraries(main PRIVATE fmt::fmt ZLIB::ZLIB unofficial::sqlite3::sqlite3)
main.cpp
#include <iostream> #include <fmt/core.h> #include <sqlite3.h> #include <zlib.h> int main() { fmt::print("Version of fmt: {}\n" "Version of sqlite3: {}\n" "Version of zlib: {}\n", FMT_VERSION, sqlite3_libversion(), ZLIB_VERSION); return 0; }
5. 在 build 目录下打开 cmd 窗口,配置 CMake 工程
"C:\Program Files\CMake\bin\cmake.exe" "E:\test_manifest" -G "Visual Studio 16 2019" -A x64 -DVCPKG_TARGET_TRIPLET=x64-windows "-DCMAKE_TOOLCHAIN_FILE=E:/src/test/vcpkg/scripts/buildsystems/vcpkg.cmake"
输出:
-- Running vcpkg install Detecting compiler hash for triplet x64-windows... The following packages will be built and installed: fmt[core]:x64-windows -> 8.1.0 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\fmt\3aa440d7c335eb584170ae5fdf4eea6a17c900ee sqlite3[core]:x64-windows -> 3.37.2#1 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda * vcpkg-cmake[core]:x64-windows -> 2021-12-20 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake\a35eb7c761372dc64526d59fa918a13c0dfbba1b * vcpkg-cmake-config[core]:x64-windows -> 2021-12-28 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake-config\e33152002c946b93a0262931ba8bf54a2e6ab9ad zlib[core]:x64-windows -> 1.2.11#11 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8 Additional packages (*) will be modified to complete this operation. Restored 3 package(s) from C:\Users\UserName\AppData\Local\vcpkg\archives in 233.4 ms. Use --debug to see more details. Installing 1/5 vcpkg-cmake-config:x64-windows... Elapsed time to handle vcpkg-cmake-config:x64-windows: 8.51 ms Installing 2/5 vcpkg-cmake:x64-windows... Elapsed time to handle vcpkg-cmake:x64-windows: 17.89 ms Installing 3/5 fmt:x64-windows... Elapsed time to handle fmt:x64-windows: 42.7 ms Installing 4/5 sqlite3:x64-windows... Building sqlite3[core]:x64-windows... -- Installing port from location: E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda -- Downloading https://sqlite.org/2021/sqlite-amalgamation-3370100.zip -> sqlite-amalgamation-3370100.zip... -- Extracting source E:/src/test/vcpkg/downloads/sqlite-amalgamation-3370100.zip -- Applying patch fix-arm-uwp.patch -- Using source at E:/src/test/vcpkg/buildtrees/sqlite3/src/3370100-a1573e1602.clean -- Found external ninja('1.10.2'). -- Configuring x64-windows -- Building x64-windows-dbg -- Building x64-windows-rel -- Fixing pkgconfig file: E:/src/test/vcpkg/packages/sqlite3_x64-windows/lib/pkgconfig/sqlite3.pc -- Using cached msys-mingw-w64-i686-pkg-config-0.29.2-3-any.pkg.tar.zst. -- Using cached msys-mingw-w64-i686-libwinpthread-git-9.0.0.6373.5be8fcd83-1-any.pkg.tar.zst. -- Using msys root at E:/src/test/vcpkg/downloads/tools/msys2/9a1ec3f33446b195 -- Fixing pkgconfig file: E:/src/test/vcpkg/packages/sqlite3_x64-windows/debug/lib/pkgconfig/sqlite3.pc -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "C:\Users\UserName\AppData\Local\vcpkg\archives\36\363b231a44c799fbfd8abf5101ae0384b3df91cbeed9003875bf90cce770d0ae.zip" Elapsed time to handle sqlite3:x64-windows: 16.25 s Installing 5/5 zlib:x64-windows... Building zlib[core]:x64-windows... -- Installing port from location: E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8 -- Using cached zlib1211.tar.gz. -- Cleaning sources at E:/src/test/vcpkg/buildtrees/zlib/src/1.2.11-e9a093319e.clean. Use --editable to skip cleaning for the packages you specify. -- Extracting source E:/src/test/vcpkg/downloads/zlib1211.tar.gz -- Applying patch cmake_dont_build_more_than_needed.patch -- Applying patch 0001-Prevent-invalid-inclusions-when-HAVE_-is-set-to-0.patch -- Applying patch add_debug_postfix_on_mingw.patch -- Applying patch 0002-android-build-mingw.patch -- Using source at E:/src/test/vcpkg/buildtrees/zlib/src/1.2.11-e9a093319e.clean -- Found external ninja('1.10.2'). -- Configuring x64-windows -- Building x64-windows-dbg -- Building x64-windows-rel -- Installing: E:/src/test/vcpkg/packages/zlib_x64-windows/share/zlib/vcpkg-cmake-wrapper.cmake -- Fixing pkgconfig file: E:/src/test/vcpkg/packages/zlib_x64-windows/lib/pkgconfig/zlib.pc -- Using cached msys-mingw-w64-i686-pkg-config-0.29.2-3-any.pkg.tar.zst. -- Using cached msys-mingw-w64-i686-libwinpthread-git-9.0.0.6373.5be8fcd83-1-any.pkg.tar.zst. -- Using msys root at E:/src/test/vcpkg/downloads/tools/msys2/9a1ec3f33446b195 -- Fixing pkgconfig file: E:/src/test/vcpkg/packages/zlib_x64-windows/debug/lib/pkgconfig/zlib.pc -- Installing: E:/src/test/vcpkg/packages/zlib_x64-windows/share/zlib/copyright -- Performing post-build validation -- Performing post-build validation done Stored binary cache: "C:\Users\UserName\AppData\Local\vcpkg\archives\4a\4af3c4b2f08152e69bc8d8854fdcd3290083506702a7cbd84d92c33c9b7f67f2.zip" Elapsed time to handle zlib:x64-windows: 8.092 s Total elapsed time: 31.25 s The package fmt provides CMake targets: find_package(fmt CONFIG REQUIRED) target_link_libraries(main PRIVATE fmt::fmt) # Or use the header-only version find_package(fmt CONFIG REQUIRED) target_link_libraries(main PRIVATE fmt::fmt-header-only) sqlite3 provides CMake targets: # this is heuristically generated, and may not be correct find_package(unofficial-sqlite3 CONFIG REQUIRED) target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3) The package zlib is compatible with built-in CMake targets: find_package(ZLIB REQUIRED) target_link_libraries(main PRIVATE ZLIB::ZLIB) -- Running vcpkg install - done -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044. -- The CXX compiler identification is MSVC 19.29.30146.0 -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found ZLIB: optimized;E:/test_manifest/build/vcpkg_installed/x64-windows/lib/zlib.lib;debug;E:/test_manifest/build/vcpkg_installed/x64-windows/debug/lib/zlibd.lib (found version "1.2.11") -- Configuring done -- Generating done -- Build files have been written to: E:/test_manifest/build
请重点关注以下输出:
The following packages will be built and installed: fmt[core]:x64-windows -> 8.1.0 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\fmt\3aa440d7c335eb584170ae5fdf4eea6a17c900ee sqlite3[core]:x64-windows -> 3.37.2#1 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\sqlite3\f49ad405225c6326558adb79f2082814002a8cda * vcpkg-cmake[core]:x64-windows -> 2021-12-20 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake\a35eb7c761372dc64526d59fa918a13c0dfbba1b * vcpkg-cmake-config[core]:x64-windows -> 2021-12-28 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake-config\e33152002c946b93a0262931ba8bf54a2e6ab9ad zlib[core]:x64-windows -> 1.2.11#11 -- E:\src\test\vcpkg\buildtrees\versioning_\versions\zlib\ad5b59cd3722af26f09921297562dfc76c83dbf8
输出分析:
- 对于 fmt 来说: 只有 builtin-baseline 起版本约束作用,所以是 "8.1.0";
- 对于 sqlit3 来说: 有 version>= 和 builtin-baseline 两个版本约束,它会比较这两个约束,从中选择最高的执行,所以是 "3.37.2#1";
- 对于 zlib 来说: 有 version>= 和 builtin-baseline 和 overrides 三个版本约束,它会选择 overrides 执行,因为 overrides 有强制覆盖其他版本的作用,所以是 "1.2.11#11"。
- 依赖项的安装位置:E:/test_manifest/build/vcpkg_installed/x64-windows/ 。
需要注意的是,如果有多个依赖项,我们同样要查看这个 commit id 对其他依赖项的影响,如果不为其他依赖做另外的版本约束,它们就会默认安装 builtin-baseline 指定的 commit 提交日期之前发布的最新版本。 例如: 我们为 fmt 选择了 "8.1.0#0",发布日期是 "2022-01-05",如果不为 sqlite3 和 zlib 做其他版本约束(version>= 或者 overrides ),它们就会安装 "2022-01-05" 之前的最后一个版本, 即 "2021-12-21" 发布的 "3.37.0#1" 和 "2021-10-11" 发布的 "1.2.11#13"。
E:/src/test/vcpkg>./vcpkg x-history sqlite3
Version Date VCPKG Commit 3.39.2#0 2022-08-12 7d21ec61e627690a1267f97efdb4c9ee31b529d9 3.39.1#0 2022-07-20 917a86aeb3e8926ed955e93671cf70b268ebb67d 3.37.2#2 2022-06-30 250d199aa1e246e386858422abbd44b57a25f701 3.37.2#1 2022-02-16 7f3e168d8fe2c7df5cba99a62f4059468c650e37 3.37.1#0 2022-02-01 4f2d305da8a46dcaf0afbab5a59385a0441342e7 3.37.0#1 2021-12-21 1cd11e2ce7cbcada0a696b7bb32fc011605796f5
...
E:/src/test/vcpkg>./vcpkg x-history zlib
Version Date VCPKG Commit 1.2.12#1 2022-05-09 659b6b5eeacb50ffe25856413960d231ce1a4b58 1.2.12#0 2022-03-30 9b064ff07b987c8f213fdbf4358122fd4fe38ef1 1.2.11#13 2021-10-11 822eb4b4ce82d76fa5bcfa8f5774f47db4e01e8b
...
注意:
- 如果不指定任何版本,那么会默认选择 vcpkg 的最新提交版本;
- 如果指定了 version>= 或者 overrides 中的任何一个(或者同时指定这两个),那么必须指定 builtin-baseline ,因为它们对 builtin-baseline 有依赖性;
- 可以单独使用 builtin-baseline ,它对其他的版本约束没有依赖性;
- version>= 主要用于表示需要使用的最低版本,只能在 dependencies[] 中使用;
- vcpkg 不会比较 version 类型不同的版本,例如: 同一个库的两个版本 "version-string: 8.1.0" 与 "version: 8.1.2" 类型不同,所以不能比较。
所有的约束组合有以下几种:
- 不指定任何约束(默认选择 vcpkg 的最新提交版本);
- builtin-baseline;
- version>= 和 builtin-baseline;
- overrides 和 builtin-baseline;
- version>= 和 builtin-baseline 和 overrides。
6. 构建 CMake 工程
"C:\Program Files\CMake\bin\cmake.exe" --build .
输出:
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. Checking Build System Building Custom Rule E:/test_manifest/CMakeLists.txt main.cpp main.vcxproj -> E:\test_manifest\build\Debug\main.exe Building Custom Rule E:/test_manifest/CMakeLists.txt
7. 运行可执行文件
.\Debug\main.exe
输出:
Version of fmt: 80100 Version of sqlite3: 3.37.1 Version of zlib: 1.2.11
Manifest模式 - MSBuild 工程
在Manifest模式下使用 MSBuild 工程请参阅:https://www.cnblogs.com/vcpkg/p/15019899.html。
标签:src,示例,--,x64,Manifest,windows,vcpkg,test From: https://www.cnblogs.com/vcpkg/p/16791033.html