创建基于 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

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 主机下的头文件或库。

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值