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

本文介绍了在编译过程中遇到的kmalloc函数未声明错误的原因及解决方案。此问题通常是由于缺少必要的头文件linux/slab.h导致的。文章提供了具体的解决步骤。

摘要生成于 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
huaxi@ubuntu:~/c_projects/kernel_module$ make make -C /lib/modules/4.15.0-142-generic/build M=/home/huaxi/c_projects/kernel_module modules make[1]: Entering directory '/usr/src/linux-headers-4.15.0-142-generic' CC [M] /home/huaxi/c_projects/kernel_module/ping_stat.o /home/huaxi/c_projects/kernel_module/ping_stat.c: In function ‘hook_func’: /home/huaxi/c_projects/kernel_module/ping_stat.c:66:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement] struct ping_stat *entry = kmalloc(sizeof(*entry), GFP_ATOMIC); ^ /home/huaxi/c_projects/kernel_module/ping_stat.c: At top level: /home/huaxi/c_projects/kernel_module/ping_stat.c:89:17: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .hook = hook_func, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:89:17: note: (near initialization for ‘nf_ops.hook’) /home/huaxi/c_projects/kernel_module/ping_stat.c: In function ‘seq_show’: /home/huaxi/c_projects/kernel_module/ping_stat.c:115:19: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘unsigned int’ [-Wformat=] seq_printf(s, "%-15pI4 %-20lu %u\n", ^ /home/huaxi/c_projects/kernel_module/ping_stat.c: In function ‘ping_stat_init’: /home/huaxi/c_projects/kernel_module/ping_stat.c:152:5: error: implicit declaration of function ‘nf_register_hook’ [-Werror=implicit-function-declaration] nf_register_hook(&nf_ops); ^ /home/huaxi/c_projects/kernel_module/ping_stat.c: In function ‘ping_stat_exit’: /home/huaxi/c_projects/kernel_module/ping_stat.c:164:5: error: implicit declaration of function ‘nf_unregister_hook’ [-Werror=implicit-function-declaration] nf_unregister_hook(&nf_ops); ^ cc1: some warnings being treated as errors scripts/Makefile.build:337: recipe for target '/home/huaxi/c_projects/kernel_module/ping_stat.o' failed make[2]: *** [/home/huaxi/c_projects/kernel_module/ping_stat.o] Error 1 Makefile:1584: recipe for target '_module_/home/huaxi/c_projects/kernel_module' failed make[1]: *** [_module_/home/huaxi/c_projects/kernel_module] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-142-generic' Makefile:4: recipe for target 'all' failed make: *** [all] Error 2 huaxi@ubuntu:~/c_projects/kernel_module$
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值