LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 816|回复: 4

这个算时间的程序为什么不对?

[复制链接]
发表于 2004-5-29 19:03:39 | 显示全部楼层 |阅读模式
很简单的计算时间的程序为什么不对?

#include<stdio.h>
#include<time.h>
#define MILLION 1000000

int main()
{

struct timespec tpstart;
struct timespec tpend;
long timedif;

clock_gettime(CLOCK_REALTIME,&tpstart);
/* put function here to calculate its running time*/
clock_gettime(CLOCK_REALTIME,&tpend);
timedif=MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
fprintf(stderr,"it took %ld microseconds\n",timedif);

return 0;
}

我用man看过了,确实有这种用法。
但编译有错误,说:
gcc -g -o time2 time2.c
/tmp/ccsmQG1E.o(.text+0x20): In function `main':
/root/shell/time2.c:12: undefined reference to `clock_gettime'
/tmp/ccsmQG1E.o(.text+0x33):/root/shell/time2.c:14: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status

Compilation exited abnormally with code 1 at Sat May 29 18:55:51

这是为什么呢?
发表于 2004-5-29 19:50:04 | 显示全部楼层
看看man clock_gettime,里面有:
NOTE
  Most systems require the program be linked with the librt library to use these functions.
编译的时候加上 -lrt 就行了
 楼主| 发表于 2004-5-29 22:41:02 | 显示全部楼层
懂了,非常感谢!
发表于 2004-5-30 19:03:55 | 显示全部楼层
最初由 libinary 发表
看看man clock_gettime,里面有:
NOTE
  Most systems require the program be linked with the librt library to use these functions.
编译的时候加上 -lrt 就行了


我的linux系统没有clcok_gettime ?
楼主的是什么系统?
 楼主| 发表于 2004-6-1 00:36:07 | 显示全部楼层
Debian unstable
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表