The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Features

Features are named options of a package that turn on code and optional dependencies, like Cargo's features.

Declaring features

[dependencies]
json = { version = "^3", optional = true }
fmt = { version = "^11" }
[features]
default = ["std"] # enabled unless --no-default-features
std = []
serde = ["dep:json"] # enables the optional dependency json
pretty = ["fmt/color", "std"] # enables fmt's `color` feature and our `std`

A feature lists what it enables:

Item Enables
"std" another feature of this package
"dep:json" the optional dependency json
"json" the same, because an optional dependency is also a feature of its own name
"fmt/color" feature color of dependency fmt, and fmt itself if it is optional
"fmt?/color" feature color of fmt, but only if fmt is enabled anyway; doesn't turn an optional fmt on
"zlib/KEY=VALUE" the CMake option KEY of a plain CMake dependency (see below)
"zlib?/KEY=VALUE" the same, only if the dependency is enabled anyway

Using features in code

Every enabled feature becomes a compile definition named after the package and the feature:

#ifdef CX_FEATURE_MYLIB_SERDE // feature `serde` of package `mylib`
#include <json/json.hpp>
#endif
  • Names: characters other than letters and digits become _, and names are upper-cased. So my-lib's fast+simd is CX_FEATURE_MY_LIB_FAST_SIMD.
  • Visibility: the definitions are public on libraries, so code that includes a library's headers sees the same features as the library.

Choosing features

For your own package:

$ cx build --features serde,pretty # or -F serde -F pretty
$ cx build --no-default-features
$ cx build --all-features

For a dependency:

fmt = { version = "^11", features = ["color"], default-features = false }

or, from the command line:

$ cx add fmt --features color --no-default-features
$ cx add json --optional

cx tree shows which features are enabled for each package:

$ cx tree --features pretty
app v0.1.0 (/home/me/app)
└── fmt v11.1.4 [color, default, std]

Features and the lockfile

cx resolves versions as if every feature of your package were enabled, and locks all of them. --features then only decides what gets built. So switching features never changes cx.lock, and --locked keeps working whichever features you build with.

A feature that no matching version defines is a resolution error:

Because no versions of fmt[nope] match ^11.0.0 and app depends on fmt[nope] ^11.0.0, version solving failed.

Weak dependency features

"dep?/feature" says "if `dep` is enabled, also enable its `feature`":

[dependencies]
fmt = { version = "^11", optional = true }
[features]
color = ["dep:fmt"]
serde = ["fmt?/serde"] # with --features serde,color: fmt with serde; with only serde: no fmt

When versions are solved, the requirement on fmt[serde] only applies if fmt is selected, so it never pulls fmt in by itself.

Features of plain CMake dependencies

A plain CMake project has no [features] table, but it has CMake options. "dep/KEY=VALUE" sets one when the feature is on:

[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog", tag = "v1.15.0", options = ["SPDLOG_BUILD_EXAMPLE=OFF"] }
[features]
fast-logs = ["spdlog/SPDLOG_NO_EXCEPTIONS=ON"]
  • Order: they are set after the dependency's static options, so a feature wins over a static option with the same key.
  • Conflicts: two enabled features that set the same key to different values are an error that names both features.
  • Only for plain CMake packages: using KEY=VALUE on a cx package is an error after resolution, because cx packages have real features. CMake options never affect version solving or cx.lock.