没志青年
发布于 2026-01-28 / 25 阅读
0

编译 Linux 内核镜像

编译步骤

步骤:

  1. 指定架构:不同 CPU 架构的汇编指令、初始化代码等不同。

  2. 指定交叉编译工具链:特定的架构需要特定的编译工具。

  3. 选择配置文件:板子上的硬件资源不同。

  4. 编译:生成镜像文件。

(1)步骤1、2

修改源码顶层的 Makefile

ARCH		?= arm
CROSS_COMPILE	?= /opt/Linux_Workspace/i.MX6ULL/arm_gcc/bin/arm-linux-gnueabihf-

推荐交叉编译工具写完整。

(2)步骤3

生成配置文件,配置文件在 arch/$(ARCH)/configs 目录下,是各个厂商的默认配置文件。

make imx_alientek_emmc_defconfig
  • make xxx_defconfig:生成 .config 文件。

  • make savedefconfig:.config 文件生成 defconfig 文件(文件的后缀一定要是 _defconfig)

(3)步骤4

make zImage dtbs -j$(nproc)

编译后的镜像在 arch/arm/boot 下。

编译选项:

  • make:编译内核(不会生成 zImage) + 设备树 .dtb + 模块 .ko

  • make zImage:编译出 zImage 镜像

  • make dtbs:编译出设备树 .dtb 文件

  • make modules:编译内核模块 .ko(设置中选为“M”的模块)

  • make modules_install INSTALL_MOD_PATH=根文件系统路径:把 .ko 安装到 rootfs

  • make clean:清理编译产生的中间文件和结果

  • make distclean:清理编译产生的中间文件和结果、清理配置文件

编译的内核镜像的区别:

  • zImage:

  • uImage:

编译后的是vmlinux,zImage等都是这个基础上的变体。

出现这个表示 zImage 编译好了:

  OBJCOPY arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready

使能编译模块:

i.MX6ULL 编译报错

(1)lzop 不存在

/bin/sh: 1: lzop: not found
arch/arm/boot/compressed/Makefile:180: recipe for target 'arch/arm/boot/compressed/piggy.lzo' failed
make[2]: *** [arch/arm/boot/compressed/piggy.lzo] Error 1
arch/arm/boot/Makefile:52: recipe for target 'arch/arm/boot/compressed/vmlinux' failed
make[1]: *** [arch/arm/boot/compressed/vmlinux] Error 2
arch/arm/Makefile:316: recipe for target 'zImage' failed
make: *** [zImage] Error 2
make: *** Waiting for unfinished jobs....

解决办法:安装依赖后重新编译

sudo apt-get install -y lzop

(2)源码错误

/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x50): multiple definition of `yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status

解决方法:

vim scripts/dtc/dtc-parser.tab.c
vim scripts/dtc/dtc-parser.tab.c_shipped

将两个文件中的 YYLTYPE yylloc;改为 extern YYLTYPE yylloc;