A minimal C++ program demonstrating basic ONNX Runtime inference. It loads an ONNX model that adds two float tensors (C = A + B), runs inference, and verifies the result.
onnx packagesamples/cxx/
├── CMakeLists.txt # Build configuration
├── main.cc # Sample program source
├── add_model.onnx # ONNX model (C = A + B)
├── generate_model.py # Script to generate the ONNX model
└── README.md # This file
Download and extract an ONNX Runtime release archive. For example:
tar -xf onnxruntime-win-x64-1.26.0.zip
This creates a directory like onnxruntime-win-x64-1.26.0/ containing include/ and lib/ subdirectories.
cd samples/cxx
pip install onnx
python generate_model.py
This creates add_model.onnx in the current directory.
From the samples/cxx directory:
Windows: cmake -S . -B build ^ -DORT_HEADER_DIR:PATH=path\to\onnxruntime-win-x64-1.26.0\include ^ -DORT_LIBRARY_DIR:PATH=path\to\onnxruntime-win-x64-1.26.0\lib cmake --build build --config Release
Linux / macOS: cmake -S . -B build \ -DORT_HEADER_DIR:PATH=path/to/onnxruntime-linux-x64-1.26.0/include \ -DORT_LIBRARY_DIR:PATH=path/to/onnxruntime-linux-x64-1.26.0/lib cmake --build build --config Release
Adjust the paths to match your extracted package name and location.
The build automatically copies the ONNX Runtime shared libraries next to the executable.
| Variable | Description |
|---|---|
ORT_HEADER_DIR |
Path to the ONNX Runtime include directory |
ORT_LIBRARY_DIR |
Path to the ONNX Runtime lib directory |
Windows: build\Release\onnxruntime_sample_program.exe
Linux / macOS: ./build/onnxruntime_sample_program
You can also pass a model path as an argument: onnxruntime_sample_program path/to/add_model.onnx