创建基于 sysroot 的 linux arm64 交叉编译环境

背景

编译 arm64 架构的程序的方法有两种:

  • 将代码上传到 arm64 架构的机器上编译。
  • 在 x64 架构上进行 arm64 交叉编译。

多数需要交叉编译的场景一般是跨平台多架构支持或是嵌入式开发。使用 sysroot 方法是一个更优的方案,不需要特定架构的编译服务器,支持的架构可以方便扩展。

什么是 sysroot

sysroot 是交叉编译过程中使用的一个概念,指目标系统根文件系统中的一个目录结构。 该目录结构包含编译器和链接器所需的所有目标系统头文件、库文件和其他必要文件。 使用 sysroot 允许编译器和链接器在编译和链接阶段像在目标系统上运行一样进行编译和链接,从而生成可在目标系统上运行的可执行文件和库。

sysroot 在交叉编译设置中非常重要,因为它提供了目标平台的环境,包括但不限于:

  • 头文件:包含目标系统 API 和库的声明。
  • 库文件:包含目标系统上可用的静态和动态链接库。
  • 其他文件:可能包括其他必要的配置文件和资源。

使用 sysroot 的好处包括:

  • 隔离:它提供了一个隔离的环境,避免了本地系统和目标系统之间的冲突。
  • 一致性:确保编译后的程序能在目标系统上正确运行,因为它们链接了目标系统的库。
  • 可移植性:使交叉编译过程更加灵活和可移植,因为只需将 sysroot 目录复制到不同的编译环境中即可重复使用。

创建 sysroot

下面演示在 Ubuntu2204 x64 架构 的主机(host)上创建 arm64 架构(target)的 sysroot 主要步骤.

安装创建 sysroot 的工具

sudo apt update
sudo apt install debootstrap coreutils qemu qemu-user-static

创建 sysroot 目录

mkdir <target_dir>
  
# The --foreign option is used to split the debootstrap process into two stages.
# This is necessary when setting up a root filesystem for a different architecture 
# because the second stage of the process (configuring the packages) needs to run on the target architecture.
# You'll need to copy the qemu-*-static binary into the new root filesystem and then run the second stage manually.
sudo debootstrap --arch arm64 --foreign bionic <target_dir>
sudo cp /usr/bin/qemu-aarch64-static <target_dir>/usr/bin/
  
# Do the initial unpack phase of bootstrapping only,
# for example if the target architecture does not match the host architecture.
# A copy of debootstrap sufficient for completing the bootstrap process will be installed as /debootstrap/debootstrap in the target filesystem.
# You can run it with the --second-stage option to complete the bootstrapping process.
sudo chroot <target_dir> /debootstrap/debootstrap --second-stage

bionic 对应的是 Ubuntu 发行版本的 code name,也就是 Ubuntu1804,如果需要构建其他版本的环境,搜索一下对应版本的 code name 即可。

target_dir 就是我们创建 arm64 的 sysroot 的文件夹。至此,arm64 的 sysroot 环境准备就绪。

安装编译 arm64 架构的依赖库

使用 chroot 工具切换到 sysroot 目录下,也就是上面步骤创建的**target_dir** ,这样我们就可以把 arm64 架构的依赖包安装到sysroot 目录下了。

# change fs from x64 to arm64
sudo chroot <target_dir>
  
# The following steps as if you were using a machine with the arm64 architecture.
# Install compilation dependencies
apt install packages-you-need.deb

# change fs frome arm64 to x64, prepare to cross build on host.
exit

交叉编译

交叉编译推荐使用 CMake 的 toolchains 工具链。这里引用 CMake 的官方示例稍加修改。

set(CMAKE_SYSTEM_NAME Linux)
# 指定交叉编译的架构为 arm64
set(CMAKE_SYSTEM_PROCESSOR aarch64)
# 指定 sysroot 目录,以及 pkg-config 文件的搜索路径,这里我们需要到 sysroot 下查找
set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})
set(ENV{PKG_CONFIG_PATH} ${CMAKE_SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig)

# 指定编译器,此处需要注意,我们仍然使用 host 主机上的编译器
# 让 host 主机上的编译器去编译我们的代码,链接的是 sysroot 中的库
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc-10)
set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++-10)
# 指定编译时不使用 sysroot 中的工具,只在 sysroot 中查到头文件和库
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)

编译过程中如果遇到找不到头文件或是链接不到库,首先要确认头文件或是库的路径在不在 sysroot 目录下,切记不会使用到 host 主机下的头文件或库。

参考资料

  1. https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-linux

最后

分享Linux开发中的小技巧。如果文章对您有帮助,不妨关注、收藏和转发,感谢。

原文链接:,转发请注明来源!