编译Linux内核模块ko时遇到ERROR: modpost undefined的解决方法

本文介绍了在编译Linux内核模块时遇到的常见错误,如_printk、kfree等函数未定义的问题。错误原因是由于CONFIG_TRIM_UNUSED_KSYMS配置导致,关闭该选项并重新编译内核源码可以解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于情况各异,若本文方案解决不了,大家也可以参考本文评论区各路大佬的解决方案尝试解决。

注意!

本文仅针对找不到常用函数有用!!!
比如_printkkfreekmallocmemset啥的;
如果不是常用函数,是自己其他模块中的函数,本文不适用。

背景

笔者在编译ko模块时,遇到了如下报错:
modpost提示的都是常用函数啊,比如_printkkfreekmallocmemset啥的

ERROR: modpost: "__raw_copy_from_user" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "_printk" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "nonseekable_open" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "kmalloc_caches" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "kmalloc_trace" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "kfree" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "warn_slowpath_fmt" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "memset" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!
ERROR: modpost: "__register_chrdev" [/home/geng/Item/nscscc_drivers/lcd/lcd.ko] undefined!

报错信息,都是ERROR: modpost!

一度怀疑是自己的Makefile、交叉编译器等出了问题…

解决方案

问题出在Linux内核原码的.config文件上!

CONFIG_TRIM_UNUSED_KSYMS关闭掉就可以了!修改后记得重新make一下Linux源码! 修改后记得重新make一下Linux源码!

可以通过menuconfig修改,这里以MIPS的menuconfig为例:
menuconfig一级菜单 Enable loadable moudle support
关闭 Trim unused exported kernel symbols
也可以直接怼.config文件,把:

CONFIG_TRIM_UNUSED_KSYMS=y

直接换成

# CONFIG_TRIM_UNUSED_KSYMS is not set

修改后记得重新make一下Linux源码! 修改后记得重新make一下Linux源码!

Linux设备驱动程序学习(0)-Hello, world!模块 Linux设备驱动程序学习(0) -Hello, world!模块 一个学习Linux设备驱动程序都会碰到的第一个例程: #include #include MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, Tekkaman Ninja !\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, Tekkaman Ninja !\n Love Linux !Love ARM ! Love KeKe !\n"); } module_init(hello_init); module_exit(hello_exit); 我将其复制到我的工作目录,并编写了一个简单的Makefile文件: KERNELDIR = /home/tekkaman/working/SBC2440/linux-2.6.22.2 # The current directory is passed to sub-makes as argument PWD := $(shell pwd) INSTALLDIR = /home/tekkaman/working/rootfs/lib/modules CROSS_COMPILE =/home/tekkaman/working/crosstool-gcc410-k26222/gcc-4.1.0-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu- CC = $(CROSS_COMPILE)gcc obj-m := hello.o modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install: cp hello.ko $(INSTALLDIR) clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions .PHONY: modules modules_install clean 说实话,以上是我参考了《Linux设备驱动程序(第3版)》的Makefile源码修改得来的。我对Makefile不是很了解,是该好好学习学习了! 然后就是make modules 、 make modules_install 。 [root@Tekkaman-Ninja Helloworld]# make modules make -C /home/tekkaman/working/SBC2440/linux-2.6.22.2 M=/home/tekkaman/working/Linuxdriver/Helloworld modules make[1]: Entering directory `/home/tekkaman/working/SBC2440/linux-2.6.22.2' CC [M] /home/tekkaman/working/Linuxdriver/Helloworld/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/tekkaman/working/Linuxdriver/Helloworld/hello.mod.o LD [M] /home/tekkaman/working/Linuxdriver/Helloworld/hello.ko make[1]: Leaving directory `/home/tekkaman/working/SBC2440/linux-2.6.22.2' [root@Tekkaman-Ninja Helloworld]# make modules_install cp hello.ko /home/tekkaman/working/rootfs/lib/modules [root@Tekkaman-Ninja Helloworld]# 在我的开发板上的操作: [Tekkaman2440@SBC2440V4]#cd /lib/modules/ [Tekkaman2440@SBC2440V4]#ls cs89x0.ko hello.
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值