3. 为QEMU ARM virt平台编译U-Boot
任务目标
本案例目标是在Ubuntu主机上安装U-Boot编译所需的交叉编译工具链(针对ARM架构)及必要依赖包,并通过编译一个最小C程序验证工具链可用。
环境与依赖
- 主机系统:Ubuntu 20.04/22.04 x86_64
- 目标架构:ARM(32位,对应U-Boot常见的armv7/armv8 32位模式)
- 需要安装的包:
gcc-arm-linux-gnueabihf(交叉编译器)、build-essential(主机编译工具)、libssl-dev(U-Boot编译依赖)、bc、python3、device-tree-compiler(dtc)
安装步骤
执行以下命令安装全部依赖:
sudo apt update
sudo apt install -y gcc-arm-linux-gnueabihf build-essential libssl-dev bc python3 device-tree-compiler
验证编译环境
创建一个最小C程序,分别用主机编译器与交叉编译器编译,确认工具链工作正常:
# 创建测试文件
cat > hello.c << 'EOF'
#include <stdio.h>
int main(void) {
printf("Toolchain OK\n");
return 0;
}
EOF
# 主机编译(x86)
gcc hello.c -o hello_host
./hello_host
# 交叉编译(ARM)
arm-linux-gnueabihf-gcc hello.c -o hello_arm
file hello_arm
验证方法与通过标准
- 主机编译运行输出
Toolchain OK,说明主机编译环境正常。 file hello_arm输出包含ARM和EABI字样,说明交叉编译成功生成ARM可执行文件。- 确认交叉编译器版本:
arm-linux-gnueabihf-gcc --version能正常输出版本信息。 - 确认dtc工具存在:
dtc --version能输出版本号。
以上全部通过,即表示U-Boot编译环境配置完成,可进入后续U-Boot源码编译案例。