系统相关
首页 > 系统相关> > linux – 为什么存在不兼容的指针类型警告?

linux – 为什么存在不兼容的指针类型警告?

作者:互联网

我正在使用内核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]

据我所知,我的file_operations设置proc函数与设备函数相同.我可以读/写/ 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,是因为它对于未来的访客来说更加迂腐和明显.

解决方法:

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.

标签:device-driver,linux,kernel,linux-device-driver
来源: https://codeday.me/bug/20190722/1505884.html