Skip to content

jamiegibney/cba.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

175 Commits
 
 
 
 
 
 

Repository files navigation

cba.h

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

Or clone the repository:

git clone git@github.com:jamiegibney/cba.h cba

Usage

Add #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.

Simple example

// 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.c

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

Other utilities

cba.h also provides various utilties for C programs, including an arena allocator, file operations, string operations, and more.

  • See the alloc, alloc_bytes, and alloc_array macros 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, and cba_panic macros for printing/assertions/panics.
  • See the info, warn, and error macros for pretty-printing to stdout.

Todo

  • Hash table/set
  • New string functions:
    • String split by char/cstr/other (into StringArray)
    • Chop right/back until char/other/cstr
  • CBA_COMPILER_OPTIMIZE flag?
  • Auto-doc tool
  • Piping mechanism(?)

About

Single-header C utility library

Resources

License

Stars

Watchers

Forks

Contributors

Languages