驱动模块编译错误提示--implicit declaration of function ‘kmalloc’

本文介绍了一次在Linux环境下编译驱动测试模块时遇到的编译警告与错误,具体表现为格式不匹配及函数声明缺失等问题,并给出了通过引入<linux/slab.h>头文件来解决这些问题的方法。

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

编译一个驱动测试模块出现如下编译错误,

make: Entering directory `/usr/src/kernels/2.6.35.6-45.fc14.i686'
  CC [M]  /home/ty/development/driver/ch6/globalmem.o
/home/ty/development/driver/ch6/globalmem.c: In function ‘globalmem_read’:
/home/ty/development/driver/ch6/globalmem.c:77:7: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long unsigned int’
/home/ty/development/driver/ch6/globalmem.c: In function ‘globalmem_write’:
/home/ty/development/driver/ch6/globalmem.c:100:7: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long unsigned int’
/home/ty/development/driver/ch6/globalmem.c: In function ‘globalmem_init’:
/home/ty/development/driver/ch6/globalmem.c:185:3: error: implicit declaration of function ‘kmalloc’
/home/ty/development/driver/ch6/globalmem.c:185:18: warning: assignment makes pointer from integer without a cast
/home/ty/development/driver/ch6/globalmem.c: In function ‘globalmem_exit’:
/home/ty/development/driver/ch6/globalmem.c:202:3: error: implicit declaration of function ‘kfree’
make[1]: *** [/home/ty/development/driver/ch6/globalmem.o] Error 1
make: *** [_module_/home/ty/development/driver/ch6] Error 2
make: Leaving directory `/usr/src/kernels/2.6.35.6-45.fc14.i686'

 

在网上找了一下,是缺少了一个头文件

#include <linux/slab.h>

加上之后就没问题了

 

原文地址:http://blog.youkuaiyun.com/tiany524/article/details/6358771

以下为我编写的内核模块和makefile文件:#include <linux/init.h> //所有模块都会需要这个头文件 #include <linux/module.h> //下面的宏需要 static int __init hello_init(void){ printk(KERN_INFO "module init success\n"); return 0; } static void __exit hello_exit(void){ printk(KERN_INFO "module exit success\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); //开源协议 MODULE_AUTHOR("作者"); MODULE_DESCRIPTION("功能描述"); obj-m := hello.o PWD := $(shell pwd) KVER := $(shell uname -r) KDIR :=/lib/modules/$(KVER)/build/ all: $(MAKE) -C $(KDIR) M=$(PWD) clean: rm -rf *.o *.mod.c *.mod.o *.ko *.symvers *.order *.a——运行时出现以下报错,请问怎么解决:linzihao@linzihao-virtual-machine:~/Code/test/coursework2/work3$ make make -C /lib/modules/4.15.0-213-generic/build M=/home/linzihao/Code/test/coursework2/work3 modules make[1]: 进入目录“/usr/src/linux-headers-4.15.0-213-generic” CC [M] /home/linzihao/Code/test/coursework2/work3/my_char_dev.o /home/linzihao/Code/test/coursework2/work3/my_char_dev.c: In function ‘dev_write’: /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:20:12: error: implicit declaration of functionkmalloc’; did you meanvmalloc? [-Werror=implicit-function-declaration] kbuf = kmalloc(count + 1, GFP_KERNEL); ^~~~~~~ vmalloc /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:20:10: warning: assignment makes pointer from integer without a cast [-Wint-conversion] kbuf = kmalloc(count + 1, GFP_KERNEL); ^ /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:24:9: error: implicit declaration of function ‘kfree’; did you mean ‘vfree’? [-Werror=implicit-function-declaration] kfree(kbuf); ^~~~~ vfree /home/linzihao/Code/test/coursework2/work3/my_char_dev.c: In function ‘dev_exit’: /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:61:20: error: implicit declaration of function ‘class_find’; did you mean ‘devres_find’? [-Werror=implicit-function-declaration] device_destroy(class_find("char_class", NULL), major); ^~~~~~~~~~ devres_find /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:61:20: warning: passing argument 1 of ‘device_destroy’ makes pointer from integer without a cast [-Wint-conversion] In file included from ./include/linux/cdev.h:8:0, from /home/linzihao/Code/test/coursework2/work3/my_char_dev.c:3: ./include/linux/device.h:1216:13: note: expected ‘struct class *’ but argument is of type ‘int’ extern void device_destroy(struct class *cls, dev_t devt); ^~~~~~~~~~~~~~ cc1: some warnings being treated as errors scripts/Makefile.build:340: recipe for target '/home/linzihao/Code/test/coursework2/work3/my_char_dev.o' failed make[2]: *** [/home/linzihao/Code/test/coursework2/work3/my_char_dev.o] Error 1 Makefile:1596: recipe for target '_module_/home/linzihao/Code/test/coursework2/work3' failed make[1]: *** [_module_/home/linzihao/Code/test/coursework2/work3] Error 2 make[1]: 离开目录“/usr/src/linux-headers-4.15.0-213-generic” Makefile:7: recipe for target 'default' failed make: *** [default] Error 2 linzihao@linzihao-virtual-machine:~/Code/test/coursework2/work3$
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值