下载工具链并解压 https://toolchains.bootlin.com/
# 拉取内核源码
wget <https://mirrors.ustc.edu.cn/kernel.org/linux/kernel/v6.x/linux-6.15.2.tar.xz>
tar xf linux-6.15.2.tar.xz
cd linux-6.15.2
# 设置交叉编译器, 自个放哪设置哪里
export CROSS_COMPILE=/home/cyang/Public/armv7-eabihf--musl--stable-2024.05-1/bin/arm-buildroot-linux-musleabihf-
# 生成默认编译配置, 这里会寻找 arch/arm/configgs/imx_v7_defconfig
make ARCH=arm imx_v6_v7_defconfig
make ARCH=arm -j$(nproc)
正常就会得到目标 zImage
文件
编译驱动, 参考 hello world - 交叉编译内核驱动程序
hello.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static int __init hello_init(void) {
printk(KERN_INFO "Hello, World!\\n");
return 0;
}
static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, World!\\n"); }
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("MIT");
Makefile
ARCH=arm
KERNEL_MODULES=../linux-6.15.2/
# 编译器在哪
CROSS_COMPILE=/home/cyang/Public/rk3506G2/armv7-eabihf--musl--stable-2024.05-1/bin/arm-buildroot-linux-musleabihf-
# 导出变量, 被kernel的Makefile接受
export ARCH CROSS_COMPILE
obj-m += hello.o
all:
make -C ../linux-6.15.2/ M=$(PWD) modules
clean:
make -C ../linux-6.15.2/ M=$(PWD) clean