error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .read = dev_read, Linux
error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .read = dev_read, Linux
我在 linux 的内核代码中遇到以下文件操作错误。
你能帮我确定这里出了什么问题吗?
代码:
struct GraphData{
unsigned long addr;
long time;
};
static int position = 0;
struct GraphData buffer[BUFFER_SIZE];
static int dev_open(struct inode *in,struct file *f);
static ssize_t dev_read(struct file *f,struct GraphData *out,size_t count);
static int dev_release(struct inode *inod,struct file *f);
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.release = dev_release
};
static int dev_open(struct inode *in, struct file *f){
printk(KERN_INFO " Device has been opened \n");
return 0;
}
static int dev_release(struct inode *in, struct file *f){
printk(KERN_INFO "Device successfully closed\n");
return 0;
}
static ssize_t dev_read(struct file *f,struct GraphData *out, size_t count)
{
int ret=0,i=0;
for(i=0;i<BUFFER_SIZE;i++)
{
count = copy_to_user(&out[i],&buffer[i],sizeof(buffer[i]));
if(count<0)
{
printk(KERN_ALERT "Copy!");
ret = -1;
}
}
return count;
}
内核程序有文件操作,设备可以传递一个缓冲区并将数据存储到缓冲区中
用户程序
#define SIZE 500
struct Buffer
{
unsigned long addr;
long time;
};
int main()
{
struct Buffer buf[SIZE];
int i=0;
int errorFlag = 0;
int fp = open("/proc/Probe",O_RDONLY);
errorFlag = read(fp,buf,1);
printf("Page Fault Address Time\n");
for(i=0;i<SIZE;i++)
{
//printf("Read worked the Page fault address is 0x%lx caught at time %ld\n",buf[i].address,buf[i].time);
printf("0x%lx %ld\n",buf[i].addr,buf[i].time);
}
close(fp);
return 0;
}
用户程序打开文件描述符,从设备中读取并将缓冲区传递给内核设备以复制到其中。
错误:
错误:从不兼容的指针类型初始化 [-Werror=incompatible-pointer-types]
.read = dev_read,
/home/csvb/OS/assign4/hello2.c:36:12: 注意:(接近‘fops.read’的初始化)
cc1:一些警告被视为错误
Could you help me identify what's going wrong here? [...]
Error: error: initialization from incompatible pointer type
[-Werror=incompatible-pointer-types] .read = dev_read,
/home/csvb/OS/assign4/hello2.c:36:12: note: (near initialization for
‘fops.read’) cc1: some warnings being treated as errors
诊断对我来说似乎很清楚。它将问题定位到文件 /home/csvb/OS/assign4/hello2.c 的第 36 行,在由 fops
标识的 object 的成员 read
的初始化中;具体来说,指定初始化.read = dev_read
。 "Initialization from incompatible pointer type" 的意思就是它所说的:dev_read
的类型与 fops.read
的类型不兼容。这样的问题在默认情况下应该发出警告,但编译选项指示它会导致错误。
一般来说,您应该检查文档,或者至少 headers,了解您所针对的内核版本,以确定预期的内容,但即使不这样做也是安全的假设 none 个参数的类型应为 struct GraphData *
。那肯定是不兼容了。
内核设备 driver 接口的这个特殊细节在内核 5.7 的最新预发布版中是相同的,因为它至少可以追溯到内核 2.4,但是,即使不知道哪个内核版本你的目标是,我有信心说字符设备的读取功能应该有这个签名:
ssize_t dev_read_fn(struct file *, char __user *, size_t, loff_t *);
此外,第二个参数的类型与您的函数不同,它需要 四个 个参数,而不是三个。
我在 linux 的内核代码中遇到以下文件操作错误。
你能帮我确定这里出了什么问题吗?
代码:
struct GraphData{
unsigned long addr;
long time;
};
static int position = 0;
struct GraphData buffer[BUFFER_SIZE];
static int dev_open(struct inode *in,struct file *f);
static ssize_t dev_read(struct file *f,struct GraphData *out,size_t count);
static int dev_release(struct inode *inod,struct file *f);
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.release = dev_release
};
static int dev_open(struct inode *in, struct file *f){
printk(KERN_INFO " Device has been opened \n");
return 0;
}
static int dev_release(struct inode *in, struct file *f){
printk(KERN_INFO "Device successfully closed\n");
return 0;
}
static ssize_t dev_read(struct file *f,struct GraphData *out, size_t count)
{
int ret=0,i=0;
for(i=0;i<BUFFER_SIZE;i++)
{
count = copy_to_user(&out[i],&buffer[i],sizeof(buffer[i]));
if(count<0)
{
printk(KERN_ALERT "Copy!");
ret = -1;
}
}
return count;
}
内核程序有文件操作,设备可以传递一个缓冲区并将数据存储到缓冲区中
用户程序
#define SIZE 500
struct Buffer
{
unsigned long addr;
long time;
};
int main()
{
struct Buffer buf[SIZE];
int i=0;
int errorFlag = 0;
int fp = open("/proc/Probe",O_RDONLY);
errorFlag = read(fp,buf,1);
printf("Page Fault Address Time\n");
for(i=0;i<SIZE;i++)
{
//printf("Read worked the Page fault address is 0x%lx caught at time %ld\n",buf[i].address,buf[i].time);
printf("0x%lx %ld\n",buf[i].addr,buf[i].time);
}
close(fp);
return 0;
}
用户程序打开文件描述符,从设备中读取并将缓冲区传递给内核设备以复制到其中。
错误: 错误:从不兼容的指针类型初始化 [-Werror=incompatible-pointer-types] .read = dev_read, /home/csvb/OS/assign4/hello2.c:36:12: 注意:(接近‘fops.read’的初始化) cc1:一些警告被视为错误
Could you help me identify what's going wrong here? [...] Error: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .read = dev_read, /home/csvb/OS/assign4/hello2.c:36:12: note: (near initialization for ‘fops.read’) cc1: some warnings being treated as errors
诊断对我来说似乎很清楚。它将问题定位到文件 /home/csvb/OS/assign4/hello2.c 的第 36 行,在由 fops
标识的 object 的成员 read
的初始化中;具体来说,指定初始化.read = dev_read
。 "Initialization from incompatible pointer type" 的意思就是它所说的:dev_read
的类型与 fops.read
的类型不兼容。这样的问题在默认情况下应该发出警告,但编译选项指示它会导致错误。
一般来说,您应该检查文档,或者至少 headers,了解您所针对的内核版本,以确定预期的内容,但即使不这样做也是安全的假设 none 个参数的类型应为 struct GraphData *
。那肯定是不兼容了。
内核设备 driver 接口的这个特殊细节在内核 5.7 的最新预发布版中是相同的,因为它至少可以追溯到内核 2.4,但是,即使不知道哪个内核版本你的目标是,我有信心说字符设备的读取功能应该有这个签名:
ssize_t dev_read_fn(struct file *, char __user *, size_t, loff_t *);
此外,第二个参数的类型与您的函数不同,它需要 四个 个参数,而不是三个。