一.opencv依赖库的安装
在交叉编译之前,首先要安装opencv的依赖库,共有七个依赖库,下载链接如下:
链接:https://pan.baidu.com/s/1x91dvkSj-Y53Zw0XtFIi5w
提取码:jzt1
如果链接失效,需要下载的请联系我QQ邮箱:[email protected]
下载后将这些文件解压到同一文件夹(例如/usr/local/arm/opencv-arm)
解压后,分别对这些依赖库进行编译
cd zlib-1.2.11
CC=arm-linux-gcc ./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --shared
make
make install
cd libpng-1.6.36
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --enable-shared --enable-static --host=arm-linux
make
make install
cd jpeg-6b
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --enable-shared –enable-static
make
make isstall
注意:
./configure 过程会提示 bash: ./configure: /bin/sh^M: 解释器错误: 没有那个文件或目录
只需要终端输入:vim configure
进入末行模式,输入:set ff=unix,之后保存退出即可
make过程中会报错
./libtool: Command not found
这是由于缺少libtool文件,在上一步骤解压的libpng路径下就有libtool这个文件,将其复制一份到libjpeg路径下即可
make install过程中也会出现报错
/usr/bin/install: cannot create regular file
`/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi//man/man1/cjpeg': No such file or directory
这是缺少相应文件夹引起的,只需要在提示的目录下创建相应文件夹即可(例如本例中在/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/下创建man/man1即可)
接下来可能出现不同的提示缺少相应文件夹,只需创建即可解决问题
cd yasm-1.3.0
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --enable-shared --enable-static --host=arm-linux
make
make install
cd x264-snapshot-20190309-2245-stable
CC=arm-linux-gcc ./configure --enable-shared --host=arm-linux –disable-asm --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/
make
make install
cd xvidcore-1.3.3/build/generic
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --host=arm-linux --disable-assembly
make
make install
cd ffmpeg-4.1
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gcc --enable-swscale
在交叉编译时提示:ERROR: libx264 not found
此时增加--extra-ldflags=-ldl 支持动态链接装入器libdl(它有4个库函数 dlopen, dlerror, dlsym, dlclose)
./configure --prefix=/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/ --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gcc --enable-swscale --extra-ldflags=-ldl
至此,opencv依赖库全部安装成功
二.cmake的安装
在移植之前,需要安装cmake以及cmake-gui,可以通过终端输入cmake -version查看自己的cmake版本号
终端输入:apt-get install cmake-qt-gui
此次安装的是opencv-2.4.11,下载链接如下:
链接:https://pan.baidu.com/s/1FCT3JTbbC7OKwY_rfMa4WA
提取码:ilq7
下载后将文件解压,并使用mkdir创建两个文件夹build、output
之后进入build目录,运行cmake-gui
终端输入:cmake-gui
其中Where is the source code和Where to build the binaries分别选择源码目录和源码目录下的build目录,然后点击Configure
之后去掉WITH_TIFF、WITH_GTK,或者你不需要的模块。然后再搜索CMAKE_BUILD_TYPE,将这个键的值改成Release,搜索CMAKE_INSTALL_PREFIX,将这个键的值改成源码目录下的output文件夹
之后点击Next
在接下来的界面中输入目标系统(只能输linux)以及opencv源码的版本号、交叉编译链的命令存放位置:
点击finish,去掉WITH_TIFF、WITH_GTK选项,将CMAKE_BUILD_TYPE修改为Release,将
CMAKE_INSTALL_PREFIX的值改成源码目录下的output文件夹
之后点击Configure和Generate,将在该目录下生成Makefile文件
之后使用vim进入CMakeCache.txt修改配置文件
找到找到:CMAKE_EXE_LINKER_FLAGS:STRING=
修改为:CMAKE_EXE_LINKER_FLAGS:STRING=-lpthread -ldl -lrt
意思是:-lpthread支持线程,-ldl避免未定义dlopen,-lrt避免未定义
终端输入:make -j8(使用8个进程进行编译,节省编译时间)
编译成功之后终端输入:make install进行安装
安装后便会在output目录下生成如下文件
之后需要对相关环境变量进行配置
配置pkg-config,具体过程如下:
(奇怪的是,在编译output/lib下没有发现pkgconfig目录,因此,在配置pkg-config之前,需手动创建pkgconfig目录,并且在其目录下创建opencv.pc文件, 以便进行相关配置)
opencv.pc文件内容如下:
# Package Information for pkg-config
prefix=/usr/local/opencv/output
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library for arm
Version: 2.4.11
Libs: -L${exec_prefix}/lib -lopencv_ml -lopencv_objdetect -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videostab -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_ocl -lopencv_core -lopencv_nonfree -lopencv_legacy -lopencv_gpu
Cflags: -I${includedir_old} -I${includedir_new}
终端输入:vim /etc/ld.so.conf
将目录/usr/local/opencv/oepncv-2.4.11/output/lib添加到文件末尾
终端输入:
export PKG_CONFIG_PATH=
$PKG_CONFIG_PATH:/usr/local/opencv/opencv-2.4.11/output/lib/pkgconfig
终端输入:ldconfig -v
终端输入:pkg-config --libs --cflags opencv
表示安装成功;
接下来对测试程序进行编译,查看opencv是否交叉编译完成;
arm-linux-gcc main.cpp -o run -lpthread -lrt `pkg-config --libs --cflags opencv`
#include "highgui.h"
#include "opencv2/opencv.hpp"
#include
#define Usage() \
{ std::cerr << "Usage: ./showpic FILE" << std::endl; }
//using namespace cv;
int main(int argc, char** argv)
{
if (argc != 2) Usage();
IplImage* img = cvLoadImage(argv[1]);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
进行编译之后,在该目录下复制过来一个.jpg格式的图片文件
终端输入./a.out ./1.jpg(图片名)
成功执行后将会出现一个窗口显示图片
但是我在编译时提示
/usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libopencv_core.so, needed by /usr/local/opencv/output/lib/libopencv_ml.so, not found (try using -rpath or -rpath-link)
大量的not found (try using -rpath or -rpath-link),这里只列出一条
根据网上的教程,将相关库复制到交叉编译器所在的lib目录
进入opencv相关链接库/usr/local/opencv/output/lib目录
终端输入:cp libopenvc* /usr/local/arm/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/lib
报错信息消失;
但是提示:
/usr/local/opencv/output/lib/libopencv_ocl.so: undefined reference to `dlopen'
/usr/local/opencv/output/lib/libopencv_ocl.so: undefined reference to `dlsym'
只需在编译时加上-ldl选项即可
arm-linux-gcc test.cpp -o run -lpthread -lrt -ldl `pkg-config --libs --cflags opencv`