A single CMake script that makes vcpkg available before
project(): it uses an existing vcpkg or clones and bootstraps one, and sets vcpkg up as the
toolchain. After that, packages come from plain find_package(), and dependencies are declared
the way vcpkg documents it, in a vcpkg.json.
Requires CMake 3.24 or newer, git and a C++ compiler.
Add this to your top-level CMakeLists.txt, before the first project():
cmake_minimum_required(VERSION 3.24) include(FetchContent) FetchContent_Declare(vcpkg_bootstrap GIT_REPOSITORY https://github.com/mrom1/vcpkg-bootstrap.git GIT_TAG main # better: a release tag or commit hash ) # Optional: isolated mode keeps vcpkg and all packages in the build folder. option(VCPKG_BOOTSTRAP_ISOLATED "Virtual Environment Mode" OFF) FetchContent_MakeAvailable(vcpkg_bootstrap) project(myproject CXX) find_package(fmt CONFIG REQUIRED)
and list your dependencies in a vcpkg.json
next to it:
{
"dependencies": [ "fmt" ]
}That's all: configure as usual (cmake -S . -B build). A complete, working project lives on the
example branch.
vcpkg-bootstrap.cmake is self-contained. Copy it into your project and include it before
project():
cmake_minimum_required(VERSION 3.24) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/vcpkg-bootstrap.cmake) project(myproject CXX)
VCPKG_BOOTSTRAP_ISOLATED |
Where vcpkg comes from | Where packages are installed |
|---|---|---|
OFF (default) |
An existing vcpkg if there is one (see below), otherwise one shared clone in a per-user cache, used by all your projects | vcpkg's defaults: <build>/vcpkg_installed with a vcpkg.json, otherwise <vcpkg>/installed |
ON |
A private clone in <build>/vcpkg; VCPKG_ROOT is ignored |
Everything stays in the build folder |
With ON, deleting the build folder removes vcpkg and every package it installed; nothing is
written outside of it. That includes vcpkg's own caches, which otherwise live in your user profile:
| Shared mode (vcpkg's defaults) | Isolated mode | |
|---|---|---|
| Binary cache (built packages, reused across build folders) | %LOCALAPPDATA%\vcpkg\archives, ~/.cache/vcpkg/archives |
<build>/vcpkg-bootstrap/archives |
| Git registry cache | %LOCALAPPDATA%\vcpkg\registries, ~/.cache/vcpkg/registries |
<build>/vcpkg-bootstrap/registries |
| Downloads (sources, tools) | <vcpkg>/downloads |
<build>/vcpkg/downloads |
In isolated mode, settings for these in the environment (VCPKG_BINARY_SOURCES,
VCPKG_DEFAULT_BINARY_CACHE, X_VCPKG_REGISTRIES_CACHE, VCPKG_DOWNLOADS) are ignored, like
VCPKG_ROOT; the configure says so. The price is a full vcpkg clone and bootstrap per build
folder, and every package is built once per build folder.
Choose the mode on the first configure of a build folder (-DVCPKG_BOOTSTRAP_ISOLATED=ON). It is
remembered; changing it later in the same build folder is refused with an error (see
Troubleshooting).
A build folder is usually deeper than vcpkg's own directory, and some ports exceed Windows' 260
character path limit there (see Troubleshooting). To give ports more room, the
script has vcpkg build them with short intermediate directories on Windows: CMake 4.2+ (current
vcpkg) puts objects into <build>/.o/<hash>/<hash>.obj instead of
<build>/CMakeFiles/<target>.dir/<source>.obj (CMAKE_INTERMEDIATE_DIR_STRATEGY=SHORT). It is
passed through vcpkg's build environment (VCPKG_KEEP_ENV_VARS), does not change the package ABI
and does not affect your own project; set CMAKE_INTERMEDIATE_DIR_STRATEGY in your environment to
override it. Ports whose own file names are too long for the location still fail.
The first match wins:
| # | Source | Modified by the script? |
|---|---|---|
| 1 | -DVCPKG_BOOTSTRAP_ROOT_DIR=<dir>: an existing vcpkg checkout |
never |
| 2 | Isolated mode: <build>/vcpkg |
cloned, updated, bootstrapped |
| 3 | The VCPKG_ROOT environment variable: your own vcpkg, a CI runner's vcpkg, ... |
never |
| 4 | A shared clone in <VCPKG_BOOTSTRAP_INSTALL_DIR>/vcpkg or the per-user cache |
cloned, updated, bootstrapped |
Per-user cache: %LOCALAPPDATA%\vcpkg-bootstrap\vcpkg on Windows,
$XDG_CACHE_HOME/vcpkg-bootstrap/vcpkg or ~/.cache/vcpkg-bootstrap/vcpkg on Linux and macOS.
It is cloned and bootstrapped once and shared by all projects and build folders.
- A vcpkg toolchain you pass yourself (
-DCMAKE_TOOLCHAIN_FILE=<vcpkg>/scripts/buildsystems/vcpkg.cmake, a preset, an IDE integration) always wins: the script then only uses it. - Visual Studio sets
VCPKG_ROOTto its bundled vcpkg, which has no ports of its own and only works with avcpkg.jsonthat has abuiltin-baseline(vcpkg's own rule). Without such a manifest the script says so and uses the shared clone instead. - A
VCPKG_ROOTthat is not a vcpkg root (e.g. a leftover after moving or deleting a vcpkg) is ignored with a note. - vcpkg checkouts the script did not create are only read. If one is older than your
builtin-baseline, you get a warning telling you to update it. - Clones made by the script are full clones (vcpkg's versioning needs the history). A file lock makes parallel configures safe. To start over, delete the directory.
-
vcpkg.json(recommended). vcpkg's manifest mode: everything listed is installed during configure into<build>/vcpkg_installed. -
vcpkg_bootstrap_install(<port>...)afterproject(), anywhere in the tree, for projects without a manifest:project(myproject CXX) vcpkg_bootstrap_install(fmt nlohmann-json) find_package(fmt CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED)
Port names use vcpkg syntax (
fmt,fmt[core]). This isvcpkg installin classic mode, so the packages go to<vcpkg>/installed. In manifest mode the call does nothing and warns about ports missing fromvcpkg.json. -
Plain
find_package(<Name> ... REQUIRED)without a manifest (an extra of this project, not vcpkg behaviour): if<Name>is not found anywhere else (system, other toolchains, prefix paths), the vcpkg port that provides it is installed. It is deliberately strict: never forfind_package(... QUIET)withoutREQUIRED, never meta-ports likeboost, never guessed names. If a package is not picked up, usevcpkg_bootstrap_install(<port>).
- Ports: set
builtin-baselineinvcpkg.jsonfor reproducible builds (vcpkg x-update-baseline --add-initial-baseline). Without a baseline, versions come from whatever commit the vcpkg checkout is on — not "the latest". - vcpkg itself is put on the commit of your baseline: a new clone and the isolated clone
(
<build>/vcpkg) are checked out at exactly the baseline. vcpkg's own scripts and tools are part of every package's ABI hash, so this keeps builds reproducible and lets every build folder reuse packages from vcpkg's binary cache instead of rebuilding them. - The shared clone serves other projects too, so it only ever moves forward: if it does not contain your baseline yet, it is fetched once, moved to the baseline and bootstrapped again.
- Otherwise configure does not go to the network for vcpkg itself.
- vcpkg itself:
-DVCPKG_BOOTSTRAP_REF=<tag or commit>checks out that vcpkg release in a clone made by the script. The shared clone is shared, so give projects that pin different refs their ownVCPKG_BOOTSTRAP_INSTALL_DIR(or use isolated mode). - The script: pin
GIT_TAGto a release tag or commit hash.
A toolchain you already use (-DCMAKE_TOOLCHAIN_FILE, a preset, or the CMAKE_TOOLCHAIN_FILE
environment variable) is not replaced: it is passed to vcpkg as VCPKG_CHAINLOAD_TOOLCHAIN_FILE,
so Android, Emscripten and embedded toolchains keep working. Select the vcpkg triplet as usual,
e.g. -DVCPKG_TARGET_TRIPLET=arm64-android.
conan_toolchain.cmakeas toolchain is chainloaded; packages found through Conan are used from Conan, and thefind_package()fallback only fills in what is missing.- If another dependency provider is configured (e.g. cmake-conan
in
CMAKE_PROJECT_TOP_LEVEL_INCLUDES), it takes precedence and the fallback is off (the script says so).vcpkg.jsonandvcpkg_bootstrap_install()keep working.
Once vcpkg is cloned and contains your baseline, configuring only needs the network for ports that vcpkg has not built or cached yet. To use a local copy of this repository instead of downloading it:
cmake -S . -B build -DFETCHCONTENT_SOURCE_DIR_VCPKG_BOOTSTRAP=/path/to/vcpkg-bootstrap- "configured before without vcpkg's toolchain" / "The vcpkg location changed": CMake
reads the toolchain file only on the first configure of a build folder. Delete the build
folder (or its
CMakeCache.txt) and configure again. - "The environment points to another vcpkg … keeping …": the same build folder was
configured from a different shell, e.g. once from a plain terminal and once from a Visual
Studio developer prompt (or an IDE that loads it, like VS Code's CMake Tools), which sets
VCPKG_ROOTto its bundled vcpkg. When only the environment differs, the vcpkg the folder was configured with is kept. A location you choose with a setting (VCPKG_BOOTSTRAP_ROOT_DIR,VCPKG_BOOTSTRAP_ISOLATED,VCPKG_BOOTSTRAP_INSTALL_DIR) still fails with the error above. - "older than builtin-baseline": your own vcpkg (
VCPKG_ROOT,VCPKG_BOOTSTRAP_ROOT_DIR) is outdated:git -C <vcpkg> pull, then run itsbootstrap-vcpkgscript. - "These ports will likely fail to build from source on Windows": some ports (Qt, Boost, ...)
exceed Windows' 260 character path limit when vcpkg builds them in a deep directory; the limits
are vcpkg's own (
vcpkg_buildpath_length_warning). The script checks them before vcpkg runs, for ports not yet installed in the build folder (overlay ports included). The limit comes from MSVC: its compiler (cl.exe) cannot open longer paths, not even with Windows' long path support enabled. Packages restored from vcpkg's binary cache are fine. To build such ports, keep vcpkg in a short directory, as vcpkg recommends: shared mode with-DVCPKG_BOOTSTRAP_INSTALL_DIR=<short directory>(isolated mode keeps vcpkg in the build folder). Or build with a compiler that supports long paths, such as clang-cl, with Windows' long path support enabled, for the project and all its C++ dependencies (C++ libraries built with MSVC and clang-cl cannot always be mixed; Qt cannot). - Included after
project(): the script only warns and does nothing; move it before the firstproject()call. - A package is not installed automatically: list it in
vcpkg.json, or callvcpkg_bootstrap_install(<port>)beforefind_package().
- Dependencies declared with
FetchContent_Declare(... FIND_PACKAGE_ARGS ...)go throughfind_package()first, so without a manifest they may come from vcpkg instead of being fetched. - Tool ports whose usage declares a CMake package (e.g.
Python3) are installed when that package is really missing.
| Variable | Default | Meaning |
|---|---|---|
VCPKG_BOOTSTRAP_ISOLATED |
OFF |
vcpkg and all packages in the build folder; VCPKG_ROOT ignored |
VCPKG_BOOTSTRAP_ROOT_DIR |
— | Use this existing vcpkg as-is (wins over everything) |
VCPKG_BOOTSTRAP_INSTALL_DIR |
per-user cache | Shared mode: manage vcpkg in <dir>/vcpkg |
VCPKG_BOOTSTRAP_REF |
— | vcpkg tag or commit for a clone made by the script |
VCPKG_BOOTSTRAP_DISABLE |
OFF |
Do nothing |
VCPKG_BOOTSTRAP_ROOT |
output | The vcpkg root in use (cache) |
Relative paths are relative to the top-level source directory. vcpkg's own variables
(VCPKG_MANIFEST_DIR, VCPKG_MANIFEST_MODE, VCPKG_TARGET_TRIPLET, VCPKG_INSTALLED_DIR,
VCPKG_OVERLAY_PORTS, VCPKG_OVERLAY_TRIPLETS, VCPKG_INSTALL_OPTIONS) work as documented by vcpkg.
Function: vcpkg_bootstrap_install(<port>...) — see Getting packages.