C 分配结构文件\u操作字段时从不兼容的指针类型初始化
我正在编写一个Linux设备驱动器,我不明白为什么会收到这个警告C 分配结构文件\u操作字段时从不兼容的指针类型初始化,c,linux,linux-kernel,kernel,kernel-module,C,Linux,Linux Kernel,Kernel,Kernel Module,我正在编写一个Linux设备驱动器,我不明白为什么会收到这个警告 error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .write = file_write, 这是我创建的代码,当我使用make命令时,我得到了上面的错误。我尝试将ssize\u t更改为int,但仍然得到相同的错误 #include <linux/module.h> #include
error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
.write = file_write,
这是我创建的代码,当我使用make命令时,我得到了上面的错误。我尝试将ssize\u t
更改为int
,但仍然得到相同的错误
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/random.h>
MODULE_LICENSE("NIKS");
static char msg[100]={0};
static short readPos =0;
static int file_open(struct inode * , struct file *);
static int file_release(struct inode * , struct file *);
static ssize_t file_read(struct file *, char *, size_t,loff_t * );
static ssize_t file_write(struct file *, char *, size_t,loff_t * );
static struct file_operations fo =
{
.read = file_read,
.open = file_open,
.write = file_write,
.release = file_release,
};
int init_module(void)
{
int t = register_chrdev(150,"encdev",&fo);
if(t<0)
{
printk("error");
}
else
{
printk("success");
}
return t;
}
void cleanup_module(void)
{
unregister_chrdev(150,"encdev");
}
static int file_open(struct inode *in , struct file *fil)
{
return 0;
}
static ssize_t file_read(struct file *fil, char *buf, size_t len,loff_t *off )
{
short count=0;
printk("here %d",msg[0]!=0);
while(len && (msg[readPos]!=0))
{
printk("read");
put_user(msg[readPos],buf++);
count++;
len--;
readPos++;
}
return count;
}
static ssize_t file_write(struct file *fil, char *buf, size_t len,loff_t *off )
{
short ind = 0;
short count =0;
memset(msg,0,100);
readPos = 0;
int i =0;
char bytes[16];
char rand;
while(i<16)
{
get_random_bytes(&rand, sizeof(rand));
bytes[i]=rand;
++count;
i++;
}
int _len = len+16;
while(_len>0 || count%16){
if(ind<16) msg[ind] = bytes[ind];
else{
msg[ind] = (_len>0?buf[ind-16]:'@')^msg[ind-16];
}
++ind;
--_len;
++count;
}
return count;
}
static int file_release(struct inode *in , struct file *fil)
{
printk("done");
return 0;
}
#包括
#包括
#包括
#包括
#包括
模块许可证(“NIKS”);
静态字符msg[100]={0};
静态短readPos=0;
静态整型文件(结构索引节点*,结构文件*);
静态int文件发布(struct inode*,struct file*);
静态ssize_t file_read(结构文件*,字符*,大小,loff_t*);
静态ssize_t file_write(struct file*,char*,size_t,loff_t*);
静态结构文件\u操作fo=
{
.read=文件读取,
.open=文件\u打开,
.write=文件写入,
.release=文件发布,
};
int init_模块(void)
{
int t=寄存器(150,“encdev”、&fo);
如果(t的read
和write
函数指针的正确类型为:
因此,在您的模块中,适当的签名是:
static ssize_t file_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t file_write(struct file *, const char __user *, size_t, loff_t *);
// ...
static ssize_t file_read(struct file *fil, char __user *buf, size_t len,loff_t *off)
{
// ...
static ssize_t file_write(struct file *fil, const char __user *buf, size_t len, loff_t *off)
{
缺少的重要部分是const
限定符,但同样重要的是要记住,从用户空间传递的任何指针都应该标记有\uu user
注释(即使编译器在您忘记它时不会警告您,但它只用于静态检查)
在其他statement之后还有几个声明,这在C90中是无效的。编译器将警告您这些声明,并按照警告将它们移动到函数体的顶部。通常这些错误基于您使用的编译器命令行、编译器版本、目标、环境等。您还可以包括编译器吗命令行和问题中的相关详细信息。欢迎使用堆栈溢出
static ssize_t file_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t file_write(struct file *, const char __user *, size_t, loff_t *);
// ...
static ssize_t file_read(struct file *fil, char __user *buf, size_t len,loff_t *off)
{
// ...
static ssize_t file_write(struct file *fil, const char __user *buf, size_t len, loff_t *off)
{