在 x86_64 主机上为 ARM 交叉编译 opencv 2.4.11

Issues cross compile opencv 2.4.11 for ARM on x86_64 host

我一直在尝试为 ARM 目标系统交叉编译 opencv。我的项目依赖于使用 opencv 2.4.11.

目标系统是 Zynq Ultrascale+ FPGA 的四核 Cortex A53 上的 petalinux 2017.2 运行。

我的主机系统是 Ubuntu 16.04 x86_64.

我使用以下 toolchain.cmake 文件进行配置:

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)

我执行了以下步骤:

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

如您所见,构建过程失败并显示:错误:'asm' 中的未知寄存器名称 'st'。它似乎试图用 ARM 编译器

解释 x86_64 库
[ 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

构建过程中时不时会出现这样的警告,这表明设置的路径有问题:

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]

我不清楚为什么它采用主机路径而不是给定的目标根路径。

您的工具链文件不完整。 petalinux 有提供吗?

您需要添加指令,以便 make 在构建目标对象时不会尝试使用主机包含:

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)

您可能还需要指定目标系统根目录:

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