参考博文:Ubuntu20.04 OpenCV详细安装教程(附多版本切换共存教程)_ubuntu20.04安装opencv-CSDN博客
Ubuntu 20.04上搭建OpenCV 4.5.0 & C++环境
从源码构建
1---下载
官网地址:Releases - OpenCV
选择需要的版本,点击Sources下载zip源码压缩包,下载后解压。
2---编译过程
1>安装工具
sudo apt update
sudo apt install -y cmake g++
sudo apt install make ninja-build
2>编译
在编译OpenCV库之前,需要配置CMakeLists.txt文件。
编辑CMakeLists.txt文件,将set(CMAKE_INSTALL_PREFIX /usr/local)
修改为set(CMAKE_INSTALL_PREFIX /usr/local/opencv)
,以避免与系统中的其他OpenCV版本冲突。
保存并关闭文件。
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local/opencv
编译 OpenCV库:
make -j$(nproc)
编译成功后,安装OpenCV库:
sudo make install
设置环境变量:
nano ~/.bashrc
在文件的末尾添加以下行:
```bash
export OpenCV_DIR=/usr/local/opencv/share/OpenCV
export PATH=$PATH:/usr/local/opencv/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib:/usr/local/opencv/3rdparty/libprotobuf/lib/../protobuf/libprotobuf-lite.so:/usr/local/opencv/3rdparty/libprotobuf/lib/../protobuf/libprotobuf.so:/usr/local/opencv/3rdparty/protobuf/src/.libs/libprotobuf.so:/usr/local/opencv/3rdparty/gtest/lib/.libs:/usr/local/opencv/3rdparty/gtest/src/.libs:/usr/local/opencv/3rdparty/gtest/include:/usr/local/opencv/3rdparty/gtest//include:/usr/local/opencv//include:/usr/local//include:/usr//include:/usr//include-fixed:.export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv//lib/pkgconfig:/usr//lib/pkgconfig:.export PYTHONPATH=$PYTHONPATH:/usr//python3.6/site-packages:.export PATH=/usr//bin:.export PYTHONPATH=$PYTHONPATH:/usr//python3.6/site-packages:.export PATH=/usr//bin:.export PATH=/usr//bin:.export PATH=/usr//bin:.export PYTHONPATH=/usr//python3.6/site-packages:.export PATH=/usr//bin:.export PATH=/usr//bin:.export PYTHONPATH=/usr//python3.6site-packages:.export PATH=/usr//bin:.export PATH=/usr//bin:.export PYTHONPATH=/usr//python3
若在设置环境变量时无法写入,权限不够,修改为root用户,或者新建一个文件复制粘贴一下(但不确定这样是否会报错哈) ,可以多搜索一些,查找更多的解决方法。
换一种方法:
gedit ~/.bashrc
在文末加入:(按自己的路径修改)
export PATH=/usr/local/opencv:$PATH
需要通过以下命令使修改生效,修改PATH配置后仅对当前用户永久生效 :
source ~/.bashrc
标签:2024.12,Ubuntu20.04,OpenCV,opencv,export,usr,PATH,local
From: https://blog.csdn.net/m0_69023437/article/details/144497800