Tan*_*ir1 3 linux kernel device-driver linux-device-driver
我正在使用内核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]
Run Code Online (Sandbox Code Playgroud)
据我所知,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.
};
Run Code Online (Sandbox Code Playgroud)
编辑:所以答案是我是白痴,看不到“ int”和“ ssize_t”。谢谢大家!Codenheim和Andrew Medico大致在同一时间都给出了正确答案,但我选择Medico,因为它对未来的访客来说更加有趣和明显。
您的read_proc
函数的返回类型(引发警告)与完全编译的函数不匹配。
static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
Run Code Online (Sandbox Code Playgroud)
与
static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
Run Code Online (Sandbox Code Playgroud)
ssize_t
且int
大小可能不同。您函数的返回类型应为ssize_t
。