error: storage size of ‘timeout’ isn’t known struct timeval timeout

本文介绍了一段使用C99标准编译的代码中遇到的关于timeval结构体声明错误的问题及解决过程。文章详细记录了通过查找不同路径下的time.h文件来定位问题所在,并最终发现是因为预编译宏定义导致的问题。

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

1  源代码

   #include<stdio.h>

#include<unistd.h>

//#include<sys/time.h>
#include<string.h>

#define __need_time_t

//#define __need_timespec

#include <time.h>
main()
{

  struct timeval timeout;
//  struct timespec elaped;
  ("dong\n");
  int i=10;
  printf("%d",i);
}
2 编译

kaola@kaola:~/dongwork$ gcc -std=c99 testtime.c
testtime.c:13:1: warning: return type defaults to ‘int’ [enabled by default]
 main()
 
testtime.c: In function ‘main’:
testtime.c:16:18: error: storage size of ‘timeout’ isn’t known
   struct timeval timeout;      

3 解决方法

sudo find / -name "time.h"


usr/lib/syslinux/com32/include/sys/time.h
/usr/lib/syslinux/com32/include/time.h
/usr/src/linux-headers-3.16.0-77/include/uapi/linux/time.h
/usr/src/linux-headers-3.16.0-77/include/linux/time.h
/usr/src/linux-headers-3.16.0-77/arch/hexagon/include/asm/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/mach-iop13xx/include/mach/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/mach-iop33x/include/mach/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/mach-davinci/include/mach/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/mach-iop32x/include/mach/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/plat-orion/include/plat/time.h
/usr/src/linux-headers-3.16.0-77/arch/arm/include/asm/mach/time.h
/usr/src/linux-headers-3.16.0-77/arch/blackfin/include/asm/time.h
/usr/src/linux-headers-3.16.0-77/arch/powerpc/include/asm/time.h
/usr/src/linux-headers-3.16.0-77/arch/x86/include/asm/time.h
/usr/src/linux-headers-3.16.0-77/arch/mips/include/asm/time.h
/usr/src/linux-headers-3.16.0-77-generic/include/config/netfilter/xt/match/time.h
/usr/src/linux-headers-3.16.0-77-generic/include/config/rtc/drv/hid/sensor/time.h
/usr/src/linux-headers-3.16.0-77-generic/include/config/printk/time.h
/usr/src/linux-headers-3.16.0-30-generic/include/config/netfilter/xt/match/time.h
/usr/src/linux-headers-3.16.0-30-generic/include/config/rtc/drv/hid/sensor/time.h
/usr/src/linux-headers-3.16.0-30-generic/include/config/printk/time.h
/usr/src/linux-headers-3.16.0-30/include/uapi/linux/time.h
/usr/src/linux-headers-3.16.0-30/include/linux/time.h
/usr/src/linux-headers-3.16.0-30/arch/hexagon/include/asm/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/mach-iop13xx/include/mach/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/mach-iop33x/include/mach/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/mach-davinci/include/mach/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/mach-iop32x/include/mach/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/plat-orion/include/plat/time.h
/usr/src/linux-headers-3.16.0-30/arch/arm/include/asm/mach/time.h
/usr/src/linux-headers-3.16.0-30/arch/blackfin/include/asm/time.h
/usr/src/linux-headers-3.16.0-30/arch/powerpc/include/asm/time.h
/usr/src/linux-headers-3.16.0-30/arch/x86/include/asm/time.h
/usr/src/linux-headers-3.16.0-30/arch/mips/include/asm/time.h
/usr/include/x86_64-linux-gnu/sys/time.h
/usr/include/x86_64-linux-gnu/bits/time.h
/usr/include/linux/time.h
/usr/include/time.h
/home/kaola/dongtool/m4-1.4.17/lib/sys/time.h
/home/kaola/dongtool/m4-1.4.17/lib/time.h
/home/kaola/dongwork/kaola-player-android-linux/extra/ffmpeg/libavutil/time.h
/home/kaola/dongwork/kaola-player-android-linux/Linux/include/libavutil/time.h
kaola@kaola:~/dongwork$ vim /usr/include/linux/time.h



3.1  分别查看


           

usr/include/linux/time.h
/usr/include/time.h

3.1.1 发现加上std= c99编译/usr/include/time.h用的是这个头文件,

#define __need_time_t
#define __need_timespec
只能有struct timespec 结构体的声明。
3.1.2 usr/include/linux/time.h
中有  struct timeval timeout;
//  struct timespec elaped;两者的生明



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

余额充值