title: Cross compile opencv project on Ubuntu for Raspberry Pi
body: <p>I'm struggling with this for 2 days now and don't get it to work.</p>
  
  <p><strong>What I've done so far:</strong></p>
  
  <ul>
  <li><p>built opencv 2.4.8 from source on Ubuntu 12.04 LTS
  --> OpenCV projects compile and run on ubuntu</p></li>
  <li><p>built opencv 2.4.8 from source on the Raspberry Pi Raspian
  --> OpenCV projects compile and run on the Raspberry Pi</p></li>
  <li><p>built a cross compiler for the Raspberry Pi with crosstools-ng (<a href="http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/" rel="noreferrer">http://www.bootc.net/archives/2012/05/26/how-to-build-a-cross-compiler-for-your-raspberry-pi/</a>)
  --> c++ programs built on Ubuntu with the crosscompiler run on the Raspberry Pi</p></li>
  </ul>
  
  <hr>
  
  <p><strong>What are my problems:</strong></p>
  
  <p>
  <li> OpenCV programs don't compile with the cross compiler </li></p>
  
  <pre><code>|2|fatal error: opencv2/highgui/highgui.hpp: No such file or directory|
  </code></pre>
  
  <p><li> I think I need to cross compile OpenCV as well on ubuntu. From my cross compiler I also have a toolchain</li>  </p>
  
  <pre><code># 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)
  </code></pre>
  
  <p>with this toolchain I tried to cross compile OpenCV with this command:</p>
  
  <pre><code>cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-RaspberryPi.cmake ../
  </code></pre>
  
  <p>After the make command it compiles until 27%. There it stops with this error</p>
  
  <pre><code>[ 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 &lt;libavformat/avformat.h&gt;
                                      ^
  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
  </code></pre>
  
  <p><li> I tried to copy the OpenCV headers and libs compiled on the Raspberry Pi to my ubuntu computer and link them. I get loads of errors while compiling</li></p>
  
  <pre><code>||=== 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 ===|
  </code></pre>
  
  <hr>
  
  <p><strong>So my question is:</strong></p>
  
  <p>How can I get a crosscompiler on ubuntu to work with opencv? I just want to compile on ubuntu, copy the program on the Pi and run it there.</p>
  
  <p>Thanks a lot</p>