gpt4 book ai didi

clock_gettime() undefined symbol SunOS

转载 作者:行者123 更新时间:2023-12-05 01:38:06 29 4
gpt4 key购买 nike

我正在尝试构建一个需要 2 个时间戳的程序:第一个进程结束时的时间戳,然后是第二个进程开始时的时间戳。然后得到两者的增量。

但我无法编译,因为链接器提示 clock_gettime,给出 undefined symbol 错误。

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

最佳答案

clock_gettime() 函数和许多其他函数可以在现代版本的 Solaris 上的 rt(实时)库和 posix4 库(从有 POSIX part 4 aka POSIX.4 for 'Real Time' extensions 的时代开始)在古代版本上。实时功能现在包含在基础中 POSIX specification .

因此,要解析 clock_gettime():

  • 在 Solaris 上添加 -lrt 或者,因为这对您不起作用,所以添加 -lposix4
  • 在 Linux 上添加 -lrt
  • 在 macOS 上都不添加。

在 macOS 上,函数(包括经常在 -lm 中找到的数学函数)位于主系统库中。 Apple 确实提供了一个虚拟的 -lm 库,但您不需要使用它。

深入研究一些历史,一些旧版本的 Solaris,例如 Solaris 9,我认为,Solaris 10 提供 -lposix4;我没有使用过 Solaris 11,但我希望该库存在于那里,即使不再严格需要它。通常,手册页会告诉您使用函数所需的非标准库。浏览 Oracle 站点上的手册,我发现:

因此,从表面上看,使用 -lrt 应该对您有用。但是,如果您使用的是足够旧的 Solaris 版本(GCC 的旧版本表明您可能没有使用最新版本的 Solaris),那么您可能需要使用 -lposix4 .

Google 搜索“libposix4.so solaris”会产生:

它说:

librt, libposix4 - POSIX.1b Realtime Extensions library

Historically, functions in this library provided many of the interfaces specified by the POSIX.1b Realtime Extension. See standards(5). This functionality now resides in libc(3LIB).

This library is maintained to provide backward compatibility for both runtime and compilation environments. The shared object is implemented as a filter on libc.so.1. New application development need not specify –lrt.

回头查看我的一个makefile的版本控制信息,我发现在1996年11月,我在一个makefile中添加了-lposix4; 1993 年 6 月的先前版本中没有此内容。所以,那是很久(很久)以前的事了。我不确定您使用的是哪个版本的 Solaris。维基百科 Solaris建议我对 Solaris 2.4 (1995) 或 Solaris 2.5 (1996) 进行更改。因此,可能是其中一个版本为 clock_gettime() 添加了 -lposix4,或者可能是我在那个时间范围内编写了一个需要它的程序,但它可用更早。 (仅供娱乐,makefile 的第一个版本日期为 1987-01-27。它已经存在了很长一段时间!)

关于clock_gettime() undefined symbol SunOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60161137/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com