为什么我在Linux使用gettimeofday会报未定义的错???

Oscarming 2007-10-24 08:45:45
代码如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int i,j,k;
char bu;
float **matrix,**matrix1,**matrix2;
int num;
scanf("%d",&num);
scanf("%c",&bu);
matrix=new float*[num];
matrix1=new float*[num];
matrix2=new float*[num];

for(i=0;i<num;i++)
{
matrix[i]=new float[num];
matrix1[i]=new float[num];
matrix2[i]=new float[num];
}

srand(time(NULL));
for(i=0;i<num;i++)
for(j=0;j<num;j++)
{
matrix[i][j]=0;
matrix1[i][j]=(float)rand();
matrix2[i][j]=(float)rand();
}
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);

for(i=0;i<num;i++)
for(j=0;j<num;j++)
for(k=0;k<num;k++)
{
matrix[i][j]+=matrix1[i][k]*matrix2[k][j];
}
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
printf("%f\n",timeuse);
return 0;
}

在UINX上可以编译成功,但在Linux上会报这个错:
$ g++ -c singlethread.c
singlethread.c: In function `int main()':
singlethread.c:38: error: `gettimeofday' undeclared (first use this function)
singlethread.c:38: error: (Each undeclared identifier is reported only once for each function it appears in.)
请大家帮忙解决一下,谢谢
...全文
1714 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2007-10-25
  • 打赏
  • 举报
回复
用man gettimeofday就能知道需要什么头文件和什么库文件了!

GETTIMEOFDAY(2) Linux Programmer's Manual GETTIMEOFDAY(2)

NAME
gettimeofday, settimeofday - get / set time

SYNOPSIS
#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);

cceczjxy 2007-10-25
  • 打赏
  • 举报
回复 1
#include <sys/time.h>


加上它就可以了
yecheng_110 2007-10-24
  • 打赏
  • 举报
回复
#include <sys/time.h>
试试
hnhbdss 2007-10-24
  • 打赏
  • 举报
回复
没有任何question under centos 5

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