Below is a concise, end-to-end recipe that has worked on every Pi from the 3B+ to the Pi 5 running 32-bit Raspberry Pi OS (Bullseye/Bookworm).
It gives you:
A self-built TensorFlow Lite C++ runtime (static library + headers)
A minimal CMake project that links it and runs a model from C++
Optional Python wheel (if you also want Python later)
You can copy-paste the commands into an SSH session on the Pi.
Prerequisites
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential git cmake wget
libjpeg-dev libpng-dev libopenblas-dev
libeigen3-dev libssl-dev python3-dev python3-pip
pkg-config zip unzip
(Optional) accelerate with NEON/FPU
echo "arm_64bit=0" | sudo tee -a /boot/config.txt # stay 32-bit
sudo reboot
Build TensorFlow Lite C++ runtime
2.1 Clone TensorFlow
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v2.15.0 # or any recent tag
2.2 Configure (say NO to everything except “download clang-format”)
./configure
2.3 Build the C++ static library
bazel build --config=elinux_armhf
--config=monolithic
--config=noaws
--config=nogcp
--config=nohdfs
--config=nonccl
-c opt
//tensorflow/lite:libtensorflowlite.so
2.4 Copy the artifacts somewhere convenient
mkdir -p ~/tflite_arm
cp bazel-bin/tensorflow/lite/libtensorflowlite.so ~/tflite_arm/
cp -r tensorflow/lite ~/tflite_arm/include
FlatBuffers headers
cp -r bazel-tensorflow/external/flatbuffers/include/flatbuffers ~/tflite_arm/include/
Build a minimal C++ demo
mkdir ~/tflite_demo && cd ~/tflite_demo
3.1 CMakeLists.txt
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.16)
project(tflite_demo)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -mfpu=neon -mfloat-abi=hard")
find_package(Threads REQUIRED)
add_executable(infer main.cpp)
target_include_directories(infer PRIVATE
E N V H O M E / t f l i t e a r m / i n c l u d e ENV{HOME}/tflite_arm/include
E N V H O M E / t f l i t e a r m / in c l u d e ENV{HOME}/tflite_arm/include/flatbuffers
)
target_link_libraries(infer
E N V H O M E / t f l i t e a r m / l i b t e n s o r f l o w l i t e . s o ENV{HOME}/tflite_arm/libtensorflowlite.so
E N V H O M E / t f l i t e a r m / l ib {CMAKE_THREAD_LIBS_INIT}
dl
)
EOF
3.2 main.cpp – load a .tflite and run one inference
cat > main.cpp <<'EOF'
#include
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Usage: %s model.tflite\n", argv[0]);
return 1;
}
code Copy code
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(argv[1]);
if (!model) {
printf("Failed to load model\n");
return 1;
}
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
if (interpreter->AllocateTensors() != kTfLiteOk) {
printf("AllocateTensors failed\n");
return 1;
}
// Fill your input tensor here
float* input = interpreter->typed_input_tensor<float>(0);
for (int i = 0; i < interpreter->input_tensor(0)->bytes / sizeof(float); ++i)
input[i] = 1.0f;
if (interpreter->Invoke() != kTfLiteOk) {
printf("Invoke failed\n");
return 1;
}
float* output = interpreter->typed_output_tensor<float>(0);
printf("Output[0] = %f\n", output[0]);
return 0;
}
EOF
3.3 Build & run
mkdir build && cd build
cmake ..
make -j$(nproc)
./infer ~/my_model.tflite
(Optional) Python wheel
Same source tree
bazel build --config=elinux_armhf -c opt
//tensorflow/lite/python:tensorflow_lite_python_wheel
pip3 install bazel-bin/tensorflow/lite/python/tensorflow_lite-*-cp39-cp39-linux_armv7l.whl
Performance tips
Use --config=elinux_armhf -c opt --copt=-mfpu=neon when you build.
For Coral USB, link against libedgetpu.so instead of the CPU delegate.
If you switch to 64-bit OS, change --config=elinux_aarch64 and drop the hard-float flags.
That’s it—your Pi now has a native C++ TensorFlow Lite runtime you can link into any project.