在上一章中,我们从宏观层面剖析了嵌入式 CI/CD 的价值与架构,本章将 从零开始,以 Ubuntu(Linux)为基础,深入实践如何自动化搭建一个可靠、一致、可复现的交叉编译环境,并将其无缝集成到 CI/CD 流水线中。内容涵盖:
-
Ubuntu 环境下交叉编译链安装与配置
-
Docker 容器化构建环境的封装
-
Makefile 与 CMake 的最佳实践
-
Git 钩子(pre-commit)实现代码风格与静态检查
-
ccache 构建缓存与并行加速
2.1 Ubuntu 下交叉编译链安装与配置
2.1.1 安装基础工具
在 Ubuntu 20.04/22.04 上,首先更新系统并安装常见构建工具:
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential git wget curl
2.1.2 安装 ARM 交叉编译器
推荐使用官方 GNU Arm Embedded Toolchain:
-
从 Arm 官网下载最新版本(例如
gcc-arm-none-eabi-10-2020-q4-major
)。 -
解压并放置到
/opt
:sudo mkdir -p /opt/gcc-arm-none-eabi sudo tar xf gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 \ -C /opt/gcc-arm-none-eabi --strip-components=1
-
配置环境变量:在
~/.bashrc
或/etc/profile.d/arm-toolchain.sh
添加:export PATH=/opt/gcc-arm-none-eabi/bin:$PATH
然后执行
source ~/.bashrc
。