|
libhatchet
|
Please use the most recent tagged release. The include and src directory now report 100% line and branch coverage although a few things were exempted.
People say that you should not micro-optimize. But if what you love is micro-optimization... that's what you should do. - Linus Torvalds
libhatchet is a fast-compiling, lightweight, bespoke C17/C++23 alternative to the C++ standard library designed for cross-compilation to resource-constrained targets like DSPs, FPGAs, ASICs or WebAssembly. It also falls back to requiring only C99 libraries and a C++11 compiler, and deliberately avoids dependencies on the C++ standard library. For those with a low-level mindset, the developer experience is better than with the C++ standard library. For example, the template instantiation errors are easier to read, and hxassertmsg will format your assert messages before setting a breakpoint for you. There is nothing unnecessary to step through in the debugger.

Compile times are shockingly fast when using ninja+ccache without needing precompiled headers. A C++20 module is also available in the src directory and is recommended although modules load a little slower with GCC.
The implementation maintains compatibility with every sensible warning flag and with sanitizers for both GCC and Clang. Of course, asserts are also widely used. The implementation also avoids dynamic allocations except when initializing system allocators.
Containers: Provides a set of containers designed for environments where reallocation is not allowed. This codebase is intended for low-level work where the standard container libraries cause code bloat, memory fragmentation, and poor cache coherence. If those are not your concerns, consider using additional libraries. No tree data structures will be provided. For efficiency reasons exceptions are not supported and therefore noexcept is widely applied to defend against data loss due to the unexpected use of exceptions.
There are two array classes that both support two allocation modes:
| compile time capacity > 0 | hxallocator_dynamic_capacity | |
|---|---|---|
| hxarray | Compile-time fixed size, inline storage | Variable initial size, non-resizable, non-reallocating heap storage [1] |
| hxvector | Resizable, fixed capacity, inline storage | Resizable, variable initial capacity, non-reallocating heap storage |
[1] Not provided by the standard.
See the headers at include/hx/ for the remaining containers. They generally use the same names as the standard.
The documentation is at https://whatchamacallem.github.io/libhatchet.
Running the command doxygen with no arguments will generate docs/index.html. The markdown source for the documentation is in the header files at include/hx/ and is also readable as-is. The stylesheet was tested with Doxygen 1.15.0.
Reading <hx/hxsettings.h> is recommended. There are a number of important things that can be configured on the compiler command line. E.g. whether or not the library is wrapped in a namespace.
This project was started for the author's own personal use, and it tries to be complete enough for ordinary C++ programmers. It was started before a lot of similar functionality was added to the standard. However, if you find something missing, odds are your favorite AI already knows how to add it, or it was omitted because the C library was deemed sufficient.
That said, some functionality of the C++ standard library is not worth reimplementing here. If you need these things you are advised to use the standard library shipped with your compiler.
Almost every reasonable GCC and Clang warning flag should be safe to enable. Clang-Tidy is also in use. The environment tested with every release is glibc and musl on Ubuntu 26.04 LTS using g++-16 and clang-22. g++-10 and clang-11 are periodically tested with C++11. The latest MSVC 2022 should be working with most warnings enabled as well. Although the MSVC static analyzer is not being tested. See debian_packages.sh to install the required packages for the test scripts on a Debian based distribution (e.g. Ubuntu).
The pico directory contains a port to the Raspberry Pi Pico 2. Note how there is almost nothing to do when porting as libc provides almost all of the device abstraction required. Some devices may require you to explicitly call the C++ global constructors before calling main() yourself. Check the sample code that came with your board.
The scripted builds exercise the following toolchains, language modes, and HX_HARDENING_MODE combinations:
| Script | Toolchain | Language Modes | HX_HARDENING_MODE | Notes |
|---|---|---|---|---|
| debugbuild.sh | clang/clang++ | C17, C++23 | 3 | 32-bit debug build with ccache and no exceptions/RTTI. |
| testcmake.sh | cmake + default compiler | C17, C++23 | 3 | Uses the real Google Test and runs hxtest and Clang-Tidy. |
| testcoverage.sh | gcc, g++ + --coverage | C99, C++23 | 3 | Enables HX_TEST_ERROR_HANDLING=1 and emits coverage_details.html. |
| testmatrix.sh | gcc, clang (ASan/UBSan/TSan/MSan) | C99, C17, C++11, C++23 | 0-3 | Sweeps optimization levels and sets HX_USE_THREADS=1/11. |
| teststrip.sh | musl-gcc (static) | C17, C++11/14/17/20/23 | 0 | Size-focused static build with allocator/library stripping. |
| testwasm.sh | emcc | Emscripten defaults (Clang-based C/C++) | 3 (default) | WebAssembly build with the memory manager disabled and pthreads enabled. |
testall.sh runs all of the above and also enforces certain naming conventions.
testmsvc.bat tests 32 and 64-bit configurations of debug and release on Windows. The currently installed version of MSVC should be automatically discovered.
debugbuild.sh and teststrip.sh will detect when C++23 is missing and fall back to C++20 for testing g++-10 and clang-11.
The scripts are at the top level for easy access.
© 2017-2026 Adrian Johnston. This project is licensed under the terms of the MIT license found in the LICENSE.md file.
🪓🪓🪓