本文所有操作均在root用户下进行。
编译环境:Ubuntu-16.04
交叉编译器:arm-2014.05
目标机器:迅为IMX6Q
编译OpenCV的编译器和依赖库文件,免费分享给大家:编译ARM版OpenCV所需编译器和依赖库文件
交叉编译OpenCV3.4.10需要配置编译环境及安装依赖库:
apt-get install cmake
apt-get install cmake-qt-gui
apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff4-dev libswscale-dev libjasper-dev
1.protobuf的安装
1.1安装一些库
apt-get install autoconf automake libtool curl make g++ unzip
1.2安装源文件
git clone https://github.com/protocolbuffers/protobuf.git
1.3进入protobuf目录
cd protobuf
1.4clone子模块的依赖
git submodule update --init --recursive
1.5执行自动生成的shell脚本
./autogen.sh
1.6配置文件shell脚本
./configure
1.7make编译
make
1.8install安装
sudo make install
1.9刷新
ldconfig
1.10查看安装结果
protoc --version
以下交叉编译都需进入各自的文件夹进行操作,不再赘述。
2.libz的交叉编译
./configure --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/zlib-install --shared
修改Makefile
gedit Makefile
CC=arm-none-linux-gnueabi-gcc
LDSHARED=arm-none-linux-gnueabi-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
AR=arm-none-linux-gnueabi-ar
RANLIB=arm-none-linux-gnueabi-ranlib
编译和安装
make
make install
3.libjpeg的交叉编译
./configure --host=arm-linux --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libjpeg-install --enable-shared --enable-static
make
make install
4.libpng的交叉编译
./configure --host=arm-linux --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libpng-install --enable-shared --enable-static
make
make install
5.yasm的交叉编译
./configure --host=arm-linux --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/yasm-install --enable-shared --enable-static
make
make install
6.libx264的交叉编译
CC=arm-none-linux-gnueabi-gcc ./configure --enable-shared --host=arm-linux --disable-asm --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libx264-install
make
make install
7.libxvid的交叉编译
cd build/generic
./configure --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libxvid-install --host=arm-linux --disable-assembly
make
make install
8.ffmpeg的交叉编译
./configure --prefix=/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/ffmpeg-install --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --extra-cflags=-I/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libx264-install/include --extra-ldflags=-L/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libx264-install/lib --extra-cflags=-I/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libxvid-install/include --extra-ldflags=-L/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/libxvid-install/lib --cc=arm-none-linux-gnueabi-gcc --enable-swscale
make
make install
所有依赖库交叉编译安装完成
安装目录在:/usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/ 每个库有各自的-install 文件夹
新建文件夹
mkdir /usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/opencv-depend/
然后拷贝所有-install 下的 lib 下的文件到 opencv-depend 目录下
cd /usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/
cp -r ffmpeg-install/lib/ opencv-depend/
cp -r libjpeg-install/lib/ opencv-depend/
cp -r libpng-install/lib/ opencv-depend/
cp -r libx264-install/lib/ opencv-depend/
cp -r libxvid-install/lib/ opencv-depend/
cp -r yasm-install/lib/ opencv-depend/
cp -r zlib-install/lib/ opencv-depend/
好了,现在开始交叉编译OpenCV3.4.10
OpenCV3.4.10的源码放在:/usr/local/arm/opencv-3.4.10/
在此目录下创建build-arm并进入
mkdir build-arm && cd build-arm
1.配置cmake
gedit toolchain.cmake
在里面添加
###########user defined#############
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-none-linux-gnueabi-gcc )
set( CMAKE_CXX_COMPILER arm-none-linux-gnueabi-g++ )
###########user defined#############
set( CMAKE_FIND_ROOT_PATH" /usr/local/arm/arm-2014.05/arm-none-linux-gnueabi/opencv-depend " )
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
######################################
然后执行这一步
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../
2.配置cmake-gui
cmake-gui
选择源代码目录:/usr/local/arm/opencv-3.4.10
选择 Build 目录:/usr/local/arm/opencv-3.4.10/build-arm
需要修改的地方如下:
CMAKE_INSTALL_PREFIX默认安装目录为/usr/local,改为/usr/local/arm/opencv-arm(因为之前安装了 x86 的 opencv 在 usr/local 为了避免弄混了)
完成后点击Generate,修改 CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS :STRING= 后面原来为空,加上 -pthread -lrt -ldl
gedit CMakeCache.txt
之后就是make和make install(出现错误一般是选多了或者前面做错了 make clean)
make
make install
最后,编译成功,当看到这些库文件时就说明OK啦。
来看一个库文件的信息,是ARM平台的,就可以移植到开发板运行了,详细内容请看下一篇博客。
和/usr/local/lib/下对比看一下,一个是x86-64平台的,一个是ARM平台的。用时不要混淆了。
最后,需要把/usr/local/arm/opencv-arm/lib 下所有的动态链接库拷贝到交叉编译下/usr/local/arm/arm-2014.05/arm-non-linux-gnueabi/lib 下,其实这最后一步可以不做。
在Qt中使用交叉编译的OpenCV,生成开发板可以直接运行的ARM程序
INCLUDEPATH += /usr/local/arm/opencv-arm/include \
/usr/local/arm/opencv-arm/include/opencv \
/usr/local/arm/opencv-arm/include/opencv2
LIBS += /usr/local/arm/opencv-arm/lib/libopencv_highgui.so \
/usr/local/arm/opencv-arm/lib/libopencv_core.so \
/usr/local/arm/opencv-arm/lib/libopencv_imgproc.so \
/usr/local/arm/opencv-arm/lib/libopencv_imgcodecs.so \
/usr/local/arm/opencv-arm/lib/libopencv_videoio.so