
ONNX Runtime Linux-x64安裝ONNX Runtime Linux-x64 C 完整安裝教程CPU版Ubuntu 20.04/22.04方案一官方預(yù)編譯包推薦5分鐘裝好無(wú)需編譯1. 安裝系統(tǒng)依賴sudo apt update sudo apt install -y build-essential cmake libprotobuf-dev protobuf-compiler2. 下載預(yù)編譯包選穩(wěn)定版示例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系統(tǒng)全局可用 sudo mv onnxruntime-linux-x64-1.18.0 /opt/onnxruntime解壓目錄結(jié)構(gòu)/opt/onnxruntime ├─ include/ # C頭文件 └─ lib/ ├─ libonnxruntime.so ├─ libonnxruntime.so.1.18.0 └─ libonnxruntime_providers_shared.so3. 配置動(dòng)態(tài)鏈接運(yùn)行時(shí)能找到so方式A永久全局生效推薦# 添加庫(kù)路徑 echo /opt/onnxruntime/lib | sudo tee /etc/ld.so.conf.d/onnxruntime.conf # 更新緩存 sudo ldconfig # 驗(yàn)證是否識(shí)別 ldconfig -p | grep onnxruntime方式B臨時(shí)生效僅當(dāng)前終端export LD_LIBRARY_PATH/opt/onnxruntime/lib:$LD_LIBRARY_PATH4. CMakeLists.txt 集成你的項(xiàng)目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) # 庫(kù)搜索目錄 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. 最簡(jiǎn)C測(cè)試代碼驗(yàn)證安裝新建test_ort.cpp#include iostream #include onnxruntime_cxx_api.h int main() { // 獲取ORT版本 std::cout ONNX Runtime Version: Ort::GetVersionString() std::endl; // 創(chuàng)建環(huán)境 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; }編譯運(yùn)行mkdir build cd build cmake .. make -j$(nproc) ./fcos3d輸出版本號(hào)即安裝成功。方案二源碼編譯自定義編譯選項(xiàng)如開啟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生成動(dòng)態(tài)庫(kù).so3. 編譯產(chǎn)物路徑build/Linux/Release/ ├─ include/ └─ lib/libonnxruntime.so4. 安裝到系統(tǒng)sudo cp -r build/Linux/Release/include/* /usr/local/include/ sudo cp build/Linux/Release/lib/*.so /usr/local/lib/ sudo ldconfig常見(jiàn)問(wèn)題排查error while loading shared libraries: libonnxruntime.so未配置ldconfig或LD_LIBRARY_PATH執(zhí)行前文ldconfig步驟。找不到頭文件 onnxruntime_cxx_api.hCMake中ONNXRUNTIME_ROOT路徑寫錯(cuò)確認(rèn)解壓路徑。編譯報(bào)錯(cuò) protobuf 相關(guān)安裝依賴sudo apt install libprotobuf-dev protobuf-compiler配合你之前ARM64部署說(shuō)明x86_64(x64)包不能在aarch64開發(fā)板使用ARM設(shè)備需下載onnxruntime-linux-aarch64包。GPU版本補(bǔ)充如需CUDA推理下載GPU包wget https://github.com/microsoft/onnxruntime/releases/download/v1.18.0/onnxruntime-linux-x64-gpu-1.18.0.tgzCMake鏈接時(shí)額外加provider庫(kù)target_link_libraries(fcos3d onnxruntime onnxruntime_providers_cuda onnxruntime_providers_shared )