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

Every dependency has exactly one source:

[dependencies]
spdlog = "^1.14" # registry
fmt = { git = "https://github.com/fmtlib/fmt", tag = "11.1.4" } # git
mylib = { path = "../mylib" } # local path
zlib = { system = "ZLIB", version = "1.2" } # installed (find_package)
curl = { vcpkg = true, package = "CURL" } # a vcpkg port
[dev-dependencies] # only for tests
doctest = "^2.4"

Registry

A plain string, or a table with version, is a registry dependency. Requirements are written the way Cargo writes them:

Requirement Means
"1.2.3", "^1.2.3" >=1.2.3, <2.0.0
"^0.2.3" >=0.2.3, <0.3.0
"~1.2.3" >=1.2.3, <1.3.0
"=1.2.3" exactly 1.2.3
">=1.2, <1.5" both comparisons
"1.*", "*" any 1.x, anything

{ version = "^1", registry = "internal" } uses a registry other than the default one; see Registries. Pre-release versions such as 2.0.0-beta.1 are only used when your requirement names one.

Git

fmt = { git = "https://github.com/fmtlib/fmt", tag = "11.1.4" } # or branch = "...", rev = "..."
  • Which commit: cx resolves the tag, branch or rev to a commit and records it in cx.lock. A branch dependency stays on that commit until you run cx update.
  • Where clones live: in cx's cache, ~/.cache/cx by default.
  • What is fetched: only the commit that the tag, branch or default branch points at (--depth 1), so even a large repository takes seconds. A locked commit the branch has moved past is fetched by itself. A short rev (an abbreviated hash) can't be, so cx fetches the whole repository for it: write the full hash to avoid that.

Path

mylib = { path = "../mylib" }

The path is relative to cx.toml. Changes to the sources are picked up on the next build.

System

zlib = { system = "ZLIB" } # find_package(ZLIB REQUIRED)
openssl = { system = "OpenSSL", version = "3.0", components = ["SSL"] }
threads = { system = true, package = "Threads" }

System packages are found by CMake at configure time. They don't take part in version resolution.

vcpkg

fmt = { vcpkg = true } # the port `fmt`, find_package(fmt)
zlib = { vcpkg = "zlib", package = "ZLIB" } # find_package(ZLIB)
curl = { vcpkg = true, package = "CURL", features = ["http2"], default-features = false }

