错误: 隐式声明函数‘kmalloc’ [-Werror=implicit-function-declaration]

本文解决在Linux环境下编译时遇到的隐式声明函数错误问题,通过包含必要的头文件来修复kmalloc与kfree函数的声明问题。

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

编译时遇到这个错误,原因是没有包含头文件linux/slab.h.

/home/xxha/study/char/memdev.c:152:2: 错误: 隐式声明函数‘kmalloc’ [-Werror=implicit-function-declaration]

隐式声明函数错误的意思是函数在未声明之前就被调用了,需要先声明这两个函数,版本变化导致这个错误的出现。

解决方法:

找到kmalloc与kfree的声明头文件并include进来就可以了

#include <linux/slab.h>



以下为我编写的内核模块和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 mean ‘vmalloc’? [-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、付费专栏及课程。

余额充值