skip to content

In a vcpkg.json, how do a dependency's features and default-features settings control which optional parts of a library get built?

level: middleimportance: should knowfreq 35%

answer

  1. features are additive flags
  2. default-features defaults to true
  3. any requester can re-enable defaults
  4. the project can have features too

basics

~20 s

A port's features are optional parts that add build options and dependencies. A dependency entry lists extra features to build and can set default-features to false; features are additive across the graph, so defaults come back if another port asks for them.

solid answer

~50 s

A **feature** is an optional part of a port, such as `ssl` in `boost-asio`, that adds dependencies and build options. In a dependency object, `features` lists the extra ones you need, each a name or `{ "name", "platform" }`, and `default-features` (default `true`) controls whether the port's on-by-default set, declared in its own `default-features`, comes along. Setting `"default-features": false` on spdlog, whose defaults are `fmt` and `tz-offset`, builds it without the external fmt. Features are **additive**: one build per port per triplet carries the union of everything requested, so the defaults return if another port in the graph depends on spdlog without opting out. To drop them, list the port directly with `default-features: false` and make sure no other dependent asks for them. A project's own `features` map works the same way, enabled with `vcpkg install --x-feature=<name>` or the CMake integration.

code

json · 13 lines
json
{
  "dependencies": [
    { "name": "boost-asio", "features": [ "ssl" ] },
    { "name": "spdlog", "default-features": false }
  ],
  "features": {
    "tests": {
      "description": "Unit tests",
      "dependencies": [ "gtest" ]
    }
  },
  "builtin-baseline": "9eec002df1cd109f8f104cd966a763daf9751489"
}

go deeper

for a junior

Recall that features are optional parts of a port, that you list them in a dependency's features array, and that default-features: false drops the defaults.

for a middle

Explain additivity: one build per port per triplet, the union of every request, and the two conditions needed to keep a port's defaults off.

for a senior

Show how you trace an unwanted feature with vcpkg depend-info, promote a transitive dependency to drop its defaults, and fix the requesting port when the defaults keep returning.

for a principal

Decide how lean the team's dependency set should be: every default kept costs build time, binary size and attack surface, against the cost of maintaining explicit feature lists.

## What a feature is A port in vcpkg can declare **features**: named, optional pieces of functionality. Each feature has a `description` (required), may add `dependencies`, and may restrict itself with `supports`. The portfile turns selected features into build options, typically through `vcpkg_check_features`. Real examples from the 2026.10.16 registry: - `boost-asio` has features `deadline-timer`, `spawn` and `ssl`; `ssl` pulls in `openssl` on non-Emscripten targets; - `spdlog` has features `fmt`, `tz-offset`, `benchmark` and `wchar` (the last one `supports` only Windows); - `openssl` has features such as `tools` and `fips`. A port also declares `default-features`, the set that is on unless a consumer opts out. For spdlog that is `fmt` (use the external fmt package rather than the bundled copy) and `tz-offset`. ## Asking for features in a dependency A dependency entry in `vcpkg.json` can be a plain name or an object: ```json { "name": "boost-asio", "features": [ "ssl" ] }, { "name": "spdlog", "default-features": false }, { "name": "openssl", "features": [ { "name": "tools", "platform": "windows" } ] } ``` - `features` lists additional features; an entry may carry a `platform` expression so it applies only on matching triplets. - `default-features` is a boolean that **defaults to `true`**. Set it to `false` to build without the port's defaults. - In classic mode the same opt-out is spelled with the special `core` feature, as in `vcpkg install spdlog[core]`. ## Why the defaults sometimes come back vcpkg builds **one** copy of each port per triplet, so the features selected are the union of every request in the graph. The vcpkg-tool resolver turns defaults on for a port when either: 1. any port in the graph depends on it without `"default-features": false` (the default is `true`), or 2. the top-level manifest does not list it with `"default-features": false`. So two conditions are needed to keep defaults off: the top-level manifest lists the port directly with `"default-features": false`, **and** no other dependent asks for the defaults. This is why the vcpkg docs tell port authors that libraries consumed by others should almost always depend with `"default-features": false`, and why a transitive dependency's defaults cannot be removed without promoting it to a direct dependency. `vcpkg depend-info` shows which port pulled a feature in. | Request | spdlog built with | |---|---| | `"spdlog"` | `fmt`, `tz-offset` | | `{ "name": "spdlog", "features": ["wchar"] }` on Windows | `fmt`, `tz-offset`, `wchar` | | `{ "name": "spdlog", "default-features": false }` alone | core only | | the line above plus another port depending on plain `spdlog` | `fmt`, `tz-offset` again | ## Features of your own project A top-level manifest can declare its own `features` map, for example a `tests` feature that adds `gtest` and a `tools` feature that adds a command-line parser. They are switched on with `vcpkg install --x-feature=tests`; `--x-no-default-features` skips the project's own defaults. The CMake integration forwards a list variable to those switches during configure; that wiring belongs to the integration material. ## Consequences in practice - Adding a feature changes the package's ABI hash, so it is a different binary package and a cache miss the first time. - A feature can fail to build where its `supports` expression excludes the triplet; read the port's manifest before requesting it. - Features never subtract: there is no way for one dependent to forbid a feature another dependent requests. ## Choosing features for a pinned desktop application A desktop application that wants a small, predictable dependency set usually settles on a few habits: 1. Start each dependency with `"default-features": false` and add back only the features the code uses, so the manifest documents every optional component that ships. 2. Use platform-qualified feature entries (`{ "name": "tools", "platform": "windows" }`) instead of separate manifests per platform. 3. Run `vcpkg install --dry-run` after a change and read the selected features in the plan; a feature that appears unexpectedly names a dependent to investigate. 4. Keep test-only and tool-only libraries behind the project's own features, so a release build does not compile them. The cost of this discipline is maintenance: when a library gains a new default that the application actually needs, someone has to add it explicitly.

  • Why might a port still be built with its default features after you set default-features to false?
    Defaults are re-enabled when any other port in the graph depends on it without opting out, because vcpkg builds one copy per triplet with the union of requests. Run `vcpkg depend-info` to find the dependent; the fix is upstream, in that port's manifest, since you cannot subtract a feature another port requests.
  • How do you enable only a feature of your own project from the vcpkg command line?
    Run `vcpkg install --x-feature=<name>` in manifest mode, repeated for several features. Add `--x-no-default-features` to skip the project's own default features so only the listed ones install.

  • Setting default-features to false on your dependency removes them for every port in the graph.
  • default-features is false unless you set it, so defaults must be requested explicitly.
  • vcpkg builds a separate copy of a port for each set of features requested.
  • A dependent can forbid a feature that another dependent requests.
  • Features are only for ports; a project's own vcpkg.json cannot declare any.