You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止VCPKG清单模式自动更新依赖包?

问题

系统构建速度极慢,每隔几天到一周VCPKG就会自动更新包,引发所有依赖全量重建,部分依赖重建耗时超一天。已设置builtin-baseline锁定VCPKG,但仍有因素触发自动更新。

从CMake输出看,推测清单模式下每次刷新CMake配置时都会调用vcpkg install,希望阻止该自动行为,让VCPKG进入“不安装”状态。

相关配置如下:

vcpkg.json

{
  "name": "myproject",
  "version-string": "1.0.0",
  "dependencies": [
    "zeromq",
    "cppzmq"
  ],
  "builtin-baseline": "871e20cf67ebaa80c048b868be53c8e7375c79f3"
}

CMakePresets.json

{
  "version": 3,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 17,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "Default",
      "displayName": "Default Config",
      "description": "",
      "generator": "Ninja",
      "hidden": true
    },
    {
      "name": "DebugTemplate",
      "displayName": "Debug Template",
      "description": "",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/cmake-build-debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
      },
      "hidden": true
    },
    {
      "name": "WindowsVcpkgTemplate",
      "displayName": "Default Linux Config with hardware",
      "description": "",
      "inherits": "Default",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows-static"
      },
      "environment": {
        "TEMP": "${sourceDir}/Temp",
        "TMP": "${sourceDir}/Temp",
        "VCPKG_BINARY_SOURCES" :"clear;files,${sourceDir}/vcpkg/archives,readwrite"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      },
      "hidden": true
    },
    {
      "name": "WindowsDebug",
      "displayName": "Windows Debug Config",
      "description": "",
      "inherits": [
        "WindowsVcpkgTemplate",
        "DebugTemplate"
      ]
    }
  ]
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(MyProject)
set(CMAKE_CXX_STANDARD 20)

if(WIN32)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<$<CONFIG:Debug>:Debug>")
endif()

find_package(Threads REQUIRED)
find_package(cppzmq REQUIRED)

add_executable(my_project)
target_sources(my_project PRIVATE main.cpp)
target_include_directories(my_project PRIVATE ./)
target_link_libraries(my_project PRIVATE
        Threads::Threads
        cppzmq
)

触发自动安装的CMake输出片段

C:\SomeDirectory\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=RelWithDebInfo --preset WindowsDebug -S C:\DEC\projects\adif-test-harness -B C:\ProjectDirectory\cmake-build-debug
Preset CMake variables:

  CMAKE_TOOLCHAIN_FILE="C:/ProjectDirectory/vcpkg/scripts/buildsystems/vcpkg.cmake"
  VCPKG_TARGET_TRIPLET="x64-windows-static"

Preset environment variables:

  TEMP="C:/DEC/projects/adif-test-harness/Temp"
  TMP="C:/DEC/projects/adif-test-harness/Temp"
  VCPKG_BINARY_SOURCES="clear;files,C:/ProjectDirectory/vcpkg/archives,readwrite"

-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
Detecting compiler hash for triplet x64-windows-static...
The following packages will be rebuilt:
    cppzmq:x64-windows-static -> 4.9.0 -- C:\ProjectDirectory\vcpkg\buildtrees\versioning_\versions\cppzmq\7f4360cc5fa484c4ecd286ef6c545bde4b01bc39
  * vcpkg-cmake:x64-windows -> 2022-12-22 -- C:\ProjectDirectory\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake\1913f86defd2140d0a6751be2d51952e4090efa4
  * vcpkg-cmake-config:x64-windows -> 2022-02-06#1 -- C:\ProjectDirectory\vcpkg\buildtrees\versioning_\versions\vcpkg-cmake-config\8d54cc4f487d51b655abec5f9c9c3f86ca83311f
    zeromq:x64-windows-static -> 4.3.4#6 -- C:\ProjectDirectory\vcpkg\buildtrees\versioning_\versions\zeromq\7bdd3a27ea013a2d3b70032245631f114198cce9
Additional packages (*) will be modified to complete this operation.
Restored 0 package(s) from C:/ProjectDirectory/vcpkg/archives in 1.04 ms. Use --debug to see more details.
解决方案

1. 禁用清单模式自动安装

在CMake配置中添加VCPKG_MANIFEST_NO_INSTALL变量并设置为ON,这样CMake在配置时只会检查依赖是否已安装,不会自动调用vcpkg install。

修改CMakePresets.json中的WindowsVcpkgTemplate的cacheVariables:

"cacheVariables": {
  "CMAKE_TOOLCHAIN_FILE": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake",
  "VCPKG_TARGET_TRIPLET": "x64-windows-static",
  "VCPKG_MANIFEST_NO_INSTALL": "ON"
}

之后需要手动提前安装依赖,在项目根目录运行:

vcpkg install --x-manifest-root=. --triplet=x64-windows-static

2. 彻底锁定VCPKG版本

仅设置builtin-baseline不足以锁定VCPKG自身的更新,如果你的VCPKG是通过Git克隆的仓库,可能会被自动拉取更新。执行以下操作锁定VCPKG到特定版本:

  1. 进入VCPKG目录:
cd ${sourceDir}/vcpkg
  1. 切换到与builtin-baseline匹配的commit:
git checkout 871e20cf67ebaa80c048b868be53c8e7375c79f3
  1. 禁用Git自动拉取:
git config pull.rebase false
# 或者直接移除远程仓库(可选)
git remote remove origin

3. 避免编译器哈希变化触发重建

CMake输出中的Detecting compiler hash表明编译器版本或配置变化会触发包重建。如果是编译器自动更新导致的问题:

  • 固定编译器版本(比如禁用VS自动更新)
  • 临时禁用编译器哈希检查(不推荐长期使用),添加CMake变量:
"cacheVariables": {
  // ...其他变量
  "VCPKG_DISABLE_COMPILER_HASH": "ON"
}

4. 确认二进制缓存生效

检查你的VCPKG_BINARY_SOURCES配置是否正确,确保已安装的包缓存到本地archives目录。运行安装命令时添加--debug可以查看缓存命中情况,确保重建时优先使用缓存而不是重新编译。


内容的提问来源于stack exchange,提问作者Krupip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:00:00