unknown field ‘ioctl’ specified in initializer 问题解决方法

本文介绍在2.6.36内核版本中,file_operations结构体中的ioctl函数指针被unlocked_ioctl和compat_ioctl替代的原因及具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实现驱动函数的时候,编译出现:unknown field ‘ioctl’ specified in initializer

原因是:在2.6.36内核上file_operations结构体发生了重大的改变,在kernel 2.6.36 中已经完全删除了struct file_operations 中的ioctl 函数指针,取而代之的是unlocked_ioctl 和compat_ioctl。

2.6.36版本以前
int (*ioctl)(struct inode*, struct file*, unsigned int, unsigned long);
2.6.36版本以后
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

例子:

static struct file_operations hello_flops = {
    .owner  =   THIS_MODULE,
    .open   =   hello_open,     
    .write  =   hello_write,
    .unlocked_ioctl     =   hello_ioctl,
//  .release=   hello_release,
};
k1@k1:~/work/CDemo$ gcc 170.c 170.c: In function ‘main’: 170.c:147:12: error: variable ‘phdr’ has initializer but incomplete type struct pseudo_udp_header phdr = { ^ 170.c:148:9: error: unknown field ‘src_ip’ specified in initializer .src_ip = ip_hdr->saddr, ^ 170.c:148:19: warning: excess elements in struct initializer .src_ip = ip_hdr->saddr, ^ 170.c:148:19: note: (near initialization for ‘phdr’) 170.c:149:9: error: unknown field ‘dst_ip’ specified in initializer .dst_ip = ip_hdr->daddr, ^ 170.c:149:19: warning: excess elements in struct initializer .dst_ip = ip_hdr->daddr, ^ 170.c:149:19: note: (near initialization for ‘phdr’) 170.c:150:9: error: unknown field ‘zero’ specified in initializer .zero = 0, ^ 170.c:150:17: warning: excess elements in struct initializer .zero = 0, ^ 170.c:150:17: note: (near initialization for ‘phdr’) 170.c:151:9: error: unknown field ‘protocol’ specified in initializer .protocol = IPPROTO_UDP, ^ 170.c:152:9: error: unknown field ‘length’ specified in initializer .length = udp_hdr->len ^ 170.c:152:19: warning: excess elements in struct initializer .length = udp_hdr->len ^ 170.c:152:19: note: (near initialization for ‘phdr’) 170.c:147:30: error: storage size of ‘phdr’ isn’t known struct pseudo_udp_header phdr = { ^ 170.c:284:24: error: variable ‘ack_phdr’ has initializer but incomplete type struct pseudo_udp_header ack_phdr = { ^ 170.c:285:21: error: unknown field ‘src_ip’ specified in initializer .src_ip = ack_ip->saddr, ^ 170.c:285:31: warning: excess elements in struct initializer .src_ip = ack_ip->saddr, ^ 170.c:285:31: note: (near initialization for ‘ack_phdr’) 170.c:286:21: error: unknown field ‘dst_ip’ specified in initializer .dst_ip = ack_ip->daddr, ^ 170.c:286:31: warning: excess elements in struct initializer .dst_ip = ack_ip->daddr, ^ 170.c:286:31: note: (near initialization for ‘ack_phdr’) 170.c:287:21: error: unknown field ‘zero’ specified in initializer .zero = 0, ^ 170.c:287:29: warning: excess elements in struct initializer .zero = 0, ^ 170.c:287:29: note: (near initialization for ‘ack_phdr’) 170.c:288:21: error: unknown field ‘protocol’ specified in initializer .protocol = IPPROTO_UDP, ^ 170.c:289:21: error: unknown field ‘length’ specified in initializer .length = ack_udp->len ^ 170.c:289:31: warning: excess elements in struct initializer .length = ack_udp->len ^ 170.c:289:31: note: (near initialization for ‘ack_phdr’) 170.c:284:42: error: storage size of ‘ack_phdr’ isn’t known struct pseudo_udp_header ack_phdr = { ^ k1@k1:~/work/CDemo$
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值