经过一个多星期苦逼中,终于把Opencv交叉编译通过了。废话不多说,把我过程分享出来,希望能够让你们少走些弯路。不同的linux系统、opencv版本、arm编译器,opencv交叉编译方法会有所不同。本文提供一个已验证可实行的方法:Ubuntu14.04+Opencv2.4.9+arm-linux-gcc-4.3.2+2.8.12。
在 opencv 交叉编译之前要先进行依赖库的交叉编译,然后才能进行 opencv 库的交叉编译(因为opencv 依赖于很多库函数,如果不编译会造成,编译后的opencv 无法使用)。这里首先给出OpenCV 交叉编译时各个库的依赖关系:
OpenCV
|--------zlib
|--------jpeg
|--------libpng
|--------zlib
|--------tiff
|--------zlib
|--------ffmpeg
|--------x264
|--------xvidcore
首先感谢这位博主 http://blog.chinaunix.net/uid-28669227-id-5024889.html 本人已验证其方法可行,但由于其采用opencv2.0版本过老及没有采用CMake-gui来交叉编译,本人最终选择了更新的opencv版本。
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
这几项与视频解码相关,不安装的话后面opencv make结果就会出现这样:
FFMPEG: NO
-- codec: NO
-- format: NO
-- util: NO
-- swscale: NO
-- gentoo-style: NO
将arm-linux-gcc-4.3.2.tar.gz在 /usr/local/arm/ 下解压,在 /root/.bashrc 添加环境变量
export PATH=$PATH:/usr/local/arm/4.3.2/bin arm编译器路径
export OPENCV_DEPEND=/usr/local/arm/opencv-depend opencv依赖库安装路径
export OPENCV_INSTALL=/usr/local/arm/opencv-install opencv安装路径
arm再按Tab键出现 arm-none-linux-gnueabi-gcc-4.3.2
zlib-1.2.8
jpeg-9a
libpng-1.6.18
yasm-1.3.0
x264-snapshot-20151030-2245
xvidcore-1.3.3
ffmpeg-2.8.1
./configure --prefix=$OPENCV_DEPEND –shared
修改 Makefile
CC=arm-linux-gcc
AR=arm-linux-ar
RANLIB=arm-linux-ranlib
LDSHARED=arm-linux-gcc -shared-Wl,-soname,libz.so.1,--version-script,zlib.map
make
make install
./configure--host=arm-linux --prefix=$OPENCV_DEPEND --enable-shared --enable-static
make
makeinstall
./configure --host=arm-linux--prefix=$OPENCV_DEPEND --enable-shared --enable-static
make
makeinstall
CC=arm-linux-gcc ./configure--enable-shared --host=arm-linux --disable-asm --prefix=$OPENCV_DEPEND
make
makeinstall
CC=arm-linux-gcc ./configure--enable-shared --host=arm-linux --disable-asm --prefix=$OPENCV_DEPEND
make
makeinstall
cd build/generic
./configure --prefix=$OPENCV_DEPEND--host=arm-linux --disable-assembly
make
makeinstall
./configure --prefix=$OPENCV_DEPEND--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=-L$OPENCV_DEPEND/lib --extra-cflags=-I$OPENCV_DEPEND/include
make
makeinstall
到此所有依赖库编译安装完成
Cmake安装 sudo apt-get install cmake cmake-qt-gui
需要把 /usr/local/arm/opencv-depend 目录下lib和include所有的文件分别拷贝到 /usr/local/arm/4.3.2/arm-non-linux-gnueabi lib和include下,不然后面opencv make时会出现如下错误:
cd opencv-2.4.9
mkdir build
cd build
vim toolchain.cmake
###########user defined#############
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILERarm-none-linux-gnueabi-gcc )
set( CMAKE_CXX_COMPILERarm-none-linux-gnueabi-g++ )
###########user defined#############
set( CMAKE_FIND_ROOT_PATH"/usr/local/arm/opencv-depend" )
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAMNEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
######################################
保存然后
cmake-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../
紧接着 cmake-gui
默认安装目录为 opencv-2.4.9/build/install ,改为 /usr/local/arm/opencv-install
修改编译选项
CMAKE_INSTALL_PREFIX /usr/local/arm/opencv-install
去掉 WITH_CUDA
去掉 WITH_GTK
去掉 WITH_1394
去掉 WITH_GSTREAMER
去掉 WITH_LIBV4L (因为后面有另外一个WITH_V4L)
去掉 WITH_TIFF
去掉 BUILD_OPENEXR
去掉 WITH_OPENEXR
去掉 BUILD_opencv_ocl
去掉 WITH_OPENCL
根据自己的需要选择编译的选项,有的选项需要安装相应的依赖的库,否则编译会出错
完成后 Generate
修改 CMakeCache.txt, CMAKE_EXE_LINKER_FLAGS原来为空,加上 -lpthread -lrt
make
makeinstall
下面是我生成的make,可以参考下,有几个红色关键点
-- General configuration for OpenCV 2.4.9 =====================================最后要将 OPENCV_DEPEND下依赖库lib和OPENCV_INSTALL安装的库lib都拷贝到板子上lib目录下,Opencv即可在ARM板子上运行了….
特此感谢
OpenCV移植到ARM9Ubuntu下OpenCV 2.4.3以及Qt 4.8.3的交叉编译