|
很简单的计算时间的程序为什么不对?
#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
这是为什么呢? |
|