Skip to content
voximir-pPublic

About

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.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

49 Commits

Folders and files

Repository files navigation

cppu

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.


Features

  • Compiles a .c/.cpp file with g++ (or clang++) 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 -i and 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-clean to keep it)

Prerequisites

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.


Installation

You can install cppu in several ways.

Cargo or prebuilt binaries

  1. Cargo install - If you have Rust installed, you can run cargo install cppu

  2. GitHub Releases - Download the latest release from GitHub Releases

Building

git clone https://github.com/voximir-p/cppu
cd cppu
cargo build --release

The 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 .

Usage

cppu [OPTIONS] <source>

Run cppu --help or cppu with no arguments to see the full help screen.

Options

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.


Amalgamation

When you pass -a <path>, cppu will produce a single self-contained source file before compilation:

  1. 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.
  2. This replacement is applied recursively, so nested local includes are inlined too.
  3. System/angle-bracket includes (#include <vector>, etc.) are left untouched.
  4. Circular includes are detected and cause an immediate error.
  5. 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.

Example

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.out

After 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.


Examples

# 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

Exit Codes

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.


How It Works

  1. If -a <path> is given, recursively inlines all local #include "…" directives and writes the amalgamated source to <path>.
  2. Compiles the source (or the amalgamated file, if produced) to a temporary executable next to the original source file using g++ (or clang++). The executable replaces the source extension with .exe on Windows or removes it on Linux/macOS. Compilation time is reported before execution.
  3. Runs the resulting binary from the original source file's directory, wiring stdin/stdout/stderr as configured.
  4. Waits for the process to finish (or for Ctrl+C).
  5. Deletes the temporary binary unless --no-clean was passed.
  6. 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.


Changelog

See CHANGELOG.md for a detailed history of changes and improvements.


License

cppu is licensed under the GNU General Public License v3.0 (LICENSE)

About

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.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages