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

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

#include 
#include 
#include 
#include  


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 

int gettimeofday(struct timeval *tv, struct timezone *tz);
参考:

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


于是把

struct tim tv;

换成

struct timeval tv;
即可。



博主所有文章已转自私人博客 Joe 的个人博客,谢谢关注!

你可能感兴趣的:(Linux,经验总结,C++)