BB Black 入门基础之OpenCV的交叉编译

2019-07-15 15:27发布

BB Black 入门基础之OpenCV的交叉编译


经过几天的不断尝试,终于顺利完成了OpenCV的交叉编译。哎,本来这就是一编译过程,硬是用了一周的时间。
现在把我的实现步骤贴出来,希望大家能少走弯路。
参考文献:
docs.opencv.org/doc/tutorials/introduction/crosscompilation/arm_crosscompile_with_cmake.html
blog.csdn.net/msq19895070/article/details/24477575
m.blog.csdn.net/blog/guo8113/17794395
blog.csdn.net/shuxiao9058/article/details/7525376


虚拟机环境: Ubuntu 12.04 LTS
编译器: arm-linux-gnueabihf-
BBB系统: Ubuntu 12.04 armhf
OpenCV: 2.4.3
Cmake: 2.8.7
OpenCV交叉编译时用到的几个依赖库:http://download.**/detail/lonerzf/538159

已编译的BB Black的OpenCV库:
http://download.**/detail/lonerzf/538163


一、安装依赖项
  • sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

[color=rgb(51, 102, 153) !important]复制代码

这几项与视频解码相关,不放心的话,可以先不装,之后再装也一样。只不过不装这几项呢,make配置的结果会出现:
FFMPEG:                   NO
codec:                      NO
format:                     NO
util:                           NO
swscale:                    NO


结果就这么直观。。。

二、Cmake的安装
  • sudo apt-get install cmake cmake-qt-gui

[color=rgb(51, 102, 153) !important]复制代码

装好之后目前版本是2.8.7的。当然,也可以从官网下载源码make。只是,别装2.8.12这个最新版,个别地方会报错。


三、相关库编译安装
OpenCV交叉编译时各个库之间依赖关系如下:

OpenCV
        |--------zlib               
        |--------jpeg
        |--------libpng
                    |--------zlib
        |--------tiff
                    |--------zlib
        |--------ffmpeg
                    |--------x264
                    |--------xvidcore


考虑到个别库不太容易找到,这几个库的源码已经打包上传,可以在论坛的下载中心下载。
内容如下:



为了简化操作,咱们这里用脚本的方式操作。
首先解压OpenCV到指定目录。我这里是:
/opt/lon/OpenCV-2.4.3/

添加环境变量


export COMPILE_PREFIX=arm-linux-gnueabihf-
export INSTALL_PREFIX=`pwd`/crosscompile_root
export DL_FOLDER=`pwd`/dl
export BUILD_FOLDER=`pwd`/buildpool
export CC=${COMPILE_PREFIX}gcc


然后新建crosscompile_root dl build buildpool 四个文件夹。如下图所示:


接着将下载好的各压缩文件都复制到dl文件夹下。
同时新建一个mycfg.sh的脚本文件,并为其添加可执行权限
  • sudo chmod a+x  mycfg.sh

[color=rgb(51, 102, 153) !important]复制代码




接着就是打开mycfg.sh并写入以下内容(论坛的代码功能不太好,就直接以文本形式粘过来了):


#!/bin/bash
echo "Hello, zlib!"
pushd $BUILD_FOLDER
unzip $DL_FOLDER/zlib127.zip
pushd zlib-1.2.7
./configure --prefix=$INSTALL_PREFIX
make
make install
popd
echo "zlib end"

echo "Hello, jpeg!"
tar xf $DL_FOLDER/jpegsrc.v8d.tar.gz
pushd jpeg-8d
./configure --host=arm-linux-gnueabihf --prefix=$INSTALL_PREFIX
mkdir -p $INSTALL_PREFIX/bin
mkdir -p $INSTALL_PREFIX/man/man1
make
make install
popd
echo "jpeg end"

echo "Hello, png!"
tar xf $DL_FOLDER/libpng-1.5.14.tar.gz
pushd libpng-1.5.14
./configure --host=arm-linux-gnueabihf --prefix=$INSTALL_PREFIX CPPFLAGS=-I$INSTALL_PREFIX/include LDFLAGS=-L$INSTALL_PREFIX/lib
make
make install
popd
echo "png end"

echo "Hello, x264!"
tar xjf $DL_FOLDER/x264-snapshot-20120528-2245-stable.tar.bz2
pushd x264-snapshot-20120528-2245-stable
./configure --host=arm-linux --cross-prefix=arm-linux-gnueabihf- --enable-shared --prefix=$INSTALL_PREFIX
make
make install
popd
echo "x264 end"

echo "Hello, xvidcore!"
tar xf $DL_FOLDER/xvidcore-1.3.2.tar.gz
pushd xvidcore/build/generic
./configure --prefix=$INSTALL_PREFIX --host=arm-linux-gnueabihf  --disable-assembly
make
make install
popd
echo "xvidcore end"

echo "Hello, tiff!"
tar xf $DL_FOLDER/tiff-4.0.3.tar.gz
pushd tiff-4.0.3
./configure --prefix=$INSTALL_PREFIX --host=arm-linux-gnueabihf --enable-shared CPPFLAGS=-I$INSTALL_PREFIX/include LDFLAGS=-L$INSTALL_PREFIX/lib
make
make install
popd
echo "tiff end"

echo "Hello, ffmpeg!"
tar -xjvf $DL_FOLDER/ffmpeg-0.10.3.tar.bz2
pushd ffmpeg-0.10.3
./configure --prefix=$INSTALL_PREFIX --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --cpu=armv7-a --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gnueabihf-gcc --enable-swscale --extra-cflags=-I$INSTALL_PREFIX/include --extra-ldflags=-L$INSTALL_PREFIX/lib --disable-asm
make
make install
popd
echo "ffmpeg end"


