Ubuntu 安装与使用C++ onnxruntime库

系列文章目录


文章目录


前言

一、Ubuntu中安装onnxruntime

1、一些依赖

一键安装ROS(不安装ROS可能会缺很多库,容易报错):

bash 复制代码
wget http://fishros.com/install -O fishros && . fishros

onnxruntime的一些依赖项:

bash 复制代码
$ sudo apt-get update
$ sudo apt-get install -y libprotobuf-dev protobuf-compiler

2、onnx安装

bash 复制代码
$ git clone https://github.com/microsoft/onnxruntime
$ cd onnxruntime
$ git checkout v1.19.0
$ ./build.sh --config Release --build_shared_lib --parallel --allow_running_as_root
$ cd onnxruntime/build/Linux/Release/
$ sudo make install

二、一些报错及解决

其他同学遇到的问题参考链接

问题1:cmake版本问题

bash 复制代码
CMake 3.26 or higher is required.  You are running version 3.16.3

Ubuntu 20.04升级cmake

使用cmake编译的时候提示:CMake 3.20 or higher is required. You are running version 3.16.3

所以需要升级cmake版本。

访问cmake官网:https://cmake.org/files/v3.22/,下载3.22.1版本(很多版本,不低于V3.20的都行)

bash 复制代码
$ wget https://cmake.org/files/v3.30/cmake-3.30.0.tar.gz
$ tar xzf cmake-3.30.0.tar.gz
$ cd cmake-3.22.1
$ ./configure
$ make
$ sudo make install
$ cmake --version

问题2:

bash 复制代码
make[2]: *** [CMakeFiles/gsl-populate.dir/build.make:100:gsl-populate-prefix/src/gsl-populate-stamp/gsl-populate-download] 错误 1
make[1]: *** [CMakeFiles/Makefile2:83:CMakeFiles/gsl-populate.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
CMake Error at /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1918 (message):
  Build step for gsl failed: 2
Call Stack (most recent call first):
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1609 (__FetchContent_populateSubbuild)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145:EVAL:2 (__FetchContent_doPopulation)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145 (cmake_language)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1978:EVAL:1 (__FetchContent_Populate)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1978 (cmake_language)
  external/helper_functions.cmake:139 (FetchContent_Populate)
  external/onnxruntime_external_deps.cmake:400 (onnxruntime_fetchcontent_makeavailable)
  CMakeLists.txt:598 (include)

-- Configuring incomplete, errors occurred!
Traceback (most recent call last):

该错误表明在编译onnxruntime时,CMake在获取GSL(Guidelines Support Library)依赖时失败。以下是解决方案和注意事项:

解决方案

‌手动安装GSL‌,下载GSL源码并编译安装:

bash 复制代码
git clone https://github.com/microsoft/GSL.git
cd GSL && mkdir build && cd build
cmake .. -DGSL_TEST=OFF && make -j$(nproc) && sudo make install

在onnxruntime编译时通过-Donnxruntime_USE_SYSTEM_GSL=ON指定系统GSL路径

‌若使用build.sh脚本,添加参数:

./build.sh --cmake_extra_defines onnxruntime_USE_SYSTEM_GSL=ON

或直接修改CMakeLists.txt,将onnxruntime_USE_SYSTEM_GSL设为ON

问题3:

bash 复制代码
make[2]: *** [CMakeFiles/eigen-populate.dir/build.make:100:eigen-populate-prefix/src/eigen-populate-stamp/eigen-populate-download] 错误 1
make[1]: *** [CMakeFiles/Makefile2:83:CMakeFiles/eigen-populate.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
CMake Error at /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1918 (message):
  Build step for eigen failed: 2
Call Stack (most recent call first):
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1609 (__FetchContent_populateSubbuild)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145:EVAL:2 (__FetchContent_doPopulation)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:2145 (cmake_language)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1978:EVAL:1 (__FetchContent_Populate)
  /usr/local/share/cmake-3.30/Modules/FetchContent.cmake:1978 (cmake_language)
  external/eigen.cmake:12 (FetchContent_Populate)
  external/onnxruntime_external_deps.cmake:500 (include)
  CMakeLists.txt:598 (include)


-- Configuring incomplete, errors occurred!

解决方法:

手动执行下载脚本

进入/media/zzz/lwl/hmpose/onnxruntime/build/Linux/Release/_deps/eigen-subbuild/eigen-populate-prefix/src/目录

bash 复制代码
cd eigen-subbuild/eigen-populate-prefix/src/
wget https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
tar -xzf eigen-3.4.0.tar.gz
echo "Success" > eigen-populate-stamp/eigen-populate-download

修改CMake缓存参数

编辑CMakeCache.txt,添加:

bash 复制代码
eigen_SOURCE_DIR:PATH=/path/to/local/eigen
eigen_POPULATE:BOOL=OFF

三、onnxruntime推理

C++ onnxruntime API参考:ONNX Runtime: Ort Namespace Reference

相关推荐
闻缺陷则喜何志丹13 分钟前
【贪心 字典序 回文 最长公共前缀】LeetCode3734. 大于目标字符串的最小字典序回文排列|分数未知
c++·算法·力扣·贪心·字典序·回文·最长公共前缀
_OP_CHEN1 小时前
C++进阶:(四)set系列容器的全面指南
开发语言·c++·stl·set·multiset·关联式容器·setoj题
赖small强2 小时前
【蓝牙】BLE 数据收发实战指南(手机 App ↔ 嵌入式 Linux/BlueZ)
linux·蓝牙·bluez·bluetoothctl·dbus-monitor·central·peripheral
huazeci2 小时前
deepin Ubuntu/Debian系统 环境下安装nginx,php,mysql,手动安装,配置自己的项目
nginx·ubuntu·debian
wit_yuan2 小时前
linux udp广播数据包实际用例
linux·服务器·udp
大聪明-PLUS2 小时前
io_uring:Linux 上的高性能异步 I/O
linux·嵌入式·arm·smarc
qq_479875432 小时前
Linux time function in C/C++【2】
linux·c语言·c++
小武~2 小时前
嵌入式网络编程深度优化 --网络协议栈配置实战指南
linux·网络·网络协议
二进制星轨3 小时前
在 Ubuntu 上快速配置 Node.js 环境(附问题说明)
linux·ubuntu·node.js
一个不知名程序员www3 小时前
算法学习入门---前缀和(C++)
c++·算法