1、模板示例

包含1种文件:

1 build.sh 脚本,设定交叉编译的过程

我们的目标是,直接执行./build.sh --mlu-arch=SD5223C --cpu-arch=aarch64 就可以自动交叉编译

build.sh,先看下这个模板

#!/bin/bash
################################################################################
if [ $NEUWARE_HOME ] ;then
  echo "NEUWARE_HOME: ${NEUWARE_HOME} has been set."
else
  export NEUWARE_HOME="/usr/local/neuware"
  echo "set NEUWARE_HOME: ${NEUWARE_HOME} by default."
fi

BUILD_DIR="build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"

BUILD_MODE="release"
MLU_ARCH=""
TARGET_CPU_ARCH="x86_64-linux-gnu"
TARGET_C_COMPILER="$(which gcc)"
TARGET_CXX_COMPILER="$(which g++)"
if [ $# != 0 ]; then
  while [ $# != 0 ]; do
    case "$1" in
      --cpu-arch=*)
          TMP_STRING=$1
          TARGET_CPU_ARCH=${TMP_STRING#*=};
          TARGET_CPU_ARCH=${TARGET_CPU_ARCH}-linux-gnu;
          shift
          ;;
      --mlu-arch=*)
          TMP_MLU_ARCH=${1}
          MLU_ARCH=${TMP_MLU_ARCH#*=}
          shift
          ;;
      -d | --debug)
          BUILD_MODE="debug"
          echo "-- Using debug mode."
          shift
          ;;
      -v | --verbose)
          BUILD_VERBOSE="VERBOSE=1"
          shift
          ;;
    esac
  done
fi

