A single-header library for build recipes and general utilities in C.
This is largely based on tsoding's nob.h library — a wonderful alternative for the 'use C to build C' approach.
Grab cba.h directly with:
wget https://raw.githubusercontent.com/jamiegibney/cba.h/refs/heads/master/cba.hOr clone the repository:
git clone git@github.com:jamiegibney/cba.h cbaAdd #define CBA_IMPLEMENTATION before including cba.h in ONE C/C++ file to
generate the implementation:
#include ...
#include ...
#define CBA_IMPLEMENTATION
#include "cba.h"And otherwise just #include "cba.h for its definitions.
// File: cba.c
#define CBA_VERBOSE // See internal logging.
#define CBA_PRINT_ON_REBUILD // Print a message when rebuilding.
#define CBA_IMPLEMENTATION // Generate implementations.
#include "cba.h"
int main(int argc, char** argv) {
// Allow the program to rebuild itself when modified.
CBA_REBUILD(argc, argv);
// Create a directory (also works recursively).
file_try_create_directory("build");
// An array of arguments which can be run like a shell command.
Command cmd = {0};
// Use the CBA_COMPILER_* macros for compiler-specific flags.
cmd_append(&cmd,
CBA_COMPILER_C,
CBA_COMPILER_DEBUG_FLAGS,
CBA_COMPILER_COMMON_FLAGS,
CBA_COMPILER_OUTPUT("build/main"),
CBA_COMPILER_INPUTS("main.c"),
);
// With GCC, the above forms:
// gcc -ggdb -DDEBUG -Wall -Wextra -o build/main main.c
//
// And with MSVC:
// cl.exe /ZI /DDEBUG /W4 /nologo /D_CRT_SECURE_NO_WARNINGS /Fe:build/main main.c
// Run the command, block until it terminates, and assert that it exits normally.
cmd_run(cmd);
return 0;
}Compile the above once with e.g.:
gcc -o cba cba.cAnd when the source file is modified, the program will automatically rebuild itself when run.
Note
Only MSVC versions 19.0 and above (i.e. Visual Studio 2026) are supported due to its compliance with certain C99 features.
cba.h also provides various utilties for C programs, including an arena
allocator, file operations, string operations, and more.
- See the
alloc,alloc_bytes, andalloc_arraymacros for allocating via the arena. - See the
str_*functions for string operations. - See the
str_arr_*functions for string arrays. - See the
file_*functions for file operations. - See the
print,cba_assert, andcba_panicmacros for printing/assertions/panics. - See the
info,warn, anderrormacros for pretty-printing tostdout.
- Hash table/set
- New string functions:
- String split by char/cstr/other (into
StringArray) - Chop right/back until char/other/cstr
- String split by char/cstr/other (into
CBA_COMPILER_OPTIMIZEflag?- Auto-doc tool
- Piping mechanism(?)