移植代码遇编译报错:variable has incomplete type 'struct stat'求解
解决
struct stat不完全类型编译错误 问题原因
编译报错variable has incomplete type 'struct stat',是因为编译器无法找到struct stat的完整定义——这个结构体的声明在<sys/stat.h>头文件中,当前代码未包含该头文件。
解决步骤
- 在代码文件的最开头(所有变量、函数定义之前)添加头文件包含:
#include <sys/stat.h> - 另外,代码中使用了
malloc函数,该函数的声明在<stdlib.h>中,若未包含也可能引发警告或错误,建议一并添加:#include <stdlib.h>
验证修改
修改后的代码开头部分类似这样:
#include <sys/stat.h> #include <stdlib.h> // 其他必要的头文件 int srcfd, destfd; int nread; char ifc[PROPERTY_VALUE_MAX]; char *pbuf; char *sptr; struct stat sb; if (stat(config_file, &sb) != 0) return -1; pbuf = malloc(sb.st_size + PROPERTY_VALUE_MAX); if (!pbuf) return 0;
重新编译即可解决该错误。
内容的提问来源于stack exchange,提问作者jethrob
相关产品推荐
相关产品推荐

