目录
一、安装
1、安装依赖:
2、编译安装ffmpeg
(1)下载源码并解压;
(2)指定编译链和路径:
(3)编译安装
3、编译安装opencv
(1)下载源码并解压
(2)配置
(3)编译并安装
二、坑列表
1、编译opencv报错 c++: internal compiler error: Killed (program cc1plus)
2、解决ffmpeg执行报错“ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory”的问题
3、Ubuntu系统opencv编译错误:Makefile:162:recipe for target ‘all‘ failed make:*** [all] Error 2
4、编译opencv源码时报错:[ 62%] Built target opencv_dnn Makefile:162: recipe for target 'all' failed解决办法
5、error: ‘CODEC_ID_H264’ was not declared in this scope
6、执行opencv的小demo报错:./test1: error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory
一、安装
如果在虚拟机中交叉编译,建议将所有的lib库装在同一个目录下,我曾经安装多个lib库时装在不同的路径下,在安装ffmpeg时依赖太多前面提到的库,指定路径太多且混乱,导致ffmeg安装失败。
所以我把交叉编译链装到了arm板上,直接在板子上编译安装。我的板子可连外网(板子有4G模块,或者用网线连pc,通过pc访问外网)。以下是其步骤:
交叉编译opencv,在次之前,需要安装多个lib库和ffmpeg
1、安装依赖:
依赖包括cmake、pkg-config、yasm、X264等
sudo add-apt-repository ppa:linuxuprising/libpng12
apt-get update && apt-get -y install autoconf automake build-essential cmake ruby libpcre3 libpcre3-dev openssl vim tree lrzsz iproute2 pcregrep procps dnsutils netcat git-core libass-dev libfreetype6-dev libgnutls28-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget yasm zlib1g-dev checkinstall libfaac-dev libgpac-dev ladspa-sdk-dev libunistring-dev libbz2-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libvpx-dev libx264-dev libx265-dev libxvidcore-dev libopenal-dev libopus-dev libsdl1.2-dev libtheora-dev libx11-dev libxfixes-dev texi2html libssl-dev libfdk-aac-dev
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libcanberra-gtk-module
sudo apt-get install pkg-config
sudo apt-get install -y autoconf automake build-essential git libass-dev libfreetype6-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev
sudo apt install libswresample-dev
sudo apt install libavutil-dev
sudo apt install libsdl1.2-dev
sudo apt-get install libx264-142
sudo apt-get install libx264-152
apt-get install libtbb2 libtbb-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libpng12-0 libpng12-dev
如果上述有安装失败的,不能解决便继续往后走。
2、编译安装ffmpeg
(1)下载源码并解压;
(2)指定编译链和路径:
因为板子只有这一个gcc,所以不执行该步也可
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
export PATH=$PATH:/home/toolchain/gcc-linaro-7.5.0-arm-linux-gnueabihf/bin
(3)编译安装
进入源码路径,make -j8 & make install
遇到报错再一一解决
3、编译安装opencv
(1)下载源码并解压
(2)配置
进入源码目录,创建目录build:cd opencv; mkdir build
进入build: cd build
配置:
cmake -D CMAKE_BUILD_TYPE=Release –D CMAKE_INSTALL_PREFIX=/usr/local -D ENABLE_PRECOMPILED_HEADERS=OFF -D WITH_FFMPEG=OFF ..
(3)编译并安装
make -j4 & make install
安装过程由机器的性能决定,如果不出任何错误的话,我用了2个多小时。
每出现一个错误,解决后编译进度从零开始,但是之前完成过的进度会快很多。我总共遇到七八个error,前前后后花了1天时间才编译成功。
二、坑列表
1、编译opencv报错 c++: internal compiler error: Killed (program cc1plus)
原因:
内存不足
方法:
创建分区
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 # 1 * 2048 = 2048 创建 1 g 的内存分区
sudo mkswap /swapfile
sudo swapon /swapfile
# free -m #可以查看内存使用
# 创建完交换分区之后就可以继续编译
# 编译完之后记得用以下命令关闭交换分区
# 某次我就是忘了关闭交换分区,导致开不了机,然后切换 tty1 ,登进去之后关闭交换分区才可以进入桌面的。
#2. 关闭分区
sudo swapoff /swapfile
sudo rm /swapfile
2、解决ffmpeg执行报错“ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory”的问题
方法:
1、看下需要哪些依赖
ldd ffmpeg
2、搜索找不到的依赖
find /usr -name 'libavdevice.so.60'
发现它在路径/usr/local/lib/下
3、添加路径
export LD_LIBRARY_PATH=/usr/local/lib/
再执行ffmpeg -version就可以了
3、Ubuntu系统opencv编译错误:Makefile:162:recipe for target ‘all‘ failed make:*** [all] Error 2
参照博客 https://blog.csdn.net/wzw_2008/article/details/106944407
原因:原因在文件opencv-4.5.5/build/CMakeDownloadLog.txt
cat CMakeDownloadLog.txt
#use_cache "/home/opencv-4.5.5/.cache"
#do_unpack "v0.1.1f.zip" "b624b995ec9c439cbc2e9e6ee940d3a2" "https://github.com/opencv/ade/archive/v0.1.1f.zip" "/home/opencv-4.5.5/build/3rdparty/ade"
#cmake_download "/home/opencv-4.5.5/.cache/ade/b624b995ec9c439cbc2e9e6ee940d3a2-v0.1.1f.zip" "https://github.com/opencv/ade/archive/v0.1.1f.zip"
#try 1
# Trying 20.205.243.166...
# TCP_NODELAY set
# Connected to github.com (20.205.243.166) port 443 (#0)
# ALPN, offering http/1.1
# successfully set certificate verify locations:
# CAfile: /etc/ssl/certs/ca-certificates.crt
# CApath: /etc/ssl/certs
# [5 bytes data]
# TLSv1.3 (OUT), TLS handshake, Client hello (1):
# [512 bytes data]
# OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
# stopped the pause stream!
# Closing connection 0
下载https://github.com/opencv/ade/archive/v0.1.1f.zip失败
解决方案:
1、手动下载再放到路径/home/opencv-4.5.5/.cache/ade下
2、重新cmake:
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
3、再次make
4、编译opencv源码时报错:[ 62%] Built target opencv_dnn Makefile:162: recipe for target 'all' failed解决办法
解决方案:sudo su下进行make操作,虽然你可能在root权限下操作。
5、error: ‘CODEC_ID_H264’ was not declared in this scope
解决方案:cmake直接关闭ffmpeg,在后面加上
-D WITH_FFMPEG=OFF 文章来源:https://www.toymoban.com/news/detail-653966.html
6、执行opencv的小demo报错:./test1: error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory
共享库出现问题,增加动态库搜索路径即可:
在/etc/ld.so.conf 追加opencv库所在的路径/usr/local/lib文章来源地址https://www.toymoban.com/news/detail-653966.html
到了这里,关于在arm版ubuntu上编译安装c++版本opencv和闭坑指南的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!