In a CMake build using vcpkg.cmake, how is VCPKG_TARGET_TRIPLET chosen, and why must it and the other VCPKG_ settings be fixed before project()?
answer
- explicit value or detection
- generator platform and system name
- never a static triplet by default
- change it, delete the cache
basics
~20 svcpkg.cmake uses VCPKG_TARGET_TRIPLET if set; otherwise it builds a name like x64-windows or arm64-osx from the generator platform, processor and system. It reads every VCPKG_ setting inside the first project(), so they must be set earlier, and a changed triplet needs a fresh cache.
solid answer
~50 sIf `VCPKG_TARGET_TRIPLET` is defined before `project()` (preset, `-D` or `set()`), vcpkg.cmake lifts it into the cache and uses it. Otherwise it **detects** one: the architecture from `CMAKE_GENERATOR_PLATFORM` (for example `-A x64` or `-A ARM64` with Visual Studio generators), the Visual Studio generator's default, `CMAKE_OSX_ARCHITECTURES`, the MSVC `cl` on the `PATH` or the host processor, and the platform from `CMAKE_SYSTEM_NAME` or the host, giving names such as `x64-windows`, `x64-linux` or `arm64-osx`. Detection never picks a static Windows triplet, so a statically linked build must name `x64-windows-static` explicitly. `VCPKG_HOST_TRIPLET` likewise picks the triplet for host tools. All of these, plus `VCPKG_MANIFEST_FEATURES`, `VCPKG_OVERLAY_PORTS`, `VCPKG_OVERLAY_TRIPLETS` and `VCPKG_INSTALL_OPTIONS`, are read when the toolchain runs inside the first `project()`, so later assignments are ignored. The docs say a changed triplet requires deleting the cache, and flipping manifest mode in an existing build directory is a fatal error.
code
json · 15 lines{
"version": 3,
"configurePresets": [
{
"name": "windows-static",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/windows-static",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"VCPKG_TARGET_TRIPLET": "x64-windows-static",
"VCPKG_MANIFEST_FEATURES": "tests"
}
}
]
}go deeper
Recall that VCPKG_TARGET_TRIPLET selects which vcpkg libraries the build uses and that it must be set before project().
Explain how the triplet is detected from the generator platform and system name, and why detection never yields a static Windows triplet.
Show you have untangled stale caches: a triplet changed in place, features appended too late, manifest mode flipped, each fixed by a fresh configure with presets.
Make triplets and features a property of named presets, so every developer and CI job builds a configuration the team actually supports.
## When the toolchain reads its settings vcpkg.cmake is a toolchain file, so CMake runs it inside the first `project()` call. Everything it reads must exist by then. The vcpkg docs say it directly: all vcpkg-affecting variables must be defined before the first `project()`, in a preset's `cacheVariables`, on the command line, or with `set()` statements above `project()`. ## How the target triplet is chosen The script's logic, simplified: 1. If `VCPKG_TARGET_TRIPLET` is set, force it into the cache and use it. 2. Otherwise derive an **architecture**: - from `CMAKE_GENERATOR_PLATFORM` (`Win32` gives `x86`, `x64`, `ARM`, `ARM64`), which Visual Studio generators take from `-A`; - from the Visual Studio generator name and its default platform; - on macOS from the first entry of `CMAKE_OSX_ARCHITECTURES` when it is defined; - otherwise from the MSVC `cl` found on the `PATH` (its directory names the architecture), and failing that from the host processor. 3. Derive a **platform** from `CMAKE_SYSTEM_NAME` or the host system: `windows`, `linux`, `osx`, `ios`, `android`, `uwp`, `mingw-dynamic` and others. 4. Cache `<arch>-<platform>` as `VCPKG_TARGET_TRIPLET`. | Situation | Detected triplet | |---|---| | Visual Studio 2026 generator, x64 | `x64-windows` | | Visual Studio generator with `-A ARM64` | `arm64-windows` | | Ninja on x64 Linux | `x64-linux` | | arm64 Mac | `arm64-osx` | Detection only produces the platform's base name, so on Windows it never yields `x64-windows-static` or `x64-windows-static-md`; those must be named. The triplets themselves, and what they set, are described with triplets and ports. ## The host triplet `VCPKG_HOST_TRIPLET` selects the triplet for `"host": true` dependencies, the tools that run during the build. Left unset, vcpkg picks the native triplet of the machine. Cross builds such as Android set the target triplet and keep the host native. ## The other settings read at the same moment - `VCPKG_MANIFEST_FEATURES`: project features to enable, passed as `--x-feature`; `VCPKG_MANIFEST_NO_DEFAULT_FEATURES` adds `--x-no-default-features`. - `VCPKG_OVERLAY_PORTS` and `VCPKG_OVERLAY_TRIPLETS`: overlay directories, passed through to the install. - `VCPKG_INSTALL_OPTIONS`: any extra install arguments. - `VCPKG_INSTALLED_DIR`: where packages go, `${CMAKE_BINARY_DIR}/vcpkg_installed` by default. - `VCPKG_MANIFEST_MODE` and `VCPKG_MANIFEST_INSTALL`: whether manifest mode is on, and whether the install runs during configure. A common pattern turns CMake options into features before `project()`: ```cmake option(BUILD_TESTING "Build tests" OFF) if(BUILD_TESTING) list(APPEND VCPKG_MANIFEST_FEATURES "tests") endif() project(app CXX) ``` ## Changing settings in an existing build tree - **Triplet**: the docs state that after changing `VCPKG_TARGET_TRIPLET` you must delete the cache and reconfigure; the cached value, search paths and detected compiler all belong to the old choice. - **Manifest mode**: vcpkg.cmake records the initial mode, and turning it on or off later stops the configure with a fatal error asking you to delete the build directory. - **Settings that change the ABI hash** (triplet, features, overlays) may also need a reconfigure so the install runs again. ## Debug and Release search order When `CMAKE_BUILD_TYPE` is Debug or unset, the usual case with multi-config generators, vcpkg.cmake puts the `debug` paths of the installed tree before the release ones; for any other build type the release paths come first. Both are always searched, and the packages' config files then pick per-configuration libraries for their imported targets. ## Several configurations side by side Teams that ship one product for several targets keep one build directory per triplet, because the cached triplet, the detected compiler and the installed tree all belong to one choice. Two details make that cheaper: - `VCPKG_INSTALLED_DIR` can point several build directories at one shared installed root; packages for each triplet go into their own subdirectory, so they do not collide. - Binary caching means the second build directory for the same triplet restores packages rather than rebuilding them, as long as the compiler and inputs match. The MSBuild integration follows the same idea with `VcpkgTriplet`. When unset it is deduced from the Visual Studio settings, and the docs note that deduction only produces triplets with dynamic library and dynamic CRT linkage, so static builds name the triplet there too. `VcpkgManifestInstalledBaseDir` lets a solution with several triplets share a base directory, with the triplet name appended per project. ## Practical setup Put the triplet and features in a configure preset per target, such as `windows-static` with `VCPKG_TARGET_TRIPLET` set to `x64-windows-static`, so developers and CI select a known configuration with `--preset` instead of remembering `-D` flags.
- Why does setting VCPKG_TARGET_TRIPLET after project() change nothing?The toolchain already ran inside `project()`: it detected a triplet, installed packages for it and set the search paths. A later `set()` only changes a variable nobody reads again. Put it in a preset, on the command line or above `project()`, in a fresh build directory.
- How do you build a static Windows configuration without editing CMakeLists.txt?Pass `-DVCPKG_TARGET_TRIPLET=x64-windows-static` on the first configure, or set it in a preset, because detection always yields the dynamic `x64-windows`. Your own targets must also use the static MSVC runtime to match.
- vcpkg.cmake picks x64-windows-static automatically when the project builds static libraries.
- VCPKG_TARGET_TRIPLET can be changed in an existing build directory and takes effect on the next build.
- VCPKG_MANIFEST_FEATURES can be appended after project() to enable test dependencies.
- The host triplet is always the same as the target triplet.
- Switching VCPKG_MANIFEST_MODE in an existing build directory just reinstalls the packages.