C/C++ error: storage size of ‘tv’ isn’t known错误解决方法

来源:互联网 发布:淘宝食品如何备案 编辑:程序博客网 时间:2024/06/06 15:35

使用时间函数 gettimeofday() 时报错 “error: storage size of ‘tv’ isn’t known”,代码如下:

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/time.h> int main(){struct tim tv; struct timezone tz;gettimeofday (&tv , &tz);printf("===> tv_sec1: %ld\n", tv.tv_sec);printf("===> tv_usec1: %ld\n", tv.tv_usec);sleep(3);gettimeofday (&tv , &tz);printf("===> tv_sec2: %ld\n", tv.tv_sec);printf("===> tv_usec2: %ld\n", tv.tv_usec);return 0;}

使用 gcc 编译报错:

test.c: In function ‘main’:test.c:10: error: storage size of ‘tv’ isn’t known

于是仔细查了 gettimeofday 函数,发现函数的定义与我记得的有出入,正确的如下:

#include <sys/time.h>int gettimeofday(struct timeval *tv, struct timezone *tz);
参考:

http://man7.org/linux/man-pages/man2/settimeofday.2.html


于是把

struct tim tv;

换成

struct timeval tv;
即可。


0 0
原创粉丝点击