cppu (CPP Utils) is a C/C++ utility tool for competitive programming. It can compile your code, read input from a
file, write output to another file, and amalgamate local #includes into a single self-contained source.
- Compiles a
.c/.cppfile withg++(orclang++) and runs it in one command - Amalgamation — recursively inlines
#include "…"directives into a single file, perfect for submitting to online judges - Pipes stdin from a file with
-iand captures stdout+stderr to a file with-o - Cargo-style, color-coded status output on stderr, including compilation time
- Output truncation guard (
-m, with-o) to prevent runaway programs from flooding files - Ctrl+C cleanly kills the child process
- Auto-deletes the compiled binary after execution (use
--no-cleanto keep it)
| Requirement | Notes |
|---|---|
| g++ | MinGW-w64 on Windows; g++ must be on PATH |
| clang++ | Optional; only needed if you use --use-clang |
| Rust + Cargo | Required for Cargo installation or building from source; install from rustup.rs |
On Windows, the easiest way to get g++ is via MSYS2 (pacman -S mingw-w64-ucrt-x86_64-gcc)
and adding its bin/ directory to your PATH.
You can install cppu in several ways.
-
Cargo install - If you have Rust installed, you can run
cargo install cppu -
GitHub Releases - Download the latest release from GitHub Releases
git clone https://github.com/voximir-p/cppu
cd cppu
cargo build --releaseThe binary will be in target/release/ (cppu on Linux/macOS or cppu.exe on Windows). Copy it anywhere on your
PATH.
# Quick install to ~/.cargo/bin (already on PATH if you used rustup)
cargo install --path .cppu [OPTIONS] <source>Run cppu --help or cppu with no arguments to see the full help screen.
| Flag | Default | Description |
|---|---|---|
<source> |
(required) | Path to the source file |
-i, --input <path> |
stdin | Feed a file as the program's stdin |
-o, --output <path> |
stdout | Capture program output (stdout + stderr) to a file |
-a, --amal <path> |
off | Output path to the amalgamated source file; when set, cppu compiles that generated file instead (see Amalgamation) |
-m, --max-output-chars <N> |
50000 |
Maximum captured bytes with -o; exceeding the limit truncates output and stops the program |
-q, --quiet |
off | Suppress status messages; warnings, errors, and compiler diagnostics remain visible |
--no-clean |
off | Keep the compiled executable after the run |
--use-clang |
off | Use clang++ instead of g++ as the compiler |
--cflags <flags> |
"-O2" |
Extra flags passed through to g++ or clang++ |
Despite its name, --max-output-chars counts bytes and applies only when -o is set. Without -o, program stdout and
stderr stay on their respective streams, and output is not limited.
Use --cflags="…" for flags beginning with -. The supplied flags replace the default -O2; include it explicitly if
you want to keep that optimization level.
When you pass -a <path>, cppu will produce a single self-contained source file before compilation:
- Every
#include "header.h"directive (quoted, local includes) is replaced with the full contents of the referenced file, resolved relative to the directory of the file containing that directive. - This replacement is applied recursively, so nested local includes are inlined too.
- System/angle-bracket includes (
#include <vector>, etc.) are left untouched. - Circular includes are detected and cause an immediate error.
- The amalgamated result is written to
<path>, and that file is what gets compiled and executed instead of the original source.
Amalgamation expands includes as text; it does not evaluate preprocessor conditions or include guards. Shared headers are expanded each time they are included; only cycles in the active include chain are rejected.
This is especially useful for competitive programming, where you maintain a personal header library but need to submit a
single .c/.cpp file to an online judge.
Given the following project layout:
solution.cpp ← #include "dsu.h"
dsu.h ← your disjoint-set-union header
# Produce an amalgamated file and run it
cppu solution.cpp -a merged.cpp -i test.in -o test.outAfter this command, merged.cpp will contain the full source with the contents of dsu.h inlined in place of
#include "dsu.h". You can submit merged.cpp directly.
# Compile and run hello.cpp (output printed to terminal)
cppu hello.cpp
# Provide a test input file; capture output
cppu solution.cpp -i tests/01.in -o tests/01.out
# Amalgamate local includes, compile, and run
cppu solution.cpp -a merged.cpp
# Use C++20 with extra warnings
cppu main.cpp --cflags="-std=c++20 -Wall -Wextra"
# Use clang++ and keep the compiled binary
cppu main.cpp --use-clang --no-clean
# Pipe input through stdin (no -i flag needed)
echo "42" | cppu main.cpp| Code | Meaning |
|---|---|
0 |
Program exited successfully |
1 |
Output exceeded the -m limit with -o, compilation failed, or another runner error occurred |
2 |
Invalid command-line arguments |
130 |
Interrupted by Ctrl+C |
| (program's own code) | Whatever exit code the child process returned |
If the child process terminates without an exit code (for example, due to a signal), cppu returns 1.
- If
-a <path>is given, recursively inlines all local#include "…"directives and writes the amalgamated source to<path>. - Compiles the source (or the amalgamated file, if produced) to a temporary executable next to the original source file
using
g++(orclang++). The executable replaces the source extension with.exeon Windows or removes it on Linux/macOS. Compilation time is reported before execution. - Runs the resulting binary from the original source file's directory, wiring stdin/stdout/stderr as configured.
- Waits for the process to finish (or for Ctrl+C).
- Deletes the temporary binary unless
--no-cleanwas passed. - Exits with the same code the child process returned, unless interrupted or a runner error occurs (see Exit Codes).
All cppu diagnostic messages (compile errors, timing, status) are printed to stderr so they never pollute the output
file specified by -o. Runner status and diagnostic colors are disabled when stderr is redirected, NO_COLOR is
nonempty, or TERM=dumb.
See CHANGELOG.md for a detailed history of changes and improvements.
cppu is licensed under the GNU General Public License v3.0 (LICENSE)