Hi3516编译 OpenCV

为Hi35xx处理器编译 OpenCV 时会遇到 NEON 无法启用的问题,报错信息如下:

CMake Error at cmake/OpenCVCompilerOptimizations.cmake:535 (message):
  Required baseline optimization is not supported: NEON
  (CPU_BASELINE_REQUIRE=;VFPV3;NEON)
Call Stack (most recent call first):
  cmake/OpenCVCompilerOptions.cmake:292 (include)
  CMakeLists.txt:594 (include)

cpu 支持的特性仅检测到了 VFV3而没有 NEON。

--   CPU/HW features:
--     Baseline:                    VFPV3
--       requested:                 DETECT
--       required:                  VFPV3 NEON

报错出现在 OpenCVCompilerOptimizations.cmake 中, OpenCVUtils.cmake 会对 cpu 特性进行检查。即使在跳过检查强制启用 NEON 也不行。

几经测试发现,编译时添加-mfloat-abi=softfp选项可解决该问题。

VFP 是 ARM 的“矢量浮点”单元(Floating Point Unit),执行硬件加速的浮点计算。由于历史原因并且为了与 ARM RVCT 套件匹配,GCC 的 FPU 和 ABI 选项并非完全正交。-mfloat-abi =选项控制 ABI 以及是否可以使用浮点指令。可用的选项有:

  • soft:全软浮点,编译器不会生成真实的FPU指令,并且忽略-mfpu =。FPU 操作由编译器模拟生成函数调用以在整数寄存器中传递 FP 参数(float, double)(一个用于浮点,一对用于双精度寄存器)。
  • softfp:使用软浮点 ABI 的硬件浮点。编译器可以根据选择的FPU类型(-mfpu =)确定何时以及是否生成模拟的或实际的 FPU 指令。
  • hard:完整的硬件浮点,FP 参数直接在 FPU 寄存器中传递。如果没有在-mfpu=中定义的FPU(或该 FPU的超集),则无法运行。

build.sh

编译脚本如下:

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
BuildDir=arm_build
ToolChain=/path/to/arm-himix200-linux
if [ ! -d "$BuildDir" ]; then
  # Take action if $BuildDir doesn‘t exists. 
  echo "create ${BuildDir}..."
  mkdir -p ${BuildDir}
fi
cd ${BuildDir}

echo "building OpenCV3"

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D BUILD_SHARED_LIBS=ON \
  -D CMAKE_FIND_ROOT_PATH=${ToolChain}/target\
  -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake\
  -D CMAKE_C_COMPILER=${ToolChain}/bin/arm-himix200-linux-gcc-6.3.0\
  -D CMAKE_CXX_COMPILER=${ToolChain}/bin/arm-himix200-linux-g++\
  -D CMAKE_CXX_FLAGS="-mfloat-abi=softfp"\
  -D CMAKE_C_FLAGS="-mfloat-abi=softfp"\
  -D CMAKE_INSTALL_PREFIX=${BuildDir} \
  -D WITH_TBB=ON \
  -D WITH_EIGEN=ON \
  -D BUILD_ZLIB=ON \
  -D BUILD_TIFF=ON \
  -D BUILD_JASPER=ON \
  -D BUILD_JPEG=ON \
  -D BUILD_PNG=ON \
  -D ENABLE_NEON=ON \
  -D ENABLE_VFPV3=ON \
  -D WITH_LIBV4L=ON \
  -D BUILD_opencv_python=OFF\
  -D ENABLE_PRECOMPILED_HEADERS=OFF \
  -D BUILD_EXAMPLES=OFF \
  -D BUILD_TESTS=OFF \
  -D BUILD_PERF_TESTS=OFF \
  -D BUILD_WITH_DEBUG_INFO=OFF \
  -D BUILD_DOCS=OFF \
  -D WITH_OPENCL=OFF \
  ../
make -j$(nproc)

参考资料:

  • Ccache (简体中文)
  • 通过 ccache 改善协同构建时间
  • cmake cross-compile for andrid meeting “Compiler doesn’t support baseline optimization flags”
  • ARM Cortex-A 处理器和 GCC 命令行
  • 在编译器中使能了neon(-mfpu=neon)之后,还能同时使用fpu计算64bit浮点吗?
  • HI3516的编译参数-mcpu=cortex-a7、-mfloat-abi=softfp和-mfpu=neon-vfpv4
  • ‘std::exception’ error when cross-compile OpenCV 4
  • Upgrading to G++ 4.8 - exception_ptr.h Does not support exception propagation
  • G++ Link-time optimisation on Mac - compiler/linker bug?
  • cpu: cortex-m: compiling vectors.c with LTO triggers compiler bug #5775
  • 移植opencv3.1.0到hi3516a
  • 海思hi3516移植OpenCV2.4.9
  • cmake使用教程(十)-关于file
  • NEON™ Support in Compilation Tools
  • [dpdk-dev,v3,2/4] arm: detect NEON cpu feature by checking __ARM_NEON
  • ARM crypto instructions and __ARM_FEATURE_CRYPTO macro
  • Optimizing opencv 2.4 and 3.1 in Raspberry Pi 3
  • What is carotene_o4t and why does it cause issues with compiling a program using opencv with Qt on Android?
  • Simple C++ Symbol Visibility Demo
  • gcc的fvisibility选项 #2
  • 控制符号的可见性
  • Why is the new C++ visibility support so useful?
  • C++ -fvisibility=hidden -fvisibility-inlines-hidden
  • target_compile_definitions和target_compile_options中第二个参数的含义
  • target_compile_definitions for multiple CMake targets?
  • GCC编译之如何控制共享文件导出符号
  • Demystifying ARM Floating Point Compiler Options
  • liqi-c/tengine-nnie
  • How To Cross-Compile Clang/LLVM using Clang/LLVM
  • [ARM]ARM平台处理器简介-ARMv7
  • Building OpenCV 3.4.6 for Xavier - is VFPV3 supported?
  • ArmHardFloatPortVfpComparison
  • himix200.toolchain.cmake
  • SylixOS ARMv7A 处理器性能测试与改进
  • 移植OpenCV4.1.0到Hi3519AV100上
  • OpenCV arm용으로 포팅하기
  • Building OpenCV for ARM Cortex-A8
  • Cross compilation for ARM based Linux systems
  • Cross compiling OpenCV 4 for Raspberry Pi and BeagleBone Black
  • armv7 build with neon on error, and resize will be too slow #13886
  • Optimizing opencv 3.4 in Raspberry Pi 3 using GStreamer
  • Enabling optimization options for Raspberry Pi 3 with OpenCV 4.0.0
  • HI3516D之Caffe移植
  • Failed to cross-compile with some"undefined reference" errors #11878

你可能感兴趣的:(opencv,arm)