ONNX Runtime Linux-x64安装 ONNX Runtime Linux-x64安装ONNX Runtime Linux-x64 C 完整安装教程CPU版Ubuntu 20.04/22.04方案一官方预编译包推荐5分钟装好无需编译1. 安装系统依赖sudo apt update sudo apt install -y build-essential cmake libprotobuf-dev protobuf-compiler2. 下载预编译包选稳定版示例v1.18.0GitHub Releaseshttps://github.com/microsoft/onnxruntime/releasesCPU包onnxruntime-linux-x64-1.18.0.tgzGPU包(CUDA)onnxruntime-linux-x64-gpu-1.18.0.tgz# 下载 wget https://github.com/microsoft/onnxruntime/releases/download/v1.18.0/onnxruntime-linux-x64-1.18.0.tgz # 解压 tar -zxvf onnxruntime-linux-x64-1.18.0.tgz # 全局安装到/opt系统全局可用 sudo mv onnxruntime-linux-x64-1.18.0 /opt/onnxruntime解压目录结构/opt/onnxruntime ├─ include/ # C头文件 └─ lib/ ├─ libonnxruntime.so ├─ libonnxruntime.so.1.18.0 └─ libonnxruntime_providers_shared.so3. 配置动态链接运行时能找到so方式A永久全局生效推荐# 添加库路径 echo /opt/onnxruntime/lib | sudo tee /etc/ld.so.conf.d/onnxruntime.conf # 更新缓存 sudo ldconfig # 验证是否识别 ldconfig -p | grep onnxruntime方式B临时生效仅当前终端export LD_LIBRARY_PATH/opt/onnxruntime/lib:$LD_LIBRARY_PATH4. CMakeLists.txt 集成你的项目FCOS3D部署工程直接套用cmake_minimum_required(VERSION 3.16) project(fcos3d_deploy) set(CMAKE_CXX_STANDARD 17) # ONNX Runtime 根路径 set(ONNXRUNTIME_ROOT /opt/onnxruntime) # 头文件目录 include_directories(${ONNXRUNTIME_ROOT}/include) # 库搜索目录 link_directories(${ONNXRUNTIME_ROOT}/lib) # 你的源码 file(GLOB SRC src/*.cpp) add_executable(fcos3d ${SRC}) # 链接onnxruntime target_link_libraries(fcos3d onnxruntime opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs pthread )5. 最简C测试代码验证安装新建test_ort.cpp#include iostream #include onnxruntime_cxx_api.h int main() { // 获取ORT版本 std::cout ONNX Runtime Version: Ort::GetVersionString() std::endl; // 创建环境 Ort::Env env(ORT_LOGGING_LEVEL_WARNING, test); Ort::SessionOptions session_options; session_options.SetIntraOpNumThreads(4); session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); std::cout ORT init success! std::endl; return 0; }编译运行mkdir build cd build cmake .. make -j$(nproc) ./fcos3d输出版本号即安装成功。方案二源码编译自定义编译选项如开启OpenBLAS/DNNL1. 拉取源码子模块git clone --recursive https://github.com/microsoft/onnxruntime.git cd onnxruntime git checkout v1.18.0 git submodule update --init --recursive --force2. 一键编译脚本CPU Release./build.sh \ --config Release \ --build_shared_lib \ --parallel $(nproc) \ --use_openmp \ --use_dnnl--use_cuda开启GPU需要提前装CUDA/cuDNN--build_shared_lib生成动态库.so3. 编译产物路径build/Linux/Release/ ├─ include/ └─ lib/libonnxruntime.so4. 安装到系统sudo cp -r build/Linux/Release/include/* /usr/local/include/ sudo cp build/Linux/Release/lib/*.so /usr/local/lib/ sudo ldconfig常见问题排查error while loading shared libraries: libonnxruntime.so未配置ldconfig或LD_LIBRARY_PATH执行前文ldconfig步骤。找不到头文件 onnxruntime_cxx_api.hCMake中ONNXRUNTIME_ROOT路径写错确认解压路径。编译报错 protobuf 相关安装依赖sudo apt install libprotobuf-dev protobuf-compiler配合你之前ARM64部署说明x86_64(x64)包不能在aarch64开发板使用ARM设备需下载onnxruntime-linux-aarch64包。GPU版本补充如需CUDA推理下载GPU包wget https://github.com/microsoft/onnxruntime/releases/download/v1.18.0/onnxruntime-linux-x64-gpu-1.18.0.tgzCMake链接时额外加provider库target_link_libraries(fcos3d onnxruntime onnxruntime_providers_cuda onnxruntime_providers_shared )