error: passing argument 4 of ‘proc_create’ from incompatible pointer type [-Werror=incompatible-pointer-types]
今天又在下午做实验(坐牢)期间遇到了折磨人的bug,写完了编译出现一个warning:error: passing argument 4 of ‘proc_create’ from incompatible pointer type [-Werror=incompatible-pointer-types]
在找遍了中文网站都没找到答案后终于受不了了才选择英文网站寻找答案。
error的提示意思是参数指针类型不匹配(pointer incompatible )。本来觉得不匹配也就是一个warning没关系,不过后来还是想找到为什么。。。问题的关键就是老师给出的实验代码太老了,没法在大概linux-2.6.xx后使用了,因此出现了报错。正确答案如下:
把tasklist.c文件第78行左右的函数static struct proc_ops my_proc及其内容替换为如下代码即可:
static struct seq_operations my_seq_fops = {
//序列文件记录操作函数集合
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show
};
顺便贴一下tasklist.c的完整代码,有需要的自取:
//-------------------------------------------------------------------
// tasklist.c: 本内核文件创建一个proc伪文件,'/proc/tasklist'
// 通过如下命令可以显示系统中所有进程的部分信息
// 注意:Mkefile文件必须正确放置在当前目录下。
// 编译命令: make
// 内核模块添加:$sudo insmod tasklist.ko
// 添加内核模块后读取并信息tasklist内核信息: $ cat /proc/tasklist
// 内核模块删除:$sudo rmmod tasklist
// NOTE: Written and tested with Linux kernel version 3.13.0 and.3.2.0
// 用户也可以自己完成相关的读写程序,就像课后练习1要求的。我们尤其鼓励同学自己写程序代替cat程序。
// strace函数可用于追踪系统调用,命令格式如下所示:
// $ strace cat /proc/tasklist
//-------------------------------------------------------------------
#include <linux/module.h> // for init_module()
#include <linux/proc_fs.h> // for create_proc_info_entry()
#include <linux/sched.h> // for init_task
#include <linux/seq_file.h> // for sequence files
#include <linux/slab.h