【发布时间】:2020-05-26 09:43:27
【问题描述】:
我正在尝试构建一个需要 2 个时间戳的程序: 第一个进程结束时的时间戳,然后是第二个进程开始时的时间戳。然后得到两者的delta。
但我无法编译,因为链接器抱怨 clock_gettime,给出未定义符号错误。
gcc (GCC) 3.2.1
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int main()
{
struct timespec start, stop;
double timeStampFull1;
double timeStampFull2;
FILE *fOut;
fOut = fopen("fileOut.txt", "a");
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
timeStampFull1 = (double)start.tv_sec + (double)(start.tv_nsec/1000000000.0);
delay(1);
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
timeStampFull2 = (double)stop.tv_sec + (double)(stop.tv_nsec/1000000000.0);
printf("tv1: %f \n", timeStampFull1);
printf("tv2: %f \n", timeStampFull2);
fprintf(fOut, "TimeStartOfTest\t\t\t%f\n", timeStampFull1);
fprintf(fOut, "TimeEndOfTest\t\t\t%f\n", timeStampFull2);
fclose(fOut);
return 0;
}
这里是编译器(链接器)错误:
gcc -Wall -o time ./time.c
time.c:49:2: warning: no newline at end of file
Undefined first referenced
symbol in file
clock_gettime /var/tmp//ccuu5CKe.o
ld: fatal: Symbol referencing errors. No output written to time
collect2: ld returned 1 exit status
【问题讨论】:
-
您是否尝试添加
-lrt? -
是的,但我也收到此错误:time.c:49:2: 警告:文件末尾没有换行符 ld:致命:库 -lrt:未找到 ld:致命:文件处理错误。没有输出写入时间 collect2:ld 返回 1 退出状态
-
您使用的是哪个版本的 SunOS?
-
说是CDE1.2。
-
uname -a说什么?