Search code examples
opencvarmfpgazynqpetalinux

Issues cross compile opencv 2.4.11 for ARM on x86_64 host


I've been trying to cross compile opencv for an ARM target system. My project is dependent to use opencv 2.4.11.

The target system is a petalinux 2017.2 running on a quad-core Cortex A53 of a Zynq Ultrascale+ FPGA.

My host system is an Ubuntu 16.04 x86_64.

I used the following toolchain.cmake file for configuration:

set( CMAKE_SYSTEM_NAME Linux)
set( CMAKE_SYSTEM_PROCESSOR arm)
set( CMAKE_C_COMPILER /home/benjaminh/petalinux_2017_2/tools/linux-i386/aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set( CMAKE_CXX_COMPILER /home/benjaminh/petalinux_2017_2/tools/linux-i386/aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)
set( CMAKE_FIND_ROOT_PATH /home/benjaminh/embedded_development/ultrazed_repo/scripts/rootfs_part)

SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

I performed the following steps:

wget https://github.com/Itseez/opencv/archive/2.4.11.zip

unzip 2.4.11.zip
cd opencv-2.4.11
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../
make -j4

As you can see, the build process failed with: error: unknown register name 'st' in 'asm'. It seems that it attemps to interpret a x86_64 library with an ARM compiler

[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/bgfg_gaussmix.cpp.o
[ 44%] Building CXX object modules/photo/CMakeFiles/opencv_photo.dir/src/denoising.cpp.o
In file included from /home/benjaminh/petalinux_2017_2/tools/linux-i386/aarch64-linux-gnu/aarch64-linux-gnu/libc/usr/include/math.h:472:0,
                 from /home/benjaminh/petalinux_2017_2/tools/linux-i386/aarch64-linux-gnu/aarch64-linux-gnu/include/c++/6.2.1/cmath:45,
                 from /home/benjaminh/petalinux_2017_2/tools/linux-i386/aarch64-linux-gnu/aarch64-linux-gnu/include/c++/6.2.1/math.h:36,
                 from /home/benjaminh/embedded_development/opencv-2.4.11/modules/core/include/opencv2/core/types_c.h:94,
                 from /home/benjaminh/embedded_development/opencv-2.4.11/modules/core/include/opencv2/core/core.hpp:49,
                 from /home/benjaminh/embedded_development/opencv-2.4.11/modules/highgui/include/opencv2/highgui/highgui.hpp:46,
                 from /home/benjaminh/embedded_development/opencv-2.4.11/modules/highgui/src/precomp.hpp:47,
                 from /home/benjaminh/embedded_development/opencv-2.4.11/modules/highgui/src/cap_images.cpp:52:
/usr/include/x86_64-linux-gnu/bits/mathinline.h: In member function ‘virtual bool CvCapture_Images::setProperty(int, double)’:
/usr/include/x86_64-linux-gnu/bits/mathinline.h:889:3: error: unknown register name ‘st’ in ‘asm’
   __lrint_code;
   ^
/usr/include/x86_64-linux-gnu/bits/mathinline.h:889:3: error: unknown register name ‘st’ in ‘asm’
   __lrint_code;
   ^
modules/highgui/CMakeFiles/opencv_highgui.dir/build.make:86: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_images.cpp.o' failed
make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_images.cpp.o] Error 1
CMakeFiles/Makefile2:2275: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/all' failed
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/lkpyramid.cpp.o
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/video_init.cpp.o
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/bgfg_gmg.cpp.o
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/tvl1flow.cpp.o
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/bgfg_gaussmix2.cpp.o
[ 44%] Linking CXX shared library ../../lib/libopencv_photo.so
[ 44%] Built target opencv_photo
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/kalman.cpp.o
[ 44%] Building CXX object modules/video/CMakeFiles/opencv_video.dir/src/simpleflow.cpp.o
[ 44%] Linking CXX shared library ../../lib/libopencv_video.so
[ 44%] Built target opencv_video
Makefile:160

: recipe for target 'all' failed
    make: *** [all] Error 2

From time to time during the build process warnings like these occure, which indicates that there is something wrong with the set path:

cc1plus: warning: include location "/usr/include/glib-2.0" is unsafe for cross-compilation [-Wpoison-system-directories]
cc1plus: warning: include location "/usr/include/freetype2" is unsafe for cross-compilation [-Wpoison-system-directories]
cc1plus: warning: include location "/usr/include/x86_64-linux-gnu" is unsafe for cross-compilation [-Wpoison-system-directories]

For me it is not clear why it is taking the host path and not the given target root path.


Solution

  • Your toolchain file is incomplete. Does petalinux provide one?

    You need to add directives so that make will not attempt to use host includes when building target objects:

    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
    

    You may also need to specify the target sys root:

    set(CMAKE_SYSROOT /home/devel/petalinux-rootfs)