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

Create a package

$ cx new hello
Created binary (application) `hello` package

This creates:

hello/
├── .gitignore ignores /target
├── cx.toml the manifest
└── src/
└── main.cpp

cx new also runs git init, unless you pass --vcs none or the directory is already inside a git repository. Like uv init, it writes your git author (user.name and user.email) into authors; --author-from none leaves it out. Other variants:

Command Creates
cx new --lib mylib a library, with include/mylib/mylib.hpp, src/mylib.cpp and a test
cx new --c hello a C program
cx init a package in the current directory, without overwriting existing files
cx init --from CMakeLists.txt a cx.toml drafted from the project's CMake build (and --from vcpkg.json); see Importing an existing project

Build and run

$ cx build
Compiling hello v0.1.0 (/home/me/hello)
Finished `debug` profile [unoptimized + debuginfo] in 0.94s
$ cx run -- --some-flag
Running `target/debug/hello --some-flag`
Hello, world!
  • Build output: target/debug/ (or target/release/ with --release).
  • Extra arguments: cx run passes arguments after -- to your program.
  • Where cx's messages go: to stderr, so cx run stdout is only your program's output.

Add a dependency

$ cx add fmt # from the registry
$ cx add fmt@^11 # with a version requirement
$ cx add fmt --git https://github.com/fmtlib/fmt --tag 11.1.4
$ cx add mylib --path ../mylib
$ cx add ZLIB --system # an installed package (find_package)

cx add edits cx.toml in place, keeping your comments and formatting. It then resolves the dependencies right away. If that fails, it puts cx.toml back the way it was.

Then use the dependency:

#include <fmt/core.h>
int main() { fmt::print("Hello from fmt\n"); }

Test

Every file directly in tests/ becomes a test program. A test passes when it exits with status 0:

$ cx test
Testing hello v0.1.0
100% tests passed, 0 tests failed out of 1

Arguments after -- go to ctest, e.g. cx test -- -R parser.

Inspect

$ cx tree
hello v0.1.0 (/home/me/hello)
└── fmt v11.1.4 [cmake]

Examples

The repository's examples/ directory has small, complete projects, one feature each: a library with tests and benchmarks, a C package, a workspace, features, plain CMake and system dependencies, code generation, profiles, a shared library, platform-specific dependencies, C++20 modules, and dependencies from git, vcpkg and Autotools. Each cx.toml starts with the commands to try.