教程:使用注册表基线安装特定版本的 Boost 库

vcpkg 将 Boost 库集作为单个包提供,以便于仅依赖于 Boost 包有限子集的用户使用。 但是,这种方法有弊端。 想要锁定其 Boost 依赖项的特定版本的用户需要替代其依赖的每个 Boost 包的版本。

但是,使用注册表基线和包模式可以简单解决此问题。

先决条件

  • 终端
  • C++ 编译器
  • vcpkg

问题

假设你有一个依赖于 boost-optional 的项目,并且想要锁定 Boost 库的版本 1.80.0。 因此,你为依赖项创建了替代,如下所示:

vcpkg.json

{
    "dependencies": [ "boost-optional" ],
    "overrides": [
        { "name": "boost-optional", "version": "1.80.0" }
    ]
}

vcpkg-configuration.json

{
  "default-registry": {
    "kind": "git",
    "repository": "https://github.com/Microsoft/vcpkg",
    "baseline": "3265c187c74914aa5569b75355badebfdbab7987"
  }
}

运行 vcpkg install 时,你会注意到,当其他 Boost 依赖项使用基线版本 (boost-optional),只有 1.80.0 的版本被锁定为 1.83.0

Fetching registry information from https://github.com/Microsoft/vcpkg (HEAD)...
Detecting compiler hash for triplet x64-linux...
The following packages will be built and installed:
  * boost-assert:x64-linux -> 1.83.0
  * boost-config:x64-linux -> 1.83.0
  * boost-core:x64-linux -> 1.83.0
  * boost-detail:x64-linux -> 1.83.0
  * boost-io:x64-linux -> 1.83.0
  * boost-move:x64-linux -> 1.83.0
    boost-optional:x64-linux -> 1.80.0
  * boost-predef:x64-linux -> 1.83.0
  * boost-preprocessor:x64-linux -> 1.83.0
  * boost-static-assert:x64-linux -> 1.83.0
  * boost-throw-exception:x64-linux -> 1.83.0
  * boost-type-traits:x64-linux -> 1.83.0
  * boost-uninstall:x64-linux -> 1.83.0
  * boost-utility:x64-linux -> 1.83.0
  * boost-vcpkg-helpers:x64-linux -> 1.83.0
Additional packages (*) will be modified to complete this operation.

1 - 获取 Boost 版本基线

运行以下命令以查看已修改 boost-optional 包的版本数据库的提交历史记录:

git log "--format=%H %cd %s" --date=short --left-only -- versions/b-/boost-optional.json

此时会看到与下面类似的输出:

caa7579a1c48e2ca770f6ccf98cb03db95642631 2023-09-13 [boost] update to v1.83.0 (#33597)
5d3798ac7388ca66c169773e46103b14077b76a4 2023-06-06 [boost] Remove port version constraints (#31572)
501db0f17ef6df184fcdbfbe0f87cde2313b6ab1 2023-04-15 [boost-build] Fix SHA512 and add MSVC workaround. (#30884)
bedfdb774cfbe47da202169046ca15441a213f3e 2023-04-15 [Boost] Update Boost ports to 1.82#0 (#30856)
9484a57dd560b89f0a583be08af6753611c57fd5 2023-02-24 Update vcpkg-tool to 2023-02-16. (#29664)
6aa38234d08efefc55b70025cf6afc2212e78e15 2023-02-01 [boost] Fix generate ports to match the tree. (#29338)
6d41737729b170cb7d323a4fddd21607c9237636 2022-12-20 [boost] update to 1.81.0 (#28356)
5ba2b95aea2a39aa89444949c7a047af38c401c1 2022-10-18 [boost] Add version constraints (#26817)
8424da584e59e05956913bf96f87654aa3096c7e 2022-08-25 [boost] update to 1.80.0 (#26233)
96ec7fb25da25e0463446e552d59715a47c95e73 2022-04-21 [boost] update to 1.79.0 (#24210)
76d4836f3b1e027758044fdbdde91256b0f0955d 2022-01-10 [boost] update to 1.78.0 (#21928)
cc471dc0f59b7b2066d6172c2893419412327a7a 2021-09-27 [boost] update to 1.77.0 (#19556)
761c81d43335a5d5ccc2ec8ad90bd7e2cbba734e 2021-07-07 [boost] update to 1.76.0 (#17335)
68a74950d0400f5a803026d0860f49853984bf11 2021-01-21 [vcpkg] Rename `port_versions` to `versions` (#15784)

可以看到,版本 1.80.0 的提交已列出。 在下一步中,你将使用该提交作为所有 Boost 包的基线。

2 - 使用基线替代 Boost 包

你可以使用注册表基线来锁定版本集,而不是为所有相关的 Boost 依赖项添加替代。 修改 vcpkg-configuration.json 文件,如下所示:

vcpkg-configuration.json

{
  "default-registry": {
    "kind": "git",
    "repository": "https://github.com/Microsoft/vcpkg",
    "baseline": "3265c187c74914aa5569b75355badebfdbab7987"
  },
  "registries": [
    {
      "kind": "git",
      "repository": "https://github.com/Microsoft/vcpkg",
      "baseline": "8424da584e59e05956913bf96f87654aa3096c7e",
      "packages": [ "boost*", "boost-*"]
    }
  ]
}

配置文件包括两个注册表定义。 在撰写本文时使用最新提交在 default-registry 处指向 vcpkg 特选注册表的 https://github.com/Microsoft/vcpkg。 另一个注册表也指向 vcpkg 特选注册表,但在以前的基线下,并且仅限于匹配 boost*boost-* 模式的包。

如果运行 vcpkg install 命令,则应注意到此次所有 Boost 库都使用了版本 1.80.0

Fetching registry information from https://github.com/Microsoft/vcpkg (HEAD)...
Detecting compiler hash for triplet x64-linux...
The following packages will be built and installed:
  * boost-assert:x64-linux -> 1.80.0
  * boost-config:x64-linux -> 1.80.0
  * boost-core:x64-linux -> 1.80.0
  * boost-detail:x64-linux -> 1.80.0
  * boost-io:x64-linux -> 1.80.0
  * boost-move:x64-linux -> 1.80.0
    boost-optional:x64-linux -> 1.80.0
  * boost-predef:x64-linux -> 1.80.0
  * boost-preprocessor:x64-linux -> 1.80.0
  * boost-static-assert:x64-linux -> 1.80.0
  * boost-throw-exception:x64-linux -> 1.80.0
  * boost-type-traits:x64-linux -> 1.80.0
  * boost-uninstall:x64-linux -> 1.80.0
  * boost-utility:x64-linux -> 1.80.0
  * boost-vcpkg-helpers:x64-linux -> 1.80.0
  * boost-winapi:x64-linux -> 1.80.0
Additional packages (*) will be modified to complete this operation.

同一方法可用于其他元包,例如 qt

后续步骤

以下是接下来要尝试的一些其他任务: