error: field `list' has incomplete type的解决!:) [复制链接] 00

本文介绍了解决Linux 2.6.14.2内核编译时出现的partitions.h文件第61行错误的方法。通过查找并引入正确的头文件linux/list.h,最终成功解决了编译问题。

【遇到编译问题,十有八九是没有包含头文件!!】



本贴的目的是帮比我还菜的小鸟遇到这个问题好解决些,写完这个我
想在google或者baidu上搜partitions.h:61: error: field `list' has incomplete type
这样的关键字,应该能找到这贴了吧.

我今天在编译linux2.6.14.2的内核的时候,遇到了这个问题,
但是在google和baidu上找,都是提问的,就没有理睬过,那好
我就自己找答案:
主要是针对s3c2410的内核移植,
在我都培植好后,开始make zImage,刚开始就遇到:
  1. In file included from arch/arm/mach-s3c2410/devs.c:20:
  2. include/linux/mtd/partitions.h:61: error: field `list' has incomplete type
  3. make[1]: *** [arch/arm/mach-s3c2410/devs.o] Error 1
  4. make: *** [arch/arm/mach-s3c2410] Error 2
复制代码

开始用partitions.h:61: error: field `list' has incomplete type狂搜
关键字,结果好几位都问,但是没答案,自己来,看到:
  1. include/linux/mtd/partitions.h:61: error: field `list' has incomplete type
复制代码

这,我想我们大家都能看明白,就是在说:partitions.h头文件中的61行有问题,
然后我们打开partitions.h文件,找到61行:
  1. 60 struct mtd_part_parser {
  2. 61         struct list_head list;
  3. 62         struct module *owner;
  4. 63         const char *name;
  5. 64         int (*parse_fn)(struct mtd_info *, struct mtd_partition **, unsigned long);
  6. 65 };
复制代码


一看list是个结构,而且错误中还提示了,list是一个不完整的类型,考虑struct list_head类型
是不是没有在文件中定义过,结果没有,查看partitions.h引用的头文件中有没有定义:
  1. grep struct list_head path/types.h
复制代码

也没有定义,开始怀疑,是不是没有引用适当的头文件呢?
开始cscope...
终于发现struct list_head这个结构的定义在linux/list.h下,如此找到了定义的地方,把
partitions.h文件的最前面加上#include <linux/list.h>
重新编译,成功!
arp_final.c:16:0: warning: "IFNAMSIZ" redefined #define IFNAMSIZ 16 ^ In file included from arp_final.c:7:0: /usr/include/net/if.h:129:0: note: this is the location of the previous definition # define IFNAMSIZ IF_NAMESIZE ^ arp_final.c:68:8: error: redefinition of ‘struct ifreq’ struct ifreq { ^ In file included from arp_final.c:7:0: /usr/include/net/if.h:126:8: note: originally defined here struct ifreq ^ arp_final.c:69:10: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘.’ token char ifr_name[IFNAMSIZ]; ^ arp_final.c: In function ‘if_nametoindex’: arp_final.c:125:16: error: ‘struct ifreq’ has no member named ‘ifr_ifrn’ strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); ^ arp_final.c:134:15: error: ‘struct ifreq’ has no member named ‘ifr_ifru’ return ifr.ifr_ifru.ifru_metric; ^ arp_final.c: In function ‘get_local_mac’: arp_final.c:146:16: error: ‘struct ifreq’ has no member named ‘ifr_ifrn’ strncpy(ifr.ifr_name, ifname, IFNAMSIZ); ^ arp_final.c:153:20: error: ‘struct ifreq’ has no member named ‘ifr_ifru’ memcpy(mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, MAC_LENGTH); ^ arp_final.c: In function ‘get_local_ip’: arp_final.c:166:16: error: ‘struct ifreq’ has no member named ‘ifr_ifrn’ strncpy(ifr.ifr_name, ifname, IFNAMSIZ); ^ arp_final.c:173:57: error: ‘struct ifreq’ has no member named ‘ifr_ifru’ struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr; ^ arp_final.c: In function ‘build_arp_packet’: arp_final.c:191:25: error: ‘ETH_P_IP’ undeclared (first use in this function) arp->ar_pro = htons(ETH_P_IP); ^ arp_final.c:191:25: note: each undeclared identifier is reported only once for each function it appears in arp_final.c: In function ‘send_arp’: arp_final.c:230:12: error: variable ‘dest’ has initializer but incomplete type struct sockaddr_ll dest = { ^ arp_final.c:231:9: error: unknown field ‘sll_family’ specified in initializer .sll_family = AF_PACKET, ^ arp_final.c:232:9: error: unknown field ‘sll_protocol’ specified in initializer .sll_protocol = htons(ETH_P_ARP), ^ arp_final.c:232:25: warning: excess elements in struct initializer .sll_protocol = htons(ETH_P_ARP), ^ arp_final.c:232:25: note: (near initialization for ‘dest’) arp_final.c:233:9: error: unknown field ‘sll_ifindex’ specified in initializer .sll_ifindex = if_nametoindex(ifname), ^ arp_final.c:233:24: warning: excess elements in struct initializer .sll_ifindex = if_nametoindex(ifname), ^ arp_final.c:233:24: note: (near initialization for ‘dest’) arp_final.c:234:9: error: unknown field ‘sll_halen’ specified in initializer .sll_halen = ETH_ALEN, ^ arp_final.c:24:18: warning: excess elements in struct initializer #define ETH_ALEN 6 ^ arp_final.c:234:22: note: in expansion of macro ‘ETH_ALEN’ .sll_halen = ETH_ALEN, ^ arp_final.c:24:18: note: (near initialization for ‘dest’) #define ETH_ALEN 6 ^ arp_final.c:234:22: note: in expansion of macro ‘ETH_ALEN’ .sll_halen = ETH_ALEN, ^ arp_final.c:235:9: error: unknown field ‘sll_addr’ specified in initializer .sll_addr = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} // 广播地址 ^ arp_final.c:235:21: error: extra brace group at end of initializer .sll_addr = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} // 广播地址 ^ arp_final.c:235:21: note: (near initialization for ‘dest’) arp_final.c:235:21: warning: excess elements in struct initializer arp_final.c:235:21: note: (near initialization for ‘dest’) arp_final.c:230:24: error: storage size of ‘dest’ isn’t known struct sockaddr_ll dest = { ^ arp_final.c: In function ‘find_or_create_node’: arp_final.c:275:5: warning: implicit declaration of function ‘list_for_each’ [-Wimplicit-function-declaration] list_for_each(pos, &arp_result_head) { ^ arp_final.c:275:42: error: expected ‘;’ before ‘{’ token list_for_each(pos, &arp_result_head) { ^ arp_final.c: In function ‘cleanup_thread’: arp_final.c:373:9: warning: implicit declaration of function ‘list_for_each_safe’ [-Wimplicit-function-declaration] list_for_each_safe(pos, next, &arp_result_head) { ^ arp_final.c:373:57: error: expected ‘;’ before ‘{’ token list_for_each_safe(pos, next, &arp_result_head) { ^ arp_final.c: In function ‘print_arp_table’: arp_final.c:403:42: error: expected ‘;’ before ‘{’ token list_for_each(pos, &arp_result_head) { 报错如上
08-28
huaxi@ubuntu:~/c_projects/kernel_module$ make clean make -C /lib/modules/4.15.0-142-generic/build M=/home/huaxi/c_projects/kernel_module clean make[1]: Entering directory '/usr/src/linux-headers-4.15.0-142-generic' CLEAN /home/huaxi/c_projects/kernel_module/.tmp_versions make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-142-generic' huaxi@ubuntu:~/c_projects/kernel_module$ make make -C /lib/modules/4.15.0-142-generic/build M=/home/huaxi/c_projects/kernel_module modules make[1]: Entering directory '/usr/src/linux-headers-4.15.0-142-generic' CC [M] /home/huaxi/c_projects/kernel_module/ping_stat.o /home/huaxi/c_projects/kernel_module/ping_stat.c:138:21: error: variable ‘ping_fops’ has initializer but incomplete type static const struct proc_ops ping_fops = { ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:139:5: error: unknown field ‘proc_open’ specified in initializer .proc_open = ping_proc_open, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:139:21: warning: excess elements in struct initializer .proc_open = ping_proc_open, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:139:21: note: (near initialization for ‘ping_fops’) /home/huaxi/c_projects/kernel_module/ping_stat.c:140:5: error: unknown field ‘proc_read’ specified in initializer .proc_read = seq_read, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:140:21: warning: excess elements in struct initializer .proc_read = seq_read, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:140:21: note: (near initialization for ‘ping_fops’) /home/huaxi/c_projects/kernel_module/ping_stat.c:141:5: error: unknown field ‘proc_lseek’ specified in initializer .proc_lseek = seq_lseek, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:141:21: warning: excess elements in struct initializer .proc_lseek = seq_lseek, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:141:21: note: (near initialization for ‘ping_fops’) /home/huaxi/c_projects/kernel_module/ping_stat.c:142:5: error: unknown field ‘proc_release’ specified in initializer .proc_release = single_release, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:142:21: warning: excess elements in struct initializer .proc_release = single_release, ^ /home/huaxi/c_projects/kernel_module/ping_stat.c:142:21: note: (near initialization for ‘ping_fops’) /home/huaxi/c_projects/kernel_module/ping_stat.c: In function ‘ping_stat_init’: /home/huaxi/c_projects/kernel_module/ping_stat.c:152:56: error: passing argument 4 of ‘proc_create’ from incompatible pointer type [-Werror=incompatible-pointer-types] proc_entry = proc_create("ping_stats", 0444, NULL, &ping_fops); ^ In file included from /home/huaxi/c_projects/kernel_module/ping_stat.c:3:0: ./include/linux/proc_fs.h:32:24: note: expected ‘const struct file_operations *’ but argument is of type ‘const struct proc_ops *’ struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops); ^ cc1: some warnings being treated as errors scripts/Makefile.build:337: recipe for target '/home/huaxi/c_projects/kernel_module/ping_stat.o' failed make[2]: *** [/home/huaxi/c_projects/kernel_module/ping_stat.o] Error 1 Makefile:1584: recipe for target '_module_/home/huaxi/c_projects/kernel_module' failed make[1]: *** [_module_/home/huaxi/c_projects/kernel_module] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-142-generic' Makefile:8: recipe for target 'all' failed make: *** [all] Error
08-19
In file included from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/src/IMU_Processing.cpp:13: /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/IMU_Processing.h:52:12: error: field ‘fout_imu’ has incomplete type ‘std::ofstream’ {aka ‘std::basic_ofstream<char>’} 52 | ofstream fout_imu; | ^~~~~~~~ In file included from /usr/include/c++/9/ios:38, from /usr/include/c++/9/istream:38, from /usr/include/c++/9/sstream:38, from /usr/include/c++/9/complex:45, from /usr/include/eigen3/Eigen/Core:96, from /usr/include/eigen3/Eigen/Dense:1, from /usr/include/eigen3/Eigen/Eigen:1, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/IMU_Processing.h:16, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/src/IMU_Processing.cpp:13: /usr/include/c++/9/iosfwd:119:11: note: declaration of ‘std::ofstream’ {aka ‘class std::basic_ofstream<char>’} 119 | class basic_ofstream; | ^~~~~~~~~~~~~~ In file included from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/LIVMapper.h:16, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/src/LIVMapper.cpp:13: /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/IMU_Processing.h:52:12: error: field ‘fout_imu’ has incomplete type ‘std::ofstream’ {aka ‘std::basic_ofstream<char>’} 52 | ofstream fout_imu; | ^~~~~~~~ In file included from /usr/include/c++/9/ios:38, from /usr/include/c++/9/istream:38, from /usr/include/c++/9/sstream:38, from /usr/include/c++/9/complex:45, from /usr/include/eigen3/Eigen/Core:96, from /usr/include/eigen3/Eigen/Dense:1, from /usr/include/eigen3/Eigen/Eigen:1, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/IMU_Processing.h:16, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/include/LIVMapper.h:16, from /home/lixing/1/Fast-LIO2/src/fast-livo2-native/src/LIVMapper.cpp:13: /usr/include/c++/9/iosfwd:119:11: note: declaration of ‘std::ofstream’ {aka ‘class std::basic_ofstream<char>’} 119 | class basic_ofstream; | ^~~~~~~~~~~~~~ In file included from /usr/include/boost/preprocessor/tuple/elem.hpp:23, from /usr/include/boost/preprocessor/arithmetic/add.hpp:21, from /usr/include/boost/mpl/aux_/preprocessor/def_params_tail.hpp:66, from /usr/include/boost/mpl/aux_/na_spec.hpp:28, from /usr/include/boost/mpl/next_prior.hpp:18, from /usr/include/boost/mpl/next.hpp:17, from /usr/include/boost/mpl/list/aux_/push_front.hpp:18, from /usr/include/boost/mpl/list/list0.hpp:19, from /usr/include/boost/mpl/list/list10.hpp:18, from /usr/include/boost/mpl/list/list20.hpp:18, from /usr/include/boost/mpl/list.hpp:36, from /usr/include/boost/math/policies/policy.hpp:9, from /usr/include/boost/math/policies/error_handling.hpp:21, from /usr/include/boost/math/special_functions/round.hpp:14, from /opt/ros/noetic/include/ros/time.h:58, from /opt/ros/noetic/include/ros/ros.h:38, from /home/lixing/1/Fast-LIO2/src/FAST_LIO_SLAM/FAST-LIO/src/laserMapping.cpp:44: /home/lixing/1/Fast-LIO2/src/FAST_LIO_SLAM/FAST-LIO/src/preprocess.h:51:6: warning: ‘using uint16_t = uint16_t’ is deprecated: use std::uint16_t instead of pcl::uint16_t [-Wdeprecated-declarations] 51 | (uint16_t, ring, ring) | ^~~~~~~~ In file included from /usr/include/pcl-1.10/pcl/point_types.h:42, from /home/lixing/1/Fast-LIO2/src/FAST_LIO_SLAM/FAST-LIO/include/common_lib.h:6, from /home/lixing/1/Fast-LIO2/src/FAST_LIO_SLAM/FAST-LIO/src/IMU_Processing.hpp:11, from /home/lixing/1/Fast-LIO2/src/FAST_LIO_SLAM/FAST-LIO/src/laserMapping.cpp:46: /usr/include/pcl-1.10/pcl/pcl_macros.h:94:9: note: declared here 94 | using uint16_t [[deprecated("use std::uint16_t instead of pcl::uint16_t")]] = std::uint16_t; | ^~~~~~~~ make[2]: *** [fast-livo2-native/CMakeFiles/imu_proc.dir/build.make:63:fast-livo2-native/CMakeFiles/imu_proc.dir/src/IMU_Processing.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:7528:fast-livo2-native/CMakeFiles/imu_proc.dir/all] 错误 2 make[1]: *** 正在等待未完成的任务.... [ 94%] Linking CXX shared library /home/lixing/1/Fast-LIO2/devel/lib/libpre.so [ 94%] Built target pre make[2]: *** [fast-livo2-native/CMakeFiles/laser_mapping.dir/build.make:63:fast-livo2-native/CMakeFiles/laser_mapping.dir/src/LIVMapper.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:7501:fast-livo2-native/CMakeFiles/laser_mapping.dir/all] 错误 2 [ 95%] Linking CXX shared library /home/lixing/1/Fast-LIO2/devel/lib/libvio.so [ 95%] Built target vio [ 96%] Linking CXX shared library /home/lixing/1/Fast-LIO2/devel/lib/liblio.so [ 96%] Built target lio [ 97%] Linking CXX executable /home/lixing/1/Fast-LIO2/devel/lib/fast_lio/fastlio_mapping [ 98%] Built target fastlio_mapping make: *** [Makefile:141:all] 错误 2 Invoking "make -j12 -l12" failed lixing@lixing:~/1/Fast-LIO2$
最新发布
09-04
<think>我们正在处理一个C/C++编译错误:结构体成员类型为void。错误信息是:error: field has incomplete type 'void' in widget.h line 19。 根据错误信息,问题发生在widget.h文件的第19行,我们在结构体中定义了一个类型为void的成员。在C/C++中,void类型是不完整的类型,不能用于定义非指针的成员变量(因为void类型没有大小,编译器无法确定该成员占用的空间)。 解决方案: 1. 检查第19行的结构体定义,确认成员变量是否真的需要定义为void类型。通常,void类型不能直接作为成员变量的类型(除非是指针,但这里错误提示是incomplete type,所以很可能不是指针)。 2. 如果这个成员原本意图是通用指针,那么应该使用void*类型(指针类型,大小是确定的)。 3. 如果这个成员原本意图是其他类型,那么可能是拼写错误,应该修正为正确的类型。 例如,错误代码可能类似于: struct MyStruct { void myMember; // 错误:void是不完整类型 }; 修正为: struct MyStruct { void* myMember; // 正确:使用void指针 }; 或者,如果本意不是指针,而是其他具体类型,则替换为具体类型,如int, char等。 因此,我们需要检查widget.h的第19行,将void改为void*(如果确实需要存储任意类型的指针)或改为其他具体类型。 另外,注意错误信息中提到的“incomplete type”是指类型不完整,除了void,还有前向声明的结构体/类(没有定义)等也会导致同样的问题。但这里明确是void,所以问题就是使用了void作为非指针成员的类型。 步骤: 1. 打开widget.h文件,找到第19行。 2. 查看该行定义的结构体成员。 3. 修改成员类型:如果确实需要通用指针,则改为void*;否则改为具体类型。 如果问题仍然存在,请检查是否有其他成员使用了不完整类型(比如某个结构体只声明了但未定义),但当前错误信息明确指向void,所以先解决void的问题。 参考引用中提到了Qt错误,但我们的错误是标准C++错误,与Qt无关。不过,引用[2]中提到的“allocation of incomplete type”也是类似的不完整类型问题,但这里我们的错误是结构体成员类型不完整(void),所以原理相同。 因此,修改方法就是确保结构体成员的类型是完整的(如基本类型、指针、已定义的结构体/类等)。 相关问题: 1. 为什么void类型不能作为结构体的成员类型? 2. 在C/C++中,还有哪些情况会导致“incomplete type”错误? 3. 如何正确定义一个可以存储任意类型数据的结构体成员?</think>### C/C++编译错误:结构体成员类型为void **错误分析** 在C/C++中,`void`类型是**不完整类型(incomplete type)**,编译器无法确定其内存大小。当结构体成员被声明为`void`类型时(如`widget.h`第19行的代码),会导致编译错误: ```c struct MyStruct { void invalidMember; // 错误:field has incomplete type 'void' }; ``` 因为结构体需要为每个成员分配确定的内存空间,而`void`类型没有大小信息。 --- ### 解决方案 #### 1. **使用`void*`指针代替`void`** 若需存储通用数据指针(最常见场景): ```c struct MyStruct { void* validMember; // 正确:指针大小固定(通常4/8字节) }; ``` 使用时通过类型转换存取数据: ```c int num = 42; MyStruct obj; obj.validMember = &num; // 存储int指针 printf("%d", *(int*)obj.validMember); // 输出:42 ``` #### 2. **替换为具体类型** 若不需要通用指针,直接使用具体类型: ```c struct MyStruct { int validMember; // 明确指定类型(如int、float等) }; ``` #### 3. **联合体(Union)实现多类型存储** 若需存储多种类型数据: ```c struct MyStruct { enum { INT, FLOAT, STR } type; union { int i; float f; char* s; } data; // 联合体共享内存空间 }; ``` --- ### 错误原因详解 1. **不完整类型限制** C/C++标准规定结构体/类成员必须是完整类型(如`int`、`float`或已定义的结构体)。`void`因无法确定内存尺寸被禁止使用[^1]。 2. **例外情况** - 允许**指向不完整类型的指针**(如`void*`、`struct Undefined*`)。 - 允许**前向声明结构体**(但需在定义结构体前补全声明): ```c struct MyStruct; // 前向声明(不完整类型) struct Container { struct MyStruct* ptr; // 允许:指针大小固定 }; ``` --- ### 验证修复 修改`widget.h`第19行后重新编译: ```bash g++ -c widget.cpp -o widget.o # 应不再报错 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值