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

cx finds your sources by convention, so cx.toml doesn't list files:

cx.toml
include/<name>/... public headers (makes the package usable as a library)
src/main.cpp the binary named after the package
src/bin/<name>.cpp one more binary per file
src/bin/<name>/main.cpp one binary per directory, built from every source in it
src/**.cpp everything else: library sources
src/**.cppm, src/**.ixx C++20 module interface units of the library (see C++20 modules)
examples/<name>.cpp examples (also examples/<name>/main.cpp)
benches/<name>.cpp benchmarks (also benches/<name>/main.cpp)
tests/*.cpp one test program per file
tests/<name>/main.cpp one test program per directory, built from every source in it
  • Binaries and the library:
    • A src/main.{c,cpp,cc,cxx} directly in src/ is a binary named after the package. Each file in src/bin/, and each directory in src/bin/ with a main.cpp, is another binary.
    • Everything else in src/, searched recursively, plus include/, forms the package's library.
    • When there is a library, the binaries, examples, benchmarks and tests all link it.
  • Header-only libraries: a package with include/ but no library sources is header-only.
  • Examples and benchmarks: each top-level file in examples/ or benches/ is one program, and so is each subdirectory with a main.cpp. Other subdirectories can hold shared helpers. They are only built when you ask (cx build --examples or --example <NAME>, cx run --example <NAME>, cx bench), and they can use [dev-dependencies].
  • Tests: each top-level file in tests/ is its own test program, and so is each subdirectory with a main.cpp (for a test suite spread over several files). Other subdirectories of tests/ can hold shared helpers.
  • Include paths: include/ is public, so dependents see it. src/ is private, so your own sources and tests can include internal headers.
  • Languages: C and C++ files can be mixed in one package.
  • Names: binary, example and benchmark names follow the rules for package names. Two targets of the same kind with the same name are an error.

A build.cmake next to cx.toml and [[generate]] entries add generated code; see Code generation and build scripts.

Targets the conventions can't express

[[bin]], [[example]] and [[bench]] in cx.toml add targets from other places, or set options on the ones cx found:

[[bin]]
name = "mytool"
path = "tools/mytool/main.cpp" # a main.* brings the rest of its directory along
required-features = ["cli"]
[[bin]]
name = "small" # found in src/bin/small.cpp; only adds the requirement
required-features = ["cli"]

A target with required-features is skipped, with a note, unless all of them are enabled (for example with --features cli).

Running binaries

cx run runs the package's only binary. With several, pick one with --bin <name>, or set a default:

[package]
name = "tools"
default-run = "mytool"

Shared libraries

The library is static by default. [lib] kind changes that for the root package:

[lib]
kind = "shared" # "static" (default), "shared", or "both"
  • Exports: symbols of a shared library are hidden unless marked. cx generates <name>/export.hpp (export.h for C) for every compiled library, with an <ID>_EXPORT macro (for my-lib: MY_LIB_EXPORT). Mark the public API with it; in a static build the macro is empty.

    #include <mylib/export.hpp>
    MYLIB_EXPORT int add(int a, int b);
  • Where it goes: target/<profile>/libmylib.so (.dylib on macOS, a .dll and its import library on Windows). Binaries find it next to themselves ($ORIGIN / @loader_path), so target/<profile>/ can be moved. Windows has no RPATH: examples and benches get a copy of the .dll in their directory, and cx test puts target/<profile>/ first in the tests' PATH (tests stay in the build tree).
  • both: builds the static and the shared library. The package's own binaries use the shared one.
  • Dependencies are always built as static libraries.

Build output

Path Contents
target/<profile>/ binaries and the library (target/debug/, target/release/, or a custom profile)
target/<profile>/examples/, target/<profile>/benches/ examples and benchmarks
target/<profile>/build/ the CMake build tree
target/cx/CMakeLists.txt the CMake project cx generates; don't edit it

cx generate writes the generated project and prints its path. You can point an IDE at it. compile_commands.json is in the build tree.