Skip to main content

Embedding in C/C++

Timber generates self-contained C99 source code that you can embed directly in any C/C++ project.

Compile to Source

timber compile --model model.json --out ./inference/

This produces:

inference/
├── model.h # Public API
├── model.c # Inference logic
├── model_data.c # Tree data (static const)
├── CMakeLists.txt
└── Makefile

Using the Generated API

#include "model.h"
#include <stdio.h>

int main() {
// Initialize
TimberCtx* ctx;
int err = timber_init(&ctx);
if (err != TIMBER_OK) {
printf("Init failed: %s\n", timber_strerror(err));
return 1;
}

// Single inference
float inputs[TIMBER_N_FEATURES] = {17.99, 10.38, 122.8, /* ... */};
float outputs[TIMBER_N_OUTPUTS];

err = timber_infer_single(inputs, outputs, ctx);
if (err != TIMBER_OK) {
printf("Inference failed: %s\n", timber_strerror(err));
return 1;
}
printf("Prediction: %f\n", outputs[0]);

// Batch inference
float batch[100 * TIMBER_N_FEATURES];
float results[100 * TIMBER_N_OUTPUTS];
// ... fill batch ...
err = timber_infer(batch, 100, results, ctx);

// Cleanup
timber_free(ctx);
return 0;
}

Building

With Make:

cd inference/
make # Produces libtimber_model.so and libtimber_model.a

With CMake:

cd inference/
mkdir build && cd build
cmake .. && make

Link against your project:

gcc -O2 -o my_app my_app.c -I./inference -L./inference -ltimber_model -lm

Error Codes

CodeConstantMeaning
0TIMBER_OKSuccess
-1TIMBER_ERR_NULLNull pointer argument
-2TIMBER_ERR_INITContext not initialized
-3TIMBER_ERR_BOUNDSArgument out of bounds

Runtime Logging

Set a callback to receive log messages from the generated code:

void my_logger(int level, const char* msg) {
const char* labels[] = {"ERROR", "WARN", "INFO", "DEBUG"};
fprintf(stderr, "[timber][%s] %s\n", labels[level], msg);
}

// Register before inference
timber_set_log_callback(my_logger);

When no callback is set, logging is a no-op with zero overhead.

Constants

The generated header provides compile-time constants:

#define TIMBER_N_FEATURES  30    // Input feature count
#define TIMBER_N_OUTPUTS 1 // Output count
#define TIMBER_N_TREES 50 // Tree count
#define TIMBER_ABI_VERSION 1 // ABI version for compatibility