解决error: unknown type name ‘__u8’问题,认识__u8,__u16,__u32,__u64的大小及作用

本文介绍了解决Linux内核编译时遇到的未知类型‘__u8’错误的方法,通过包含<asm/types.h>头文件来解决该问题。此外,文章还探讨了在驱动开发中使用特定数据类型(如__u32)的重要性,以确保代码的可移植性和清晰性。

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

1、解决Linux error: unknown type name ‘__u8’问题
内核提供的数据类型都在头文件<asm/types.h>声明,可以在程序中加上#include <asm/types.h>头文件。详细类型可参考
2、认识__u8,__u16,__u32,__u64的大小及作用:
作者:时国怀
链接:https://www.zhihu.com/question/23223900/answer/23969589
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

驱动开发的原则:

能用__u32就最好用它,或者用u_int32_t之类的也可以,但不要直接用unsigned int等默认的数据类型。目的是让别人明白,你这个变量占多大内存

原因:

你不能确定你的代码未来只在一个平台上运行,它可能会被移植到非Linux平台(确实有很多人这么做),它可能运行在不同的CPU平台,比如64位环境,非x86环境。

因为C语言中并未明确规定int一定要占多少字节,只是规定了long >= int >= short,所以__u32就是告诉别人,这变量占4字节。

同样的:__iomem也是能加就加,这样当你的驱动被别人维护的时候,别人会明白,这块内存是用于IO的,可DMA的,而不是paged-memory。

说到底,这是一种良好的编码风格,确实,不加这些东西,对于编译和运行来说,可能是无任何影响的,但是,不写注释的代码一样也对编译和运行无影响,你敢一句注释都不写吗?

--------------------------------------

驱动中使用哪种类型最标准,这个没有正确答案,但建议是不使用C语言原始的数据类型(char/short/int/long),而是使用有明确字节大小的数据类型(u_int32_t/__u32/uint32_t/DWORD/UINT32均可)。保持代码风格的一致性即可。

Linux社区提倡开放,并不严格限制开发者必须用哪种类型,一切都只是建议使用

但有一种情况是例外的:开发标准库函数,比如自己实现strlen,那么返回值就必须是size_t,因为标准库是标准的。



/usr/include/linux/types.h:13:9: error: unknown type name ‘u32’ 13 | typedef u32 __kernel_dev_t; | ^~~ /usr/include/linux/types.h:20:9: error: unknown type name ‘u32’ 20 | typedef u32 nlink_t; | ^~~ /usr/include/linux/types.h:93:9: error: unknown type name ‘u8’ 93 | typedef u8 u_int8_t; | ^~ /usr/include/linux/types.h:94:9: error: unknown type name ‘s8’ 94 | typedef s8 int8_t; | ^~ /usr/include/linux/types.h:95:9: error: unknown type name ‘u16’ 95 | typedef u16 u_int16_t; | ^~~ /usr/include/linux/types.h:96:9: error: unknown type name ‘s16’ 96 | typedef s16 int16_t; | ^~~ /usr/include/linux/types.h:97:9: error: unknown type name ‘u32’ 97 | typedef u32 u_int32_t; | ^~~ /usr/include/linux/types.h:98:9: error: unknown type name ‘s32’ 98 | typedef s32 int32_t; | ^~~ /usr/include/linux/types.h:102:9: error: unknown type name ‘u8’ 102 | typedef u8 uint8_t; | ^~ /usr/include/linux/types.h:103:9: error: unknown type name ‘u16’ 103 | typedef u16 uint16_t; | ^~~ /usr/include/linux/types.h:104:9: error: unknown type name ‘u32’ 104 | typedef u32 uint32_t; | ^~~ /usr/include/linux/types.h:107:9: error: unknown type nameu64’ 107 | typedef u64 uint64_t; | ^~~ /usr/include/linux/types.h:108:9: error: unknown type nameu64’ 108 | typedef u64 u_int64_t; | ^~~ /usr/include/linux/types.h:109:9: error: unknown type name ‘s64’ 109 | typedef s64 int64_t; | ^~~ /usr/include/linux/types.h:125:9: error: unknown type nameu64’ 125 | typedef u64 sector_t; | ^~~ /usr/include/linux/types.h:126:9: error: unknown type nameu64’ 126 | typedef u64 blkcnt_t; | ^~~ /usr/include/linux/types.h:145:9: error: unknown type name ‘u32’ 145 | typedef u32 dma_addr_t; | ^~~ /usr/include/linux/types.h:155:9: error: unknown type name ‘u32’ 155 | typedef u32 phys_addr_t;
最新发布
08-09
/root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:29:5: error: type name requires a specifier or qualifier __uint(type, BPF_MAP_TYPE_HASH); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:29:12: error: a parameter list without types is only allowed in a function definition __uint(type, BPF_MAP_TYPE_HASH); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:30:5: error: type name requires a specifier or qualifier __uint(max_entries, 1024); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:30:25: error: expected identifier __uint(max_entries, 1024); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:30:5: error: duplicate member '__uint' __uint(max_entries, 1024); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:29:5: note: previous declaration is here __uint(type, BPF_MAP_TYPE_HASH); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:31:5: error: type name requires a specifier or qualifier __type(key, struct flow_key); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:31:17: error: expected identifier __type(key, struct flow_key); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:32:5: error: type name requires a specifier or qualifier __type(value, __u8); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:32:19: error: unexpected type name '__u8': expected identifier __type(value, __u8); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:32:12: error: a parameter list without types is only allowed in a function definition __type(value, __u8); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:32:5: error: duplicate member '__type' __type(value, __u8); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:31:5: note: previous declaration is here __type(key, struct flow_key); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:37:5: error: type name requires a specifier or qualifier __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_kern.c:37:12: error: a parameter list without types is only allowed in a function definition __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); ^ 16 errors generated.
07-22
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值