Ubuntu18.04 ONNXRuntime C++编译
参考:
安装编译:https://blog.csdn.net/m0_46303486/article/details/131681105
https://blog.csdn.net/fyc300/article/details/135369635
https://blog.csdn.net/hh1357102/article/details/131983210
cmake版本升级:https://blog.csdn.net/Boys_Wu/article/details/104940575
gcc版本升级:https://blog.csdn.net/tytyvyibijk/article/details/123074333?spm=1001.2014.3001.5502
安装环境
cmake
cmake版本:3.26.0及以上,下载网址:https://cmake.org/download/
# 下载源码
wget https://github.com/Kitware/CMake/releases/download/v3.31.2/cmake-3.31.2-linux-x86_64.tar.gz
# 解压
tar -xzvf cmake-3.31.2-linux-x86_64.tar.gz
# 将解压出来的包移到/opt目录下
sudo mv cmake-3.31.2-linux-x86_64 /opt/cmake-3.31.2
# 建立软链接
sudo ln -sf /opt/cmake-3.31.2/bin/* /usr/bin/
# 检查是否成功
cmake --version
将cmake的文件路径添加至 .bashrc里
# 修改~/.bashrc
sudo nano ~/.bashrc
# 输入以下内容
export PATH=$PATH:/opt/cmake-3.31.2/bin
# 更新source一下
source ~/.bashrc
gcc
gcc版本:9及以上
目前适用于Ubuntu18.04的GCC/G++ 11不在稳定仓库中,因此需要源码编译或者添加PPA仓库安装。
# 添加PPA仓库安装
add-apt-repository ppa:ubuntu-toolchain-r/test
# 更新软件源
sudo apt update
# 安装新版本GCC/G++
sudo apt install gcc-11 g++-11
查找所有已安装的GCC/G++,如果wsl更新索引较慢请参考:https://liu66.cc/archives/0fd2b3df-d5d7-4b68-bf7f-e864ab55a8f2#opencv
# 使用前更新一下索引
sudo updatedb && sudo ldconfig
locate gcc | grep -E "/usr/bin/gcc-"
#如果locate不能用
ls /usr/bin/gcc*
ls /usr/bin/g++*
切换到最新GCC/G++版本
# 命令最后的1和10是优先级,如果使用auto选择模式,系统将默认使用优先级高的
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 1
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 10
查看是否更新成功
gcc --version
g++ --version
源码安装
# 安装依赖
sudo apt update
sudo apt install -y build-essential cmake git libprotobuf-dev protobuf-compiler
# 克隆仓库并选择旧版本,否则容易因为版本过新而编译失败
cd ~/code && git clone --branch v1.8.0 --recursive https://github.com/microsoft/onnxruntime.git
cd onnxruntime
# 构建
./build.sh --skip_tests --config Release --build_shared_lib --parallel
若配合cuda使用,命令行末尾应添加
-cuda_home /usr/local/cuda-11.3 --cudnn_home /usr/local/cuda-11.3
# 安装
cd build/Linux/Release
sudo make install
评论区