skip to content

How do you write a custom vcpkg triplet, such as release-only static libraries with one port built as a DLL, and make vcpkg find it?

level: middleimportance: nice to knowfreq 25%

answer

  1. copy a built-in triplet file
  2. the PORT variable inside it
  3. VCPKG_BUILD_TYPE release
  4. overlay-triplets in the configuration

basics

~20 s

Copy a built-in triplet into a project directory under a new name, set VCPKG_BUILD_TYPE release and branch on the PORT variable for per-port linkage, then list that directory in overlay-triplets (or pass --overlay-triplets) and select the new triplet name.

solid answer

~50 s

A custom triplet is just another `<name>.cmake` file. Start from a built-in one, say `x64-windows-static-md.cmake`, save it in the repository as `triplets/x64-windows-static-md-rel.cmake`, and add settings: `set(VCPKG_BUILD_TYPE release)` skips the debug configuration, and because vcpkg sets the `PORT` variable while reading the triplet, `if(PORT STREQUAL "openssl")` can switch one port to `dynamic` linkage. Tell vcpkg where the file is through `"overlay-triplets"` in `vcpkg-configuration.json` (paths relative to that file), `--overlay-triplets`, or the `VCPKG_OVERLAY_TRIPLETS` environment variable, then select it as the target triplet. An overlay file named like a built-in triplet replaces it. The file's contents and name are part of every package's ABI hash, so a custom triplet gets its own binary cache entries; files it `include()`s must be listed in `VCPKG_HASH_ADDITIONAL_FILES`. A per-port DLL needs a dynamic CRT, which is why the example starts from static-md.

code

cmake · 9 lines
cmake
# triplets/x64-windows-static-md-rel.cmake (overlay triplet in the repository)
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_BUILD_TYPE release)

if(PORT STREQUAL "openssl")
    set(VCPKG_LIBRARY_LINKAGE dynamic)
endif()

go deeper

for a junior

Recall that a triplet is a .cmake file and that you can copy one, change it, and point vcpkg at its directory.

for a middle

Explain the PORT variable for per-port settings, VCPKG_BUILD_TYPE release, and the three ways to register an overlay triplet directory.

for a senior

Show the traps you have met: DLLs on a static CRT refused by vcpkg_check_linkage, release-only triplets breaking Debug builds, and included files missing from the ABI hash.

for a principal

Decide whether the team owns custom triplets at all: each one is a toolchain contract to review, document and cache separately.

## A triplet is a file you can write vcpkg finds a triplet by name: `--triplet=foo` loads a file named `foo.cmake`. The built-in files live in the registry's `triplets` directory (tested) and `triplets/community` (untested). To add or replace triplets you put files in another directory and register it as an **overlay triplet** location. The vcpkg docs suggest copying a built-in triplet as the starting point, and they recommend a systematic naming scheme; names may use only lowercase letters, digits and hyphens. ## What a custom triplet usually changes | Variable | Typical custom use | |---|---| | `VCPKG_LIBRARY_LINKAGE` | flip static or dynamic, globally or per port | | `VCPKG_CRT_LINKAGE` | choose `/MT` or `/MD` on MSVC | | `VCPKG_BUILD_TYPE` | `release` to skip debug builds (empty builds both) | | `VCPKG_CXX_FLAGS`, `VCPKG_C_FLAGS` | extra compiler flags; setting one requires setting the other | | `VCPKG_CHAINLOAD_TOOLCHAIN_FILE` | use your own CMake toolchain for the ports | | `VCPKG_HASH_ADDITIONAL_FILES` | files whose contents must feed the ABI hash | ## Per-port settings While vcpkg reads the triplet for a port, the CMake variable `PORT` holds that port's name. The docs' example builds all `qt5-*` ports dynamically and the rest statically: ```cmake set(VCPKG_LIBRARY_LINKAGE static) if(PORT MATCHES "qt5-") set(VCPKG_LIBRARY_LINKAGE dynamic) endif() ``` This is also how a single triplet holds mixed settings: the concept docs note that if different libraries need different settings in one build, they must be combined in **one** custom triplet, because a build consumes only one target triplet. Two constraints shape per-port DLLs on Windows: 1. A DLL built against a **static CRT** gets its own runtime copy. vcpkg's `vcpkg_check_linkage` helper refuses to turn a static request into a DLL when the CRT is static, calling the result surprising for most ports. Starting from `x64-windows-static-md` (dynamic CRT) avoids that. 2. Release-only builds (`VCPKG_BUILD_TYPE release`) leave no debug libraries; a Debug configuration of your own MSVC code then links release-runtime libraries, which typically fails with a runtime mismatch. Teams using it either build only Release locally or keep a second, full triplet for debugging. ## Registering the overlay Any of these makes the directory visible: - `"overlay-triplets": [ "./triplets" ]` in `vcpkg-configuration.json` (or the `configuration` object inside `vcpkg.json`); relative paths resolve against that file; - `--overlay-triplets=<dir>` on the command line; - the `VCPKG_OVERLAY_TRIPLETS` environment variable, a path-separated list; - from CMake or MSBuild, the integration's own variable or extra options (covered with the integration). Then select the name as the target triplet: `--triplet=x64-windows-static-md-rel` on the command line, or the integration's triplet setting. ## Effects on caching and reproducibility - The triplet file's **contents and name** are hashed into each package's ABI hash, so editing the triplet invalidates its binary packages, and renaming it does too. - Files the triplet pulls in with `include()` are not hashed unless listed in `VCPKG_HASH_ADDITIONAL_FILES`; forgetting them lets stale binaries be restored after the included file changes. - Keeping custom triplets in the repository, beside `vcpkg-configuration.json`, means every developer and CI agent builds with the same settings. ## Testing a new triplet A custom triplet is a toolchain change, so it deserves the same verification as one: 1. Install the full manifest with the new triplet on a clean machine or a clean build directory, so no earlier packages hide a failure. 2. Read the install plan and the build log: the loaded triplet path is printed, and any port that switched linkage says so. 3. Inspect the installed tree: static ports leave libraries under `lib`, DLL ports also leave files under `bin`, and a release-only triplet leaves no `debug` directory. 4. Build and run the application's tests against the result in every configuration the team uses. 5. Commit the triplet, the `vcpkg-configuration.json` change and the build-system change that selects it together, so no machine runs half of the switch. ## Worked file The example below keeps static libraries on the shared CRT, drops debug builds and makes one port a DLL, for a Windows tool that must load that library dynamically.

  • How do you replace a built-in triplet rather than add a new one?
    Put a file with the same name, such as `x64-windows.cmake`, in an overlay triplet directory. Overlay locations are searched before the built-in `triplets` directory, so your file wins. Prefer a new name unless every consumer really should change, because the same name now means different binaries on different machines.
  • Why might a custom triplet restore stale binaries from the cache?
    The ABI hash covers the triplet file's own text and name, but not files it `include()`s. If shared settings live in an included file, list it in `VCPKG_HASH_ADDITIONAL_FILES`; otherwise changing it leaves the hash, and the cached packages, unchanged.

  • A custom triplet must be added to the vcpkg repository's own triplets directory.
  • You need a separate triplet per library to mix static and dynamic linkage.
  • VCPKG_BUILD_TYPE release still installs debug libraries, just unoptimised.
  • Setting VCPKG_CXX_FLAGS alone is enough; the C flags follow automatically.
  • Editing an included file always invalidates the binary cache for that triplet.