unknown field 'ioctl' specified in initializer

本文记录了解决在Linux内核2.6.37.4版本中编写LED驱动模块遇到的编译错误及警告的过程。主要介绍了因内核版本更新导致的struct file_operations结构体中ioctl成员变更,并提供了相应的解决方案。

写个LED的驱动模块,编译一直不能通过,问题是ioctl是未知的,查看了下原始结构体定义才发现,内核中file_operations的结构较早的版本(大部分书上都还是用的ioctl,至少我手头上的都是)发生了变化(我用的是linux2.6.37.4)。

1、编译报错:error: unknown field 'ioctl' specified in initializer,

解释:struct file_operations结构体定义在include/linux/fs.h文件中,linux2.6.37中内核取消了原有的ioctl成员,添加来新的成员:

 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);    原来的ioctl 但是返回值变为long
long (*compat_ioctl) (struct file *, unsigned int, unsigned long); 内核空间64位,用户空间32位
函数指针compat_ioctl的作用的解释,本人没有验证仅供参考。

2>、编译警告:warning: initialization from incompatible pointer type

出现此种warnning 的原因  “不兼容的指针类型初始化”,你定义的函数类型与接口函数的类型不一样。在我的代码中是多了个参数struct inode *inode。返回值类型不匹配(long 定义成int型)也会产生同样的警告


弱弱说一句:对我这种小白来说碰到这个问题挺坑的。记录下,看着韦东山老师的书写的,结果找不到头文件抓狂,找到了发现根本木有那样的定义大哭,慢慢再找找尝试下。


申明下:这里指出韦东山老师并不是说老师的不好或者抨击,只是客观指出我学习中碰到的问题,也提醒和我一样的新手朋友们这里需要注意内核版本的问题。

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

余额充值