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

标签 linux kernel linux-device-driver 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]

据我所知,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_tint 的大小可能不同。您的函数的返回类型应为 ssize_t

关于linux - 为什么会出现不兼容的指针类型警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678924/

相关文章:

linux - Bash 函数忽略 set -e

linux - 运行 Linux 内核模块 (Hello World)

c - PCI_VDEVICE 和 PCI_DEVICE 有什么区别?

c - 内核用户 I/O 应用程序开发

linux - 什么是对延迟敏感的应用程序?

c++ - 从usb获取设备名称

linux - 编写一个shell脚本,在该行的前面的文本文件中打印每行的行号

c - 使用驱动程序 Hook 网络功能,高级概述?

c - 为什么系统调用位左移?

c - 为什么首先中断发生在注册时?