为什么会出现不兼容的指针类型警告?

问题描述:

我正在使用内核3.13.0编写Linux设备驱动程序,我很困惑,为什么我得到这个警告。为什么会出现不兼容的指针类型警告?

warning: initialization from incompatible pointer type [enabled by default] 
    .read = read_proc, 
    ^
warning: (near initialization for ‘proc_fops.read’) [enabled by default] 

据我可以告诉我的proc函数的file_operations设置是相同的设备功能。我可以读写/ dev/MyDevice,没有任何问题,也没有任何警告。 proc写函数不会引发警告,只会引发读取。我做错了什么?

/*****************************************************************************/ 
//DEVICE OPERATIONS 
/*****************************************************************************/ 
static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t 
    len, loff_t *p_off) 
{ 
    //Not relevant to this question 
} 

static ssize_t dev_write(struct file *pfil, const char __user *pBuf, 
         size_t len, loff_t *p_off) 
{ 
    //Not relevant to this question 
} 

static struct file_operations dev_fops = 
{ //None of these cause a warning but the code is identical the proc code below 
    .owner = THIS_MODULE, 
    .read = dev_read, 
    .write = dev_write 
}; 

/*****************************************************************************/ 
//PROCESS OPERATIONS 
/*****************************************************************************/ 
static int read_proc(struct file *pfil, char __user *pBuf, size_t 
       len, loff_t *p_off) 
{ 
    //Not relevant to this question 
} 

static ssize_t write_proc(struct file *pfil, const char __user *pBuf, 
         size_t len, loff_t *p_off) 
{ 
    //Not relevant to this question 
} 

struct file_operations proc_fops = 
{ 
    .owner = THIS_MODULE, 
    .write = write_proc, 
    .read = read_proc, //This line causes the warning. 
}; 

编辑:所以答案是,我是一个白痴没有看到“int”与“ssize_t”。谢谢大家! Codenheim和Andrew Medico大致在同一时间得到了正确的答案,但我选择了Medico's,因为它对未来的访问者来说更迂腐和明显。

read_proc函数的返回类型(引发警告)与干净编译的函数不匹配。

static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off) 

static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off) 

ssize_tint可以是不同的尺寸。你的函数的返回类型应该是ssize_t

函数指针预计随着ssize_t返回类型的功能,但你给它的int

一个你需要一个ssize_t那里,不是int

+0

也不是这样。不警告的函数返回** s ** size_t。 – 2014-10-31 16:22:12

+0

@AndrewMedico - 这是一个错字。我首先将它作为size_t读取,但重点仍然相同。 – codenheim 2014-10-31 16:24:18

虽然有文件操作的工作只是根据这种结构遵循的规则

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 (*write) (struct file *, const char __user *, size_t, loff_t *); 
    ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); 
    ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); 
    ssize_t (*read_iter) (struct kiocb *, struct iov_iter *); 
    ssize_t (*write_iter) (struct kiocb *, struct iov_iter *); 
    int (*iterate) (struct file *, struct dir_context *); 
    unsigned int (*poll) (struct file *, struct poll_table_struct *); 
    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); 
    long (*compat_ioctl) (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 *, loff_t, loff_t, 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 (*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); 
    int (*check_flags)(int); 
    int (*flock) (struct file *, int, struct file_lock *); 
    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, size_t, unsigned int); 
    ssize_t (*splice_read)(struct file *, struct pipe_inode_info *, size_t, unsigned int); 
    int (*setlease)(struct file *, long arg, struct file_lock **); 
    long (*fallocate)(struct file *, int mode, loff_t offset, loff_t len); 
    int (*show_fdinfo)(struct seq_file *m, struct file *f); }; 

你可以在内核文档文档/文件系统/ vfs.txt这种结构,也可以使用标签VIM -t从file_operations中找到它内核源代码或者你可以看看头文件/include/linux/fs.h。

只是你的错误是你使用静态int而不是使用ssize_t的返回类型。