A vcpkg dependency is a port that vcpkg builds and installs; cx then finds it with find_package(), like a system package (package, version and components mean the same). features and default-features are the port's features. cx add fmt --vcpkg writes the first line.

  • Installing: before configuring, cx writes a vcpkg.json with every vcpkg dependency of the build (target/cx/vcpkg/<triplet>/) and runs vcpkg install in manifest mode into target/vcpkg/<triplet>/. It runs vcpkg again only when the ports, their features or the triplet change, so later builds (and --offline ones) don't touch the network.
  • Finding vcpkg: $VCPKG_ROOT/vcpkg, then vcpkg in PATH, or tools.vcpkg (see External tools). cx doesn't download vcpkg.
  • Triplets: the platform you build for decides it: x64-linux, arm64-osx, x64-windows-static-md, x64-mingw-static, ... (with --target, the target's). VCPKG_DEFAULT_TRIPLET overrides it, and VCPKG_DEFAULT_HOST_TRIPLET for the host build of build-dependencies. Another triplet is another install, and starts a fresh CMake cache.
  • Versions are vcpkg's: the ports of your vcpkg checkout. vcpkg packages don't take part in version resolution and are never in cx.lock. A library published to a registry can declare vcpkg dependencies; they are installed for whoever builds it.

cx packages and plain CMake projects

A git, path or registry dependency is either:

  • a cx package (it has a cx.toml): cx builds it and resolves its dependencies too
  • any CMake project: cx adds it with add_subdirectory

Linking

cx links the dependency's CMake target. Without targets, it tries <name>::<name>, then <name>, then old-style <Name>_LIBRARIES variables. When the guess is wrong, say which targets to link:

fmt = { git = "https://github.com/fmtlib/fmt", tag = "11.1.4", targets = ["fmt::fmt"] }

options sets CMake cache variables before the project is added:

spdlog = { git = "https://github.com/gabime/spdlog", tag = "v1.15.0", options = ["SPDLOG_FMT_EXTERNAL=ON"] }

Public and private dependencies

A library's dependencies are private by default: the library is compiled and linked with them, but packages that use the library don't get their headers. A dependency that is part of the library's interface, because its public headers include the dependency's headers or use its types, is public:

[dependencies]
fmt = "^11" # used only in src/: private
expected-lite = { version = "^0.8", public = true } # its types appear in include/

Private dependencies keep downstream include paths short and catch code that includes a header it never declared a dependency on. A static library's private dependencies are still linked into programs that use it.

When public isn't set, cx looks at the package's own include/ headers. If one of them includes a header of the dependency, the dependency is treated as public, and cx warns the package's author to say so:

warning: `include/outer/outer.hpp` includes `<inner/inner.hpp>` from `inner`, so `inner` is linked as a public dependency; ...

Set public = false to keep such a dependency private anyway. The package's own binaries, tests, examples and benches always get every dependency. Header-only (INTERFACE) libraries pass all of their dependencies on. cx add --public writes public = true.

Versions of plain CMake projects

A plain CMake project has no version cx can read, so it counts as 0.0.0. If registry packages require a version of it, declare the version the source provides:

fmt = { git = "https://github.com/me/fmt-fork", branch = "fix", version = "11.1.4" }

Projects built with configure or SCons

A git or path dependency with neither a cx.toml nor a CMakeLists.txt is built with its own build system:

  • configure: a configure script, or a configure.ac (cx runs autoreconf --install first, which needs Autoconf, Automake and Libtool). cx runs configure --prefix=... --disable-shared --enable-static --with-pic, then make and make install into the build directory.
  • SCons: an SConstruct. cx runs scons in the project's directory.
zlib = { git = "https://github.com/madler/zlib", tag = "v1.3.1", build-system = "configure", libs = ["z"] }
shout = { path = "../shout", libs = ["build/shout"], include-dirs = ["include"] } # SConstruct
  • Which build system: build-system = "cmake", "configure" or "scons" picks one when a project has several (zlib has both a CMakeLists.txt and a configure). Without it, cx takes the first of CMakeLists.txt, configure (or configure.ac) and SConstruct.
  • Libraries: libs lists the static libraries the project builds, as [<dir>/]<name>: z is libz.a (z.lib with MSVC), and a name with .a or .lib is taken as it is. With configure, they are relative to the install prefix's lib/; with SCons, to the project's directory. The default is the dependency's name without a lib prefix (libpng is png).
  • Headers: include-dirs, relative to the install prefix (configure, default include) or the project's directory (SCons, default include, or the top directory when there is no include/).
  • Options: options are passed on as they are: to configure (["--without-zlib", "CFLAGS=-O3"]) or to scons (["MODE=release"]). Features of your package can add more with "dep/KEY=VALUE".
  • Toolchain: the build gets the compilers CMake found (CC, CXX, AR, RANLIB in its environment). With --target, configure also gets --host=<triple>. An SConstruct has to read CC from the environment itself (Environment(CC=os.environ["CC"])).
  • Linking: cx wraps the libraries in the target <name>::<name> and links it like any other dependency, public or private.
  • When it builds: at build time, in a copy of the sources under target/<profile>/build/cx_foreign/. A path dependency's changed sources are copied and built again on every build; after changing its configure script, run cx clean. Their output goes to log files, which are printed when the build fails.
  • Tools: cx finds make and autoreconf in PATH (tools.make, tools.autoreconf), and never downloads them. For SCons, it uses scons from PATH; when there is none, it downloads SCons, and Python too if there is no Python 3.7 or newer in PATH (tools.scons, tools.python; see External tools).

Like plain CMake projects, they count as version 0.0.0 unless they declare version, and cx install --lib can't bundle them.

Platform-specific dependencies

Dependencies under [target.<platform>.dependencies] are only used when building for a platform that <platform> applies to:

[target.'cfg(windows)'.dependencies]
wil = "^1.0"
[target.'cfg(unix)'.dependencies]
threads = { system = "Threads", targets = ["Threads::Threads"] }
[target.'cfg(all(target_os = "linux", not(target_env = "musl")))'.dev-dependencies]
libunwind = { system = true }
[target.aarch64-linux-gnu.dependencies] # an exact target triple
neon-helpers = "^2"

The platform is the --target triple, or the machine cx runs on. Conditions use these keys, combined with all(...), any(...) and not(...):

Condition Values
unix, windows the platform family
target_os "linux", "macos", "windows", "android", "ios", "freebsd", ...
target_arch "x86_64", "aarch64", "x86", "arm", "riscv64", ...
target_env "gnu", "musl", "msvc", or ""
target_vendor "pc", "apple", "unknown", ...
target_family "unix", "windows"

Version resolution considers every platform's dependencies, so cx.lock is the same on every machine. The dependencies of other platforms are resolved (and git or path sources looked at) but never built. cx vendor copies them all. cx tree --target <TRIPLE> shows what a platform uses, and cx add --platform 'cfg(windows)' adds to a platform's section.

A dependency is declared in one section only: it can't be both in [dependencies] and in a [target.*] section, or in two [target.*] sections. Conditions about the compiler (as opposed to the platform) aren't supported; use build.cmake for those.

Language standards

Registry entries record the language standard of each version (standard, taken from the package's cx.toml when it is published). When a newer version of a dependency needs a newer standard than your package uses, resolution prefers an older version that still fits:

[package]
standard = 17
[resolver]
incompatible-standards = "fallback" # the default
# "deny": never pick such a version; the failure report says why
# "allow": ignore standards
max-standard = 20 # accept dependencies up to C++20 (default: package.standard)
  • fallback picks the newest allowed version that fits. If none does, it picks as usual and warns: `fmt` 12.0.0 needs C++23, but this package uses C++17 . A locked version is kept either way.
  • deny leaves such versions out (unless cx.lock pins one).
  • Only the same language counts: a C library's C standard never limits a C++ package. Index entries without standard (older ones, plain CMake packages) always fit. Git and path dependencies are never left out; cx only warns about them.
  • Workspaces compare against the newest standard any member uses, and take [resolver] from the workspace's root cx.toml.

cx.lock doesn't depend on your compiler, only on the manifests.

Overriding a registry package

A git or path dependency in your own cx.toml replaces that package everywhere in the dependency graph. That's the way to try a local fork:

fmt = { path = "../fmt" } # every package that needs fmt now uses ../fmt

The override's version must still satisfy what other packages require, otherwise resolution fails with an explanation. Apart from root overrides, every declaration of a name must use the same source.

When resolution fails

cx explains the chain of requirements that makes a solution impossible:

error: failed to resolve dependencies
Because every version of newapi depends on mathx ^2.0.0 and app depends on mathx ^1.0.0, every version of newapi is incompatible with app.
So, because app depends on newapi, version solving failed.