Ubuntu14.04 Opencv2.4.9交叉编译

         经过一个多星期苦逼中,终于把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编译器安装:

       将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

Ubuntu14.04 Opencv2.4.9交叉编译_第1张图片


   依赖库的安装编译主要包括:

     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

1, libz 的交叉编译:

./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

2, libjpeg的交叉编译:

./configure--host=arm-linux --prefix=$OPENCV_DEPEND --enable-shared --enable-static

make

makeinstall

3,  libpng 的交叉编译:

./configure --host=arm-linux--prefix=$OPENCV_DEPEND --enable-shared --enable-static

make

makeinstall

4,  yasm 的交叉编译:

CC=arm-linux-gcc ./configure--enable-shared --host=arm-linux --disable-asm --prefix=$OPENCV_DEPEND

make

makeinstall

5,  libx264 的交叉编译:

CC=arm-linux-gcc ./configure--enable-shared --host=arm-linux --disable-asm --prefix=$OPENCV_DEPEND

make

makeinstall

6,  libxvid 的交叉编译

cd build/generic

./configure --prefix=$OPENCV_DEPEND--host=arm-linux  --disable-assembly

make

makeinstall

7,  ffmpeg 的交叉编译:

./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时会出现如下错误:

Ubuntu14.04 Opencv2.4.9交叉编译_第2张图片


    Opencv交叉编译

     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


Ubuntu14.04 Opencv2.4.9交叉编译_第3张图片


默认安装目录为 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 =====================================
-- Version control: unknown
--
-- Platform:
-- Host: Linux 4.2.0-27-generic x86_64
-- Target: Linux arm
-- CMake: 2.8.12.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /usr/local/arm/4.3.2/bin/arm-none-linux-gnueabi-g++ (ver 4.3.2)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/local/arm/4.3.2/bin/arm-none-linux-gnueabi-gcc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- Precompiled headers: YES
--
-- OpenCV modules:
-- To be built: core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib stitching superres ts videostab
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: androidcamera dynamicuda java python viz
--
-- GUI:
-- QT: NO
-- GTK+ 2.x: NO
-- GThread : NO
-- GtkGlExt: NO
-- OpenGL support: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /usr/local/arm/opencv-depend/lib/libz.so (ver 1.2.8)
-- JPEG: /usr/local/arm/opencv-depend/lib/libjpeg.so (ver 90)
-- PNG: /usr/local/arm/opencv-depend/lib/libpng.so (ver 1.6.18)

-- TIFF: build (ver 42 - 4.0.2)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
--
-- Video I/O:
-- DC1394 1.x: NO
-- DC1394 2.x: NO
-- FFMPEG: YES
-- codec: YES (ver 54.35.0)
-- format: YES (ver 54.20.4)
-- util: YES (ver 52.3.0)
-- swscale: YES (ver 2.1.1)
-- gentoo-style: YES

-- GStreamer: NO
-- OpenNI: NO
-- OpenNI PrimeSensor Modules: NO
-- PvAPI: NO
-- GigEVisionSDK: NO
-- UniCap: NO
-- UniCap ucil: NO
-- V4L/V4L2: YES/YES
-- XIMEA: NO
-- Xine: NO
--
-- Other third-party libraries:
-- Use IPP: NO
-- Use Eigen: NO
-- Use TBB: NO
-- Use OpenMP: NO
-- Use GCD NO
-- Use Concurrency NO
-- Use C=: NO
-- Use Cuda: NO
-- Use OpenCL: YES
--
-- OpenCL:
-- Version: dynamic
-- Include path: /root/opencv-best/opencv-2.4.9/3rdparty/include/opencl/1.2
-- Use AMD FFT: NO
-- Use AMD BLAS: NO
--
-- Python:
-- Interpreter: /usr/bin/python2 (ver 2.7.6)
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java tests: NO
--
-- Documentation:
-- Build Documentation: NO
-- Sphinx: NO
-- PdfLaTeX compiler: NO
--
-- Tests and samples:
-- Tests: YES
-- Performance tests: YES
-- C/C++ Examples: NO
--
-- Install path: /usr/local/arm/opencv-install
--
-- cvconfig.h is in: /root/opencv-best/opencv-2.4.9/build
-- -----------------------------------------------------------------

最后要将 OPENCV_DEPEND下依赖库lib和OPENCV_INSTALL安装的库lib都拷贝到板子上lib目录下,Opencv即可在ARM板子上运行了….

特此感谢

OpenCV移植到ARM9

Ubuntu下OpenCV 2.4.3以及Qt 4.8.3的交叉编译







你可能感兴趣的:(嵌入式Linux)