完了以后呢,不用多想,执行脚本吧。
  • ./mycfg.sh

[color=rgb(51, 102, 153) !important]复制代码

用了脚本之后这个过程也算半自动了吧,

上一步完成了之后呢,进入上文新建的build文件夹, 创建一个toolchain.cmake的文件,并填入如下内容:

set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-linux-gnueabihf-gcc )
set( CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++ )
###########user defined#############
set( CMAKE_FIND_ROOT_PATH "/opt/lon/OpenCV-2.4.3/crosscompile_root" )
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吧。
  • cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../

[color=rgb(51, 102, 153) !important]复制代码

然后再运行如下指令
  • cmake-gui

[color=rgb(51, 102, 153) !important]复制代码

减少一些没有用的项,如gtk、cude、1394等。其中CUDE,当时就困扰了我好一会儿呢。看人家怎么解释的:
"CUDA_TOOLKIT_ROOT_DIR not found or specified", CUDA is a parallel computing platform and programming model that enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU). It is only developed for NVdia GPU. However, it seems that not all PC will be equipped with that kind of GPU, theoretically, it is not necessary to install it and we make the option WITH_CUDA OFF. Also the opencv only support CUDA 4.0 now from the post bellow.


确认之后Config 并 General生成配置信息。
下面附上我的cmake配置信息:
Detected version of GNU GCC: 113 (113)
checking for module 'gstreamer-base-0.10'
  package 'gstreamer-base-0.10' not found
checking for module 'libv4l1'
  package 'libv4l1' not found
Looking for linux/videodev.h
Looking for linux/videodev.h - not found
Looking for linux/videodev2.h
Looking for linux/videodev2.h - found
Looking for libavformat/avformat.h
Looking for libavformat/avformat.h - not found
Looking for ffmpeg/avformat.h
Looking for ffmpeg/avformat.h - not found
Could NOT find PythonLibs (missing:  PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) (Required is at least version "2.7.3")

General configuration for OpenCV 2.4.3 =====================================

  Platform:
    Host:                        Linux 3.2.0-61-generic-pae i686
    Target:                      Linux arm
    CMake:                       2.8.7
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /opt/lon/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-g++  (ver 1.13.1)
    C++ flags (Release):         -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG -ggdb3
    C Compiler:                  /opt/lon/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/arm-linux-gnueabihf-gcc
    C flags (Release):           -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 -Wno-narrowing -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -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 -Wno-narrowing -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG -ggdb3
    Linker flags (Release):      
    Linker flags (Debug):        
    Precompiled headers:         YES

  OpenCV modules:
    To be built:                 core imgproc flann highgui features2d calib3d ml video objdetect contrib nonfree photo legacy gpu stitching ts videostab
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 androidcamera java ocl python

  GUI:
    QT 4.x:                      NO
    GTK+ 2.x:                    NO
    GThread :                    NO
    GtkGlExt:                    NO
    OpenGL support:              NO

  Media I/O:
    ZLib:                        /opt/lon/OpenCV-2.4.3/crosscompile_root/lib/libz.so (ver 1.2.7)
    JPEG:                        /opt/lon/OpenCV-2.4.3/crosscompile_root/lib/libjpeg.so (ver 80)
    PNG:                         /opt/lon/OpenCV-2.4.3/crosscompile_root/lib/libpng.so (ver 1.5.14)
    TIFF:                        /opt/lon/OpenCV-2.4.3/crosscompile_root/lib/libtiff.so (ver 42 - 4.0.3)
    JPEG 2000:                   NO
    OpenEXR:                     build (ver 1.7.1)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES
      codec:                     YES (ver 53.35.0)
      format:                    YES (ver 53.21.1)
      util:                      YES (ver 51.22.2)
      swscale:                   YES (ver 2.1.0)
      gentoo-style:              YES
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/YES
    XIMEA:                       NO
    Xine:                        NO

  Other third-party libraries:
    Use TBB:                     NO
    Use Cuda:                    NO
    Use OpenCL:                  NO
    Use Eigen:                   NO

  Python:
    Interpreter:                 /usr/bin/python (ver 2.7.3)

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    Examples:                    NO

  Install path:                  /opt/lon/OpenCV-2.4.3/build/install

  cvconfig.h is in:              /opt/lon/OpenCV-2.4.3/build
-----------------------------------------------------------------

Configuring done


当然,更细节的内容你也可以在CMakeCache.txt文件中看到。
哦,差点忘了,还要在CMakeCache.txt文件中添加点东西:


(1)
CMAKE_CXX_FLAGS:STRING=


这一行后面添加
  • -mfpu=neon -I/opt/lon/OpenCV-2.4.3/crosscompile_root/include -L/opt/lon/OpenCV-2.4.3/crosscompile_root/lib -L/opt/lon/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux,-Wl,-rpath,/lib

[color=rgb(51, 102, 153) !important]复制代码


(2)
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=


这一行后面添加
  • -lpthread –lrt

[color=rgb(51, 102, 153) !important]复制代码




好。下面就是make & make install 完成操作了。


至此,应该就能完成OpenCV的交叉编译了。然后将
/opt/lon/OpenCV-2.4.3/build/install
/opt/lon/OpenCV-2.4.3/crosscompile_root
两个文件夹下的内容合并,放到咱们的BB Black中,应该就能工作了。

如果编译失败,可考虑重装虚拟机。并且避免安装其他东西。这是阻碍了我很长时间的一个重要原因。希望能乡亲们带来一点点的提醒。  


0条回答

一周热门 更多>