- C 99%
- Shell <1%
- Nix <1%
c-template #
Two ready-to-use C project templates for executables and libraries.
Creating a project #
./new binary my-app # executable project in ./my-app/
./new library my-lib # library project in ./my-lib/
./new binary my-app ~/src/my-app # optional explicit destination
./new # interactive prompts via gum
This copies the template, sets the project name in the justfile, initialises a git repo, and (for libraries) renames the placeholder source files to match the name. A .forgejo/workflows/ci.yml that runs just test on every push is included automatically.
Common recipes #
Run these from inside the project directory.
| Recipe | Description |
|---|---|
just build |
Debug build (ASan + UBSan) |
just release |
Optimised release build |
just run |
Build (debug) and run |
just test |
Compile and run all tests/test_*.c |
just valgrind |
Run under Valgrind (Linux) |
just cppcheck |
Static analysis |
just fmt |
Format all sources in-place |
just fmt-check |
Check formatting (CI-friendly) |
just compile-commands |
Generate compile_commands.json for clangd |
just docs |
Generate HTML documentation in .build/docs/html/ |
just clean |
Remove .build/ |
Binary only:
| Recipe | Description |
|---|---|
just build-for <triple> |
Cross-compile (e.g. aarch64-linux-gnu) |
just run-release |
Build (release) and run |
Library only:
| Recipe | Description |
|---|---|
just static |
Build .build/libname.a |
just dynamic |
Build .build/libname.so (or .dylib) |
just pack [version] [dirname] |
Pack for distribution |
Customising a template #
The top of each justfile has all the variables you should need to touch:
name := "app" # output binary / library name
std := "c11" # -std flag passed to gcc
cc := "gcc" # compiler — swap for clang or a cross-compiler
To link additional libraries, append to lnk:
lnk := "-Llib/static -lfoo -Llib/dynamic -lbar -lm"
Drop the corresponding .a / .so files into lib/static/ or lib/dynamic/.
Packaging a library #
cd my-lib
just pack # produces my-lib.tar.gz
just pack 1.2.3 # produces my-lib-1.2.3.tar.gz
just pack 1.2.3 my-lib-1.2.3 # same, extracts into my-lib-1.2.3/ subdirectory
To consume the package in a binary project:
cd my-app
tar -xzf ../my-lib/my-lib-1.2.3.tar.gz # drops into include/, lib/static/, lib/dynamic/
Then add -lmy-lib to the lnk variable in the binary's justfile.
Dev environment #
Managed via Nix flakes + direnv. Running direnv allow provides gcc, just, bear, gum, cppcheck, doxygen, clang-format, and valgrind (Linux).