在为android kernel编写模块的时候,将之前另一个工程的ko包驱动移植到新系统工程下,配置kernledir 等参数后,编译出现
warning: initialization from incompatible pointer type [enabled by default]
error, forbidden warning:
错误,因为在之前工程中编译没有问题,(因为之前的工程没有开始warning 停止选项)。
其中报警与错误信息解释如下
warning: initialization from incompatible pointer type [enabled by default]
表示编写的函数与系统中默认的函数参数不全相同。
error, forbidden warning:
查找了出错代码行 内容如下:
static const struct file_operations mytest_proc_fops = {
.owner = THIS_MODULE,
.read=xxx_proc_read,
.write=xxx_proc_write,
};
代码中实现的函数命名为
static ssize_t xxx_proc_write(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)
{
}
而查找file_operations 中对于.write的命名为:
struct file_operations { struct module *owner; loff_t(*llseek) (struct file *, loff_t, int); ssize_t(*read) (struct file *, char __user *, size_t, loff_t *); ssize_t(*aio_read) (struct kiocb *, char __user *, size_t, loff_t); ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t(*aio_write) (struct kiocb *, const char __user *, size_t, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t(*readv) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t(*writev) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t(*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void __user *); ssize_t(*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned long); };
其中ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *);static ssize_t xxx_proc_write(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)对比,第二个参数缺少了一个const 添加上后,编译警告去除,并编译通过。总结:在编写linux 驱动代码时,对于系统中定义的结构体中的函数,每个参数需要对应正确,否则会出现警告。不要凭印象,直接从相应结构体头文件中获取函数参数为佳。