我正在使用内核 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 write 函数不会抛出警告,只会抛出警告。我做错了什么?
/*****************************************************************************/
//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,因为它对 future 的访问者来说更迂腐和明显。
最佳答案
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_t
和 int
的大小可能不同。您的函数的返回类型应为 ssize_t
。
关于linux - 为什么会出现不兼容的指针类型警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678924/