编译内核模块遇asm头文件缺失:软链接无法解决多依赖问题
内核模块编译头文件缺失问题解决
问题描述
我尝试编译一个最小化内核模块,使用如下Makefile:
obj-m += mymodule.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
编译时出现头文件找不到的错误,我创建软链接解决了第一个问题,但随后又遇到另一个头文件缺失的问题,而一个软链接无法同时指向两个路径。我的理解是否有误?该如何解决此问题?
编译错误日志与操作记录
root@system-yocto:~/kernelModule# make -j make -C /lib/modules/5.15.54-yocto-standard/build M=/home/root/kernelModule modules make[1]: Entering directory '/lib/modules/5.15.54-yocto-standard/build' warning: the compiler differs from the one used to build the kernel The kernel was built by: arm-poky-linux-gnueabi-gcc (GCC) 11.4.0 You are using: gcc (GCC) 11.4.0 CC [M] /home/root/kernelModule/mymodule.o In file included from ./include/linux/init.h:5, from /home/root/kernelModule/mymodule.c:1: ./include/linux/compiler.h:255:10: fatal error: asm/rwonce.h: No such file or directory 255 | #include <asm/rwonce.h> | ^~~~~~~~~~~~~~ compilation terminated. make[2]: *** [scripts/Makefile.build:288: /home/root/kernelModule/mymodule.o] Error 1 make[1]: *** [Makefile:1886: /home/root/kernelModule] Error 2 make[1]: Leaving directory '/lib/modules/5.15.54-yocto-standard/build' make: *** [Makefile:4: all] Error 2 root@system-yocto:~/kernelModule# ls -l /lib/modules/5.15.54-yocto-standard/source/include/ total 204 drwxr-xr-x 3 root root 4096 Mar 9 12:34 acpi drwxr-xr-x 4 root root 4096 Mar 9 12:42 asm-generic drwxr-xr-x 2 root root 4096 Mar 9 12:34 clocksource drwxr-xr-x 2 root root 53248 Mar 9 12:34 config drwxr-xr-x 3 root root 4096 Mar 9 12:34 crypto drwxr-xr-x 5 root root 4096 Mar 9 12:34 drm drwxr-xr-x 37 root root 4096 Mar 9 12:34 dt-bindings drwxr-xr-x 3 root root 4096 Mar 9 12:34 generated drwxr-xr-x 2 root root 4096 Mar 9 12:34 keys drwxr-xr-x 2 root root 4096 Mar 9 12:34 kunit drwxr-xr-x 2 root root 4096 Mar 9 12:34 kvm drwxr-xr-x 65 root root 40960 Mar 9 12:34 linux drwxr-xr-x 2 root root 4096 Mar 9 12:34 math-emu drwxr-xr-x 6 root root 4096 Mar 9 12:34 media drwxr-xr-x 2 root root 4096 Mar 9 12:34 memory drwxr-xr-x 2 root root 4096 Mar 9 12:34 misc drwxr-xr-x 12 root root 4096 Mar 9 12:34 net drwxr-xr-x 2 root root 4096 Mar 9 12:34 pcmcia drwxr-xr-x 2 root root 4096 Mar 9 12:34 ras drwxr-xr-x 2 root root 4096 Mar 9 12:34 rdma drwxr-xr-x 3 root root 4096 Mar 9 12:34 scsi drwxr-xr-x 16 root root 4096 Mar 9 12:34 soc drwxr-xr-x 4 root root 4096 Mar 9 12:34 sound drwxr-xr-x 3 root root 4096 Mar 9 12:34 target drwxr-xr-x 3 root root 4096 Mar 9 12:34 trace drwxr-xr-x 12 root root 4096 Mar 9 12:34 uapi drwxr-xr-x 2 root root 4096 Mar 9 12:34 vdso drwxr-xr-x 2 root root 4096 Mar 9 12:34 video drwxr-xr-x 4 root root 4096 Mar 9 12:34 xen root@system-yocto:~/kernelModule# ln -s /lib/modules/5.15.54-yocto-standard/source/include/asm-generic/ /lib/modules/5.15.54-yocto-standard/source/include/asm root@system-yocto:~/kernelModule# make -j make -C /lib/modules/5.15.54-yocto-standard/build M=/home/root/kernelModule modules make[1]: Entering directory '/lib/modules/5.15.54-yocto-standard/build' warning: the compiler differs from the one used to build the kernel The kernel was built by: arm-poky-linux-gnueabi-gcc (GCC) 11.4.0 You are using: gcc (GCC) 11.4.0 CC [M] /home/root/kernelModule/mymodule.o In file included from ./include/linux/errno.h:5, from ./include/linux/string.h:8, from ./include/linux/bitmap.h:10, from ./include/linux/cpumask.h:12, from ./include/linux/mm_types_task.h:14, from ./include/linux/mm_types.h:5, from ./include/linux/buildid.h:5, from ./include/linux/module.h:14, from /home/root/kernelModule/mymodule.c:2: ./include/uapi/linux/errno.h:1:10: fatal error: asm/errno.h: No such file or directory 1 | #include <asm/errno.h> | ^~~~~~~~~~~~~ compilation terminated. make[2]: *** [scripts/Makefile.build:288: /home/root/kernelModule/mymodule.o] Error 1 make[1]: *** [Makefile:1886: /home/root/kernelModule] Error 2 make[1]: Leaving directory '/lib/modules/5.15.54-yocto-standard/build' make: *** [Makefile:4: all] Error 2
问题根源与解决方法
你的理解没错,单个软链接确实无法同时指向两个路径,核心问题在于Yocto环境下内核头文件包不完整,或者/lib/modules/$(uname -r)/build指向的内核源码缺少对应架构的asm目录。以下是可行的解决步骤:
1. 安装完整内核头文件包
在Yocto系统中,必须安装对应内核版本的kernel-dev包,执行命令:
opkg install kernel-dev-5.15.54-yocto-standard
注意替换版本号为uname -r输出的实际内核版本。
2. 验证内核源码链接
确认/lib/modules/$(uname -r)/build和/lib/modules/$(uname -r)/source指向完整的内核源码目录。如果链接错误,重新创建:
rm -f /lib/modules/5.15.54-yocto-standard/build ln -s /path/to/your/complete/kernel/source /lib/modules/5.15.54-yocto-standard/build
3. 使用Yocto交叉编译工具链(推荐)
当前使用的gcc与内核编译时的arm-poky-linux-gnueabi-gcc不一致,后续易出现兼容性问题。正确做法是使用Yocto提供的交叉编译环境:
- 加载Yocto环境变量:
source /path/to/yocto/build/environment-setup-armv7at2hf-neon-poky-linux-gnueabi - 修改Makefile,指定交叉编译器:
obj-m += mymodule.o ARCH=arm CROSS_COMPILE=arm-poky-linux-gnueabi- all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean - 重新执行
make编译。
4. 临时应急修复(不推荐长期使用)
若无法安装完整头文件包,可手动创建asm目录并合并头文件:
mkdir -p /lib/modules/5.15.54-yocto-standard/source/include/asm cp -r /lib/modules/5.15.54-yocto-standard/source/include/asm-generic/* /lib/modules/5.15.54-yocto-standard/source/include/asm/ # 复制对应架构的asm头文件,例如从Yocto构建目录获取arm架构内容 cp -r /path/to/yocto/build/tmp/work-shared/.../linux-yocto/include/asm/* /lib/modules/5.15.54-yocto-standard/source/include/asm/
此方法易引发头文件冲突,仅作临时方案。
内容的提问来源于stack exchange,提问作者ohiliouh
相关产品推荐
相关产品推荐