## TOOLCHAIN_ROOT not exist, use default env
if [ ! "${TOOLCHAIN_ROOT}" ]; then
  if [ "$TARGET_CPU_ARCH" == "aarch64-linux-gnu" ]; then
    if [ "$MLU_ARCH" == "SD5223" ]; then
      #TOOLCHAIN_ROOT="/tools/gcc/gcc-9/gcc-stable-9.3.0-2020.08-x86_64_aarch4-linux-gnu"
      TOOLCHAIN_ROOT="/tools/gcc/gcc-9/gcc-stable-9.3.0-2020.08-x86_64_aarch4-linux-gnu"
    else
      TOOLCHAIN_ROOT="/tmp/gcc-linaro-6.2.1-2016.11-x86_64_aarch64-linux-gnu"
    fi
    echo "Using default TOOLCHAIN_ROOT=${TOOLCHAIN_ROOT}"
    if [ -f ${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-gcc ]; then
      TARGET_C_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-gcc
      TARGET_CXX_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-g++
    else
      TARGET_C_COMPILER=${TOOLCHAIN_ROOT}/bin/aarch64-linux-gcc
      TARGET_CXX_COMPILER=${TOOLCHAIN_ROOT}/bin/aarch64-linux-g++
    fi
  fi
else
  echo "Using TOOLCHAIN_ROOT=${TOOLCHAIN_ROOT}"
  if [ "$TARGET_CPU_ARCH" == "aarch64-linux-gnu" ]; then
    if [ -f ${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-gcc ]; then
      TARGET_C_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-gcc
      TARGET_CXX_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-g++
    else
      TARGET_C_COMPILER=${TOOLCHAIN_ROOT}/bin/aarch64-linux-gcc
      TARGET_CXX_COMPILER=${TOOLCHAIN_ROOT}/bin/aarch64-linux-g++
    fi
  fi
fi

if [[ -f /proc/cpuinfo ]]; then
    MJOBS=$(grep -c processor /proc/cpuinfo)
else
    MJOBS=4
fi

pushd ${BUILD_DIR}
  cmake -DCMAKE_BUILD_TYPE="${BUILD_MODE}" \
           -DTOOLCHAIN_ROOT="${TOOLCHAIN_ROOT}" \
           -DTARGET_CPU_ARCH="${TARGET_CPU_ARCH}" \
           -DCMAKE_C_COMPILER="${TARGET_C_COMPILER}" \
           -DCMAKE_CXX_COMPILER="${TARGET_CXX_COMPILER}" \
           -DMLU_ARCH="${MLU_ARCH}" \
           -DCMAKE_INSTALL_PREFIX=../aarch_64_install \
           -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/aarch64-gnu.toolchain.cmake \
        ..
  make -j$MJOBS ${BUILD_VERBOSE}
popd

这个脚本只有2个地方需要修改:

  • 如果你的交叉编译器的地址修改,第53行 TOOLCHAIN_ROOT="/tmp/aarch64--glibc--stable-2020.08-1"。
  • TARGET_C_COMPILER和TARGET_CXX_COMPILER地址,需要注意。其他地方都很好理解。如果不理解,请在评论区讨论。
      TARGET_C_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-gcc
      TARGET_CXX_COMPILER=${TOOLCHAIN_ROOT}/bin/${TARGET_CPU_ARCH}-g++

这个脚本还是很好用的,如果理解里面的知识点的话!!

make install

编译结束后进入aarch_64_install文件夹。

opencv的交叉编译工具链在…/opencv3.4.16/platforms/linux 路径下,linux文件夹下是一些.cmake文件,对应不同的移植对象,本人需要在NVIDIA的Xavier部署模型,所以选择aarch64-gnu.toolchain.cmake 工具链。在opencv3.4.16目录下,编写build.sh文件,然后执行./build.sh --mlu-arch=SD5223C --cpu-arch=aarch64。 具体的buld.sh 这样写

2、undefined reference to `expf@GLIBC_2.27’问题的说明

1、确保安装了正确版本的 GLIBC。

  • 如果是为x86 的设备,通过命令 ldd --version 检查你的系统上安装的 GLIBC 版本。
  • 如果是 aarch64 设备,通过命令 # /tmp/aarch64–glibc–stable-2020.08-1/bin/aarch64-linux-gcc -v 检查你的系统上安装的 GLIBC 版本。
# /tmp/aarch64--glibc--stable-2020.08-1/bin/aarch64-linux-gcc -v
Using built-in specs.
COLLECT_GCC=/tmp/aarch64--glibc--stable-2020.08-1/bin/aarch64-linux-gcc.br_real
COLLECT_LTO_WRAPPER=/tmp/aarch64--glibc--stable-2020.08-1/bin/../libexec/gcc/aarch64-buildroot-linux-gnu/9.3.0/lto-wrapper
Target: aarch64-buildroot-linux-gnu
Configured with: ./configure --prefix=/opt/aarch64--glibc--stable-2020.08-1 --sysconfdir=/opt/aarch64--glibc--stable-2020.08-1/etc --enable-static --target=aarch64-buildroot-linux-gnu --with-sysroot=/opt/aarch64--glibc--stable-2020.08-1/aarch64-buildroot-linux-gnu/sysroot --enable-__cxa_atexit --with-gnu-ld --disable-libssp --disable-multilib --disable-decimal-float --with-gmp=/opt/aarch64--glibc--stable-2020.08-1 --with-mpc=/opt/aarch64--glibc--stable-2020.08-1 --with-mpfr=/opt/aarch64--glibc--stable-2020.08-1 --with-pkgversion='Buildroot 2020.08-14-ge5a2a90' --with-bugurl=http://bugs.buildroot.net/ --without-zstd --disable-libquadmath --disable-libquadmath-support --enable-tls --enable-threads --without-isl --without-cloog --with-abi=lp64 --with-cpu=cortex-a53 --enable-languages=c,c++,fortran --with-build-time-tools=/opt/aarch64--glibc--stable-2020.08-1/aarch64-buildroot-linux-gnu/bin --enable-shared --disable-libgomp
Thread model: posix
gcc version 9.3.0 (Buildroot 2020.08-14-ge5a2a90) 

确保链接器可以找到正确的库。在编译和链接程序时,需要指定正确的库路径。
实际上,我没有使用下列命令,替换为合适的交叉编译工具就可以了,理论上下列命令有效。

if(${TARGET_CPU_ARCH} MATCHES "aarch64-linux-gnu")
  #include_directories(${TOOLCHAIN_ROOT}/aarch64-linux-gnu/include/c++/6.2.1/)
  #include_directories(${TOOLCHAIN_ROOT}/aarch64-linux-gnu/include/c++/6.2.1/aarch64-linux-gnu/)
  #include_directories(${TOOLCHAIN_ROOT}/aarch64-linux-gnu/)
  message("${TARGET_CPU_ARCH}")
  include_directories(${TOOLCHAIN_ROOT}/aarch64-buildroot-linux-gnu/include/c++/9.3.0/)
  include_directories(${TOOLCHAIN_ROOT}/aarch64-buildroot-linux-gnu/include/c++/9.3.0/aarch64-buildroot-linux-gnu/)
  include_directories(${TOOLCHAIN_ROOT}/aarch64-buildroot-linux-gnu/)
endif()

3、仓库

在该地址可以找到对opencv的一个helloworld的交叉编译的演示。
https://gitee.com/hiyanyx/demo-source-code-myself-cambrian-sg-sd5223c-cross-compilation–v1-c/tree/master/helloOpencv

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