tokenizers-c is a thin, panic-safe Rust FFI staticlib wrapper around the official HuggingFace tokenizers Rust crate (v0.23) that compiles into an embeddable C header (dist/include/tokenizers.h) and static library archive (dist/lib/libtokenizers.a / dist/lib/tokenizers.lib) for Windows, Linux, and macOS.
Building the project runs cargo build --release and populates dist/:
dist/include/tokenizers.h— Single embeddable C header (compatible with C99, C11, and C++11+, zero external header dependencies).dist/lib/libtokenizers.a— Static library archive for GCC / Clang / MinGW-w64 / Unix linkers.dist/lib/tokenizers.lib— Static library archive copy for Windows MSVC (cl.exe/link.exe).dist/lib/pkgconfig/tokenizers.pc—pkg-configmetadata with platform linker flags.
- Rust & Cargo (
rustup/cargo>= 1.75) to compile the upstreamtokenizerscrate. - C Compiler (
cl.exeon Windows MSVC,gccon MinGW-w64 / Linux, orclangon Windows / macOS / Linux) to compile and link your C application, examples, or unit tests. fancy-regexis enabled by default and compiles on Windows MSVC and MinGW without an external Oniguruma library. HTTP/Hugging Face Hub loading is disabled by default to keep the static library smaller. Enable it withcargo build --release --features httpto usetk_from_pretrained; local tokenizer loading works without it. Optional Oniguruma andesaxx_fastsupport can be enabled with--features onig,esaxx_fast.
:: Build dist\include\tokenizers.h + dist\lib\libtokenizers.a & dist\lib\tokenizers.lib via Cargo
build.bat
:: Optional: cross-compile specifically for MinGW-w64 GNU ABI or MSVC ABI
build.bat gnu
build.bat msvc
:: Compile and run unit tests and examples (auto-selects MSVC or MinGW ABI)
build.bat test
build.bat examples
:: Optionally choose the matching ABI explicitly
build.bat test gnu
build.bat examples msvc
build\basic_usage.exe
build\batch_and_pair.exe
build\streaming_decode.exe
:: Optional arguments: tokenizer JSON path, then text to encode
build\streaming_decode.exe examples\sample_bert_tokenizer.json "Hello world, one token at a time!"# Build static library & header via Cargo, then compile and run C tests and examples
.\build.ps1 -Test -Examples
# Or specify a specific Rust target triple (e.g. for MinGW gcc linking):
.\build.ps1 -Target x86_64-pc-windows-gnu -TestFrom an x64 Native Tools Command Prompt for VS:
nmake /f NMakefile
nmake /f NMakefile test
nmake /f NMakefile examplescmake -B build
cmake --build build --config Release
ctest --test-dir build -C Release --output-on-failuremingw32-make
mingw32-make test
mingw32-make examplesmake
make test
make examples
./build/basic_usage
./build/batch_and_pair
./build/streaming_decodeCopy dist/include/tokenizers.h and dist/lib/libtokenizers.a (or dist/lib/tokenizers.lib on MSVC) into your C project:
#include "tokenizers.h"
#include <stdio.h>
int main(void) {
/* Load a local tokenizer.json. Build with --features http to use tk_from_pretrained(). */
TkTokenizer *tk = tk_from_file("tokenizer.json");
if (!tk) {
fprintf(stderr, "Error: %s\n", tk_last_error_message());
return 1;
}
TkEncoding *enc = tk_encode(tk, "Hello, HuggingFace tokenizers!", true);
size_t len = 0;
const uint32_t *ids = tk_encoding_get_ids(enc, &len);
for (size_t i = 0; i < len; i++) {
printf("%u (%s) ", ids[i], tk_encoding_get_token(enc, i));
}
printf("\n");
char *decoded = tk_decode(tk, ids, len, true);
printf("Decoded: %s\n", decoded);
tk_string_free(decoded);
tk_encoding_free(enc);
tk_free(tk);
return 0;
}Because libtokenizers.a / tokenizers.lib is a Rust staticlib, link the standard OS system libraries required by the Rust standard library and HTTP/TLS stack:
- Windows (MSVC
cl.exe, built withx86_64-pc-windows-msvc):cl /O2 /Idist\include my_app.c dist\lib\tokenizers.lib ws2_32.lib userenv.lib advapi32.lib bcrypt.lib ntdll.lib crypt32.lib secur32.lib
- Windows (MinGW-w64
gcc, built withx86_64-pc-windows-gnu):gcc -O2 -Idist/include my_app.c dist/lib/libtokenizers.a -lws2_32 -luserenv -ladvapi32 -lbcrypt -lntdll -lcrypt32 -lsecur32 -lstdc++ -lm -lpthread -o my_app.exe
- Linux:
cc -O2 -Idist/include my_app.c dist/lib/libtokenizers.a -Wl,--no-as-needed -ldl -lpthread -lm -lstdc++ -lgcc_s -lc -o my_app
- macOS:
cc -O2 -Idist/include my_app.c dist/lib/libtokenizers.a -framework Security -framework CoreFoundation -liconv -lSystem -lc++ -lm -lpthread -o my_app
- Via
pkg-config(Linux / macOS / MSYS2):PKG_CONFIG_PATH=dist/lib/pkgconfig cc -O2 my_app.c $(pkg-config --cflags --libs tokenizers) -o my_app