在Ubuntu上为树莓派交叉编译OpenCV项目

6
我已经奋斗了两天,但仍无法让它运行。
迄今为止我做的事情:

我的问题是什么:

  • OpenCV程序不能使用交叉编译器编译
  • |2|fatal error: opencv2/highgui/highgui.hpp: No such file or directory|
    

  • 我认为我还需要在Ubuntu上进行OpenCV的交叉编译。我也有一个工具链来进行交叉编译。
  • # this one is important
    SET(CMAKE_SYSTEM_NAME Linux)
    #this one not so much
    SET(CMAKE_SYSTEM_VERSION 1)
    
    # specify the cross compiler
    SET(CMAKE_C_COMPILER
    /home/christian/Programming/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)
    
    SET(CMAKE_CXX_COMPILER
    /home/christian/Programming/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)
    
    # where is the target environment
    SET(CMAKE_FIND_ROOT_PATH
    /home/christian/Programming/x-tools/arm-unknown-linux-gnueabi)
    
    # search for programs in the build host directories
    SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    # for libraries and headers in the target directories
    SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    

    使用这个工具链,我尝试使用以下命令交叉编译OpenCV:
    cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-RaspberryPi.cmake ../
    

    在执行make命令后,编译进程进行到27%时出现了以下错误。
    [ 27%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o
    In file included from /home/christian/Downloads/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp:60:0,
                     from /home/christian/Downloads/opencv-2.4.8/modules/highgui/src/cap_ffmpeg.cpp:45:
    /home/christian/Downloads/opencv-2.4.8/modules/highgui/src/ffmpeg_codecs.hpp:81:36: fatal error: libavformat/avformat.h: No such file or directory
       #include <libavformat/avformat.h>
                                        ^
    compilation terminated.
    make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.cpp.o] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2
    

    我尝试将在树莓派上编译的OpenCV头文件和库复制到我的Ubuntu电脑上并进行链接。但是在编译时出现了许多错误。
    ||=== RPi_Cross_CV, Debug ===|
    ||warning: libz.so.1, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_core.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libjpeg.so.8, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libpng12.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libtiff.so.4, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libjasper.so.1, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgtk-x11-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgdk-x11-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libatk-1.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgio-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libpangoft2-1.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libpangocairo-1.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgdk_pixbuf-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libcairo.so.2, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libpango-1.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libfreetype.so.6, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libfontconfig.so.1, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgobject-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libglib-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgthread-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgstbase-0.10.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgstreamer-0.10.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libgmodule-2.0.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libxml2.so.2, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libdc1394.so.22, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libv4l1.so.0, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libavcodec.so.53, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libavformat.so.53, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libavutil.so.51, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ||warning: libswscale.so.2, needed by ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8, not found (try using -rpath or -rpath-link)|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `TIFFSetWarningHandler@LIBTIFF_3.9'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `png_read_info@PNG12_0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_time_val_add'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jas_image_writecmpt'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_widget_queue_draw'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `av_guess_codec@LIBAVFORMAT_53'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `av_rescale_q@LIBAVUTIL_51'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jpeg_std_error@LIBJPEG_8.0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jas_cmprof_destroy'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `png_get_tRNS@PNG12_0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_return_if_fail_warning'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_camera_enumerate'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_capture_stop'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `compress'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_get_image_size_from_video_mode'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_timeout_add'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `png_set_packing@PNG12_0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `v4l2_munmap'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_feature_whitebalance_get_value'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_type_new'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jas_image_destroy'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `avcodec_close@LIBAVCODEC_53'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `sws_getCachedContext@LIBSWSCALE_2'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_core.so.2.4.8||undefined reference to `gzeof'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_style_attach'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_widget_get_type'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_window_resize'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_cond_broadcast'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_video_get_supported_framerates'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jpeg_read_header@LIBJPEG_8.0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `avio_open@LIBAVFORMAT_53'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `TIFFReadEncodedTile@LIBTIFF_3.9'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `uncompress'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_type_check_instance_cast'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_usleep'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `avcodec_find_decoder@LIBAVCODEC_53'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `g_get_current_time'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jpeg_set_defaults@LIBJPEG_8.0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_video_get_mode'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jpeg_start_compress@LIBJPEG_8.0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `av_free_packet@LIBAVCODEC_53'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_window_set_geometry_hints'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_signal_connect_full'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gdk_draw_rgb_image'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_widget_queue_resize'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `dc1394_video_set_mode'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jas_stream_close'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jpeg_set_quality@LIBJPEG_8.0'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `jas_image_readcmpt'|
    ../../../../Programming/x-tools/usr-local/lib/libopencv_highgui.so.2.4.8||undefined reference to `gtk_disable_setlocale'|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build finished: 50 errors, 29 warnings ===|
    

    所以我的问题是:

    我如何在Ubuntu上获取一个交叉编译器来与OpenCV一起使用?我只想在Ubuntu上编译,然后将程序复制到Raspberry Pi上运行。

    非常感谢。

    2个回答

    6

    我知道这是一个老问题,但是为了记录,我能够从Ubuntu交叉编译我的OpenCV项目。

    在我的情况下,我在Ubuntu 12.0.4上使用了预构建的树莓派工具链github:https://github.com/raspberrypi/tools。我采取的方法是将目标树莓派系统上安装了OpenCV和PiCamera库的本地库复制到Ubuntu实例中,并像您在帖子中提到的那样使用CMAKE_TOOLCHAIN_FILE支持。

    但是,我必须克服某些问题,在这篇文章中我会涵盖这些问题:https://solderspot.wordpress.com/2016/02/04/cross-compiling-for-raspberry-pi-part-ii

    关键点如下:

    1. CMAKE_FIND_ROOT_PATH没有效果。我必须使用CMAKE_SYSROOT。

    2. 在我的CMakeLists.txt文件中,我仍然必须显式地将任何路径前缀添加到我已复制本地文件系统的文件夹路径。

    3. 我还需要在CMAKE_TOOLCHAIN_FILE中显式指定动态库链接路径,因为工具链无法正确解析/usr/etc/ld.so.conf文件。

    我目前唯一无法解决的问题是关于Raspbian Jessie版本的问题,因为OpenCV使用gcc/g++ 4.9功能,而该功能不受工具链中gcc/g++ 4.8版本支持。看起来工具链将在不久的将来增强以支持4.9。

    我的最终CMAKE_TOOLCHAIN_FILE如下:

    SET(CMAKE_SYSTEM_NAME Linux)
    SET(CMAKE_SYSTEM_VERSION 1)
    
    SET(DEVROOT $ENV{HOME}/pidev)
    SET(PIROOT ${DEVROOT}/piroot)
    SET(PITOOLS ${DEVROOT}/pitools)
    
    SET(TOOLROOT ${PITOOLS}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64)
    
    # specify the cross compiler
    SET(CMAKE_C_COMPILER   ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
    SET(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)
    
    SET(FLAGS "-Wl,-rpath-link,${PIROOT}/opt/vc/lib -Wl,-rpath-link,${PIROOT}/lib/arm-linux-gnueabihf -Wl,-rpath-link,${PIROOT}/usr/lib/arm-linux-gnueabihf -Wl,-rpath-link,${PIROOT}/usr/local/lib")
    
    UNSET(CMAKE_C_FLAGS CACHE)
    UNSET(CMAKE_CXX_FLAGS CACHE)
    
    SET(CMAKE_CXX_FLAGS ${FLAGS} CACHE STRING "" FORCE)
    SET(CMAKE_C_FLAGS ${FLAGS} CACHE STRING "" FORCE)
    
    SET(CMAKE_SYSROOT ${PIROOT})
    SET(CMAKE_FIND_ROOT_PATH ${PIROOT})
    
    
    # search for programs in the build host directories
    SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    # for libraries and headers in the target directories
    SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    

    我在我的主目录下有一个pidev文件夹,其中的piroot文件夹拷贝了来自我的树莓派主机的/usr /opt /etc和/lib目录,pitools是我从上面提到的GitHub工具链的克隆版本。

    我的项目的CMakeLists.txt文件如下:

    cmake_minimum_required(VERSION 2.8)
    project( PiCamCVTest )
    SET(COMPILE_DEFINITIONS -Werror)
    
    include_directories(SYSTEM ${PIROOT}/opt/vc/include ${PIROOT}/opt/vc/include/interface/vcos/pthreads ${PIROOT}/opt/vc/include/interface/vmcs_host/linux )
    link_directories( ${PIROOT}/opt/vc/lib )
    add_executable(PiCamCVTest main.cpp camera.cpp cameracontrol.cpp graphics.cpp)
    
    target_link_libraries(PiCamCVTest libmmal_core.so libmmal_util.so libmmal_vc_client.so libvcos.so librt.so libbcm_host.so GLESv2 EGL libopencv_core.so libopencv_imgproc.so)
    

    希望这可以帮到您。

    你从哪里获取了树莓派上的opencv库副本?我在我的树莓派上运行了“sudo apt-get install libopencv-dev”并成功安装了它。但是我找不到任何libopencv*.so文件,无论是在“/usr/lib”,“/lib”还是“/usr/local/lib”中都没有。我只在“/usr/include/opencv”和“/usr/include/opencv2”中找到头文件。然而,我非常确定我可以在树莓派上本地编译opencv,只是找不到库的位置。 - Alston

    2

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接