Linux驱动——error: unknown field 'ioctl' specified in initializer

本文针对初学者在驱动编程中遇到的“error: unknown field 'ioctl' specified in initializer”错误进行了详细解析。该问题源于内核版本升级导致的ioctl字段变更。文章提供了具体的解决方案,包括如何将旧版本的ioctl字段替换为新版本兼容的compat_ioctl字段。

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

驱动编程初学者经常会遇到资料与实际环境不匹的情况:

error: unknown field 'ioctl' specified in initializer

原因:

        由于2.6.36内核之后,去掉了原来的ioctl,添加两个新的成员,所以会出错,我的内核版本是:4.2.0-27-generic

解决方法:

        新成员:

           long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

           long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

         修改源文件中file_operations内.ioctl 改为 .compat_ioctl 即可。


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、付费专栏及课程。

余额充值