Skip to main content

C/C++ Inference API

The ONNX Runtime C++ API provides high-performance inference with RAII-based resource management and exception safety. This guide covers the complete C++ API with real examples from the codebase.

Installation

Using NuGet (Windows)

Using vcpkg

Manual Installation

Download pre-built binaries from the ONNX Runtime releases page.

Quick Start

Here’s a minimal C++ example:

Core Classes

Ort::Env

The environment manages global state. Create one per application.
Logging levels:
  • ORT_LOGGING_LEVEL_VERBOSE (0)
  • ORT_LOGGING_LEVEL_INFO (1)
  • ORT_LOGGING_LEVEL_WARNING (2)
  • ORT_LOGGING_LEVEL_ERROR (3)
  • ORT_LOGGING_LEVEL_FATAL (4)

Ort::SessionOptions

Configure session creation and optimization.
Graph optimization levels:

Ort::Session

The main inference session class. Create from file:
Create from memory:
Query model metadata:
Get model metadata:

Running Inference

Basic inference:

Ort::Value

Represents tensors and other values. Create tensor from existing data:
Create tensor with allocator:
Query tensor properties:

Execution Providers

Add Execution Providers

CUDA:
TensorRT:
DirectML (Windows):
CoreML (macOS/iOS):

Complete Example: Image Classification

Memory Management

The C++ API uses RAII (Resource Acquisition Is Initialization) for automatic resource management:

Error Handling

Next Steps

Model Optimization

Optimize models for production deployment

Execution Providers

Configure hardware acceleration