Basic C project template.
README.md

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 nob.c, initialises a git repo, and (for libraries) renames the placeholder source files to match the name. A .forgejo/workflows/ci.yml that runs ./nob test on every push is included automatically.

Bootstrap #

Each project needs a one-time bootstrap step:

gcc nob.c -o nob

After that, ./nob rebuilds itself automatically whenever nob.c changes.

Commands #

Run these from inside the project directory.

Command Description
./nob Debug build (default)
./nob build Debug build with ASan + UBSan
./nob release Optimised release build
./nob run Build (debug) and run
./nob test Compile and run all tests/test_*.c
./nob valgrind Run under Valgrind (Linux)
./nob strace Trace system calls
./nob gdb Launch in gdb (binary only)
./nob cppcheck Static analysis
./nob ctags Generate tags file for editor navigation
./nob fmt Format all sources in-place
./nob fmt-check Check formatting (CI-friendly)
./nob compile-commands Generate compile_commands.json for clangd
./nob docs Generate HTML documentation in .build/docs/html/
./nob clean Remove .build/

Binary only:

Command Description
./nob build-for <triple> Cross-compile (e.g. aarch64-linux-gnu)
./nob run-release Build (release) and run
./nob install [prefix] Install release binary to prefix/bin (default /usr/local)
./nob uninstall [prefix] Remove installed binary

Library only:

Command Description
./nob static Build .build/libname.a
./nob dynamic Build .build/libname.so
./nob pack [version] [dirname] Pack for distribution

Customising a template #

The top of nob.c has the settings you need to touch:

#define NAME      "app"   /* output binary / library name */
#define STD       "c11"   /* -std flag passed to gcc */
#define CC        "gcc"   /* compiler — swap for clang or a cross-compiler */

To link additional libraries, append to LNK_FLAGS:

#define LNK_FLAGS  "-L" LIB_DIR "/static", "-L" LIB_DIR "/dynamic", "-lfoo", "-lm"

Drop the corresponding .a / .so files into lib/static/ or lib/dynamic/.

To expose POSIX or GNU extensions, add to BASE_FLAGS:

#define BASE_FLAGS  "-std=" STD, WARN_FLAGS, "-I" INC_DIR, "-D_POSIX_C_SOURCE=200809L"

Packaging a library #

cd my-lib
./nob pack                      # produces my-lib.tar.gz
./nob pack 1.2.3                # produces my-lib-1.2.3.tar.gz
./nob 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 LNK_FLAGS in the binary's nob.c.

Dev environment #

Managed via Nix flakes + direnv. Running direnv allow provides gcc, bear, gdb, gum, universal-ctags, cppcheck, doxygen, clang-tools, and valgrind (Linux).