【问题标题】:why Variable has incomplete type 'struct file_operations' though importing fs.h?为什么变量在导入 fs.h 时具有不完整的类型“struct file_operations”?
【发布时间】:2020-12-09 12:42:53
【问题描述】:

我尝试导入 file_operations 的结构 并得到这个错误:

Variable has incomplete type 'struct file_operations'

我的进口是

#include <linux/kernel.h>   /* We're doing kernel work */
#include <linux/module.h>   /* Specifically, a module */
#include <linux/fs.h>       /* for register_chrdev */
#include "sys/types.h"

错误出现在 fops:

 struct file_operations Fops =
        {
                .owner    = THIS_MODULE, // Required for correct count of module usage. This prevents the module from being removed while used.
                .read           = device_read,
                .write          = device_write,
                .open           = device_open,
                .release        = NULL
        };

最少的代码:

#include <linux/kernel.h>   /* We're doing kernel work */
#include <linux/module.h>   /* Specifically, a module */
#include <linux/fs.h>       /* for register_chrdev */
#include "sys/types.h"

 struct file_operations Fops =
        {
                .owner    = THIS_MODULE, // Required for correct count of module usage. This prevents the module from being removed while used.
                .read           = device_read,
                .write          = device_write,
                .open           = device_open,
                .release        = NULL
        };

【问题讨论】:

  • 请提供minimal reproducible example。我认为将两个代码部分放在一个 .c 中可能会奏效,但您需要明确这一点。
  • @Yunnosch 添加
  • @KrishnaKanthYenumula 我对此表示怀疑。那里的答案基本上是“您需要包含具有该声明的标题。”。此处的哪个 OP 明确解释了他们考虑并采取了行动。至少需要一些细节,为什么 OP 标头虽然很有希望,但不能达到这个目的。
  • @KrishnaKanthYenumula 不
  • “完整代码”和minimal reproducible example之间有一个重要区别。请阅读链接信息并应用该概念。

标签: c linux fs chardev


【解决方案1】:

如果您将您的代码与https://docs.huihoo.com/doxygen/linux/kernel/3.7/structfile__operations.htmlfile_operations() 的定义进行比较,您还没有初始化很多字段,这可能就是抛出不完整错误的原因。

某些操作不是由驱动程序实现的。例如,司机 处理视频卡的不需要从目录中读取 结构体。 file_operations 结构中的对应条目 应设置为 NULL。

来源:https://tldp.org/LDP/lkmpg/2.4/html/c577.htm

如果你有 gcc 的 C99 扩展,你的方法通常是有效的

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多