The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Toolchains and cross-compilation

By default cx builds with whatever compiler CMake finds. [toolchain] picks the compilers, and [target.<triple>] describes another platform to build for.

Choosing compilers

# cx.toml, or .cx/config.toml / ~/.config/cx/config.toml
[toolchain]
cc = "clang-19"
cxx = "clang++-19"
min-version = "18" # fail early, with a clear message, if the compiler is older
  • Precedence: the CC and CXX environment variables win, then the configuration files, then cx.toml. So a project can pin compilers for the team, and anyone can override them for one build.
  • Lookup: compilers are looked up in PATH. One that isn't there is an error that names the setting it came from.
  • min-version: checked when CMake configures, against the C++ compiler (or the C compiler in a C-only project).
  • Changing compilers: when the compiler or toolchain file changes, cx starts the build directory's CMake cache over, so the switch takes effect right away.

Cross-compiling

[target.aarch64-linux-gnu]
cmake-toolchain-file = "cmake/aarch64.cmake" # relative to cx.toml (or to the config file's project)
sysroot = "/opt/sysroots/aarch64"
runner = "qemu-aarch64 -L /opt/sysroots/aarch64" # a string or a list
cxx = "aarch64-linux-gnu-g++" # optional: overrides [toolchain] for this target
$ cx build --target aarch64-linux-gnu # output in target/aarch64-linux-gnu/debug/
$ cx run --target aarch64-linux-gnu # runs the program through the runner
$ cx test --target aarch64-linux-gnu # ctest runs the tests through the runner
Key Becomes
cmake-toolchain-file CMAKE_TOOLCHAIN_FILE
sysroot CMAKE_SYSROOT
runner the prefix of cx run and cx bench, and CMAKE_CROSSCOMPILING_EMULATOR for cx test
cc, cxx CMAKE_C_COMPILER, CMAKE_CXX_COMPILER
  • Output: with --target, everything goes to target/<triple>/<profile>/ instead of target/<profile>/, so native and cross builds don't overwrite each other. cx clean --target <triple> removes only that target's output.
  • Unknown targets: --target x without a [target.x] section is an error that lists the known targets.
  • Where to put it: [target.*] works in cx.toml and in the configuration files; fields in the configuration files win.
  • cx.lock doesn't depend on the target. Dependencies that only exist for some platforms aren't supported yet.

cx doesn't download toolchains; it uses compilers that are installed.