简体   繁体   English

在Ubuntu上为Raspberry Pi交叉编译opencv项目

[英]Cross compile opencv project on Ubuntu for Raspberry Pi

I'm struggling with this for 2 days now and don't get it to work. 我现在正在努力奋斗2天并且不能让它发挥作用。

What I've done so far: 到目前为止我做了什么:


What are my problems: 我有什么问题:

  • OpenCV programs don't compile with the cross compiler OpenCV程序不使用交叉编译器进行编译
  • |2|fatal error: opencv2/highgui/highgui.hpp: No such file or directory|
    

  • I think I need to cross compile OpenCV as well on ubuntu. 我想我需要在ubuntu上交叉编译OpenCV。 From my cross compiler I also have a toolchain 从我的交叉编译器我也有一个工具链
  • # 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)
    

    with this toolchain I tried to cross compile OpenCV with this command: 使用此工具链,我尝试使用此命令交叉编译OpenCV:

    cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-RaspberryPi.cmake ../
    

    After the make command it compiles until 27%. 在make命令之后,它编译为27%。 There it stops with this error 它停止了这个错误

    [ 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
    

  • I tried to copy the OpenCV headers and libs compiled on the Raspberry Pi to my ubuntu computer and link them. 我试图将在Raspberry Pi上编译的OpenCV头文件和库复制到我的ubuntu计算机并链接它们。 I get loads of errors while compiling 编译时我遇到了很多错误
  • ||=== 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 ===|
    

    So my question is: 所以我的问题是:

    How can I get a crosscompiler on ubuntu to work with opencv? 如何在ubuntu上使用交叉编译器来使用opencv? I just want to compile on ubuntu, copy the program on the Pi and run it there. 我只想在ubuntu上编译,在Pi上复制程序并在那里运行它。

    Thanks a lot 非常感谢

    I realize this is an old question but for the record I was able to cross compile my OpenCV project from Ubuntu. 我意识到这是一个老问题但是为了记录我能够从Ubuntu交叉编译我的OpenCV项目。

    In my case I used the the pre-built raspberry pi toolchain on github: https://github.com/raspberrypi/tools on Ubuntu 12.0.4. 在我的例子中,我在github上使用了预构建的raspberry pi工具链: https//github.com/raspberrypi/tools在Ubuntu 12.0.4上。 I took the approach of copying the native libraries (with OpenCV and PiCamera libraries installed) from the target Raspberry Pi system to the Ubuntu instance and using the CMAKE_TOOLCHAIN_FILE support like you mention in your post. 我采用了将原生库(安装了OpenCV和PiCamera库)从目标Raspberry Pi系统复制到Ubuntu实例并使用您在帖子中提到的CMAKE_TOOLCHAIN_FILE支持的方法。

    However, there were certain issues I had to overcome, which I cover in this post: https://solderspot.wordpress.com/2016/02/04/cross-compiling-for-raspberry-pi-part-ii 但是,我必须克服一些问题,我将在这篇文章中介绍: https//solderspot.wordpress.com/2016/02/04/cross-compiling-for-raspberry-pi-part-ii

    The key points were: 关键点是:

    1. CMAKE_FIND_ROOT_PATH had not effect. CMAKE_FIND_ROOT_PATH没有效果。 I had to use CMAKE_SYSROOT. 我不得不使用CMAKE_SYSROOT。

    2. In my CMakeLists.txt file I still had to explicitly prepend any paths with the path to the folder where I had copied over the native file system. 在我的CMakeLists.txt文件中,我仍然必须使用我在本机文件系统上复制的文件夹的路径显式地添加任何路径。

    3. I also needed to explicitly specify the dynamic library link paths from within CMAKE_TOOLCHAIN_FILE as the toolchain does not correctly parser the /usr/etc/ld.so.conf file. 我还需要在CMAKE_TOOLCHAIN_FILE中明确指定动态库链接路径,因为工具链无法正确解析/usr/etc/ld.so.conf文件。

    The only current issue I could not solve yet was to do with the Jessie release of Raspbian, as OpenCV uses an gcc/g++ 4.9 feature that is not supported by the gcc/g++ 4.8 version of the toolchain. 我无法解决的唯一当前问题是与Jessie发布的Raspbian有关,因为OpenCV使用了gcc / g ++ 4.9版本的工具链不支持的gcc / g ++ 4.9功能。 It looks like the toolchain will be augmented to support 4.9 in the near future. 看起来工具链将在不久的将来得到增强以支持4.9。

    My final CMAKE_TOOLCHAIN_FILE was: 我的最终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)
    

    Where I had a pidev folder in my home directory and under that piroot had a copy of /usr /opt /etc and /lib from my pi host, and pitools is a clone of the toolchain from GitHub I reference above. 我在我的主目录中有一个pidev文件夹,并且在piroot下面有一个来自我的pi主机的/ usr / opt / etc和/ lib的副本,而pitools是GitHub的工具链的克隆,我在上面引用。

    My CMakeLists.txt file for my project was: 我的项目的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)
    

    Hope this helps. 希望这可以帮助。

    You can cross compile Qt (c++) with OpenCV to the raspberry pi following this link: http://aplacetogeek.wordpress.com/qt-opencv-v4l-cross-compiling-raspberry-pi-beagle-bone/ 您可以通过以下链接将Qt(c ++)与OpenCV交叉编译到raspberry pi: http//aplacetogeek.wordpress.com/qt-opencv-v4l-cross-compiling-raspberry-pi-beagle-bone/

    This link should give you all the needed information on how to make your system able to cross compile for the raspberry pi. 此链接应该为您提供有关如何使您的系统能够为raspberry pi交叉编译所需的所有信息。 When you've set up everything the way the link says, I think you can also use the opencv library's to cross compile c++ without Qt if you prefer. 当您按照链接所说的方式设置所有内容时,我认为如果您愿意,我也可以使用opencv库来交叉编译c ++而不使用Qt。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM