linux 模块编译错误

在为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:


表示开启了编译出现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 驱动代码时,对于系统中定义的结构体中的函数,每个参数需要对应正确,否则会出现警告。不要凭印象,直接从相应结构体
头文件中获取函数参数为佳。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值