1. 程式人生 > >Opencv2.4.9交叉編譯

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

      安裝依賴項

        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依賴庫安裝路徑

       exportOPENCV_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

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時會出現如下錯誤:


    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


預設安裝目錄為 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板子上運行了….

特此感謝