>我尝试导入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
};
如果您将代码与 https://docs.huihoo.com/doxygen/linux/kernel/3.7/structfile__operations.html 中file_operations()
的定义进行比较,那么您还没有初始化许多字段,这可能就是抛出不完整错误的原因。
某些操作不是由驱动程序实现的。例如,驱动程序 处理视频卡不需要从目录中读取 结构。file_operations结构中的相应条目 应设置为 NULL。
来源 : https://tldp.org/LDP/lkmpg/2.4/html/c577.htm
通常,如果您有 gcc 的 C99 扩展名,您的方式是有效的