首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C++错误:对'clock_gettime‘和'clock_settime’的引用未定义

C++错误:对'clock_gettime‘和'clock_settime’的引用未定义
EN

Stack Overflow用户
提问于 2010-03-10 23:36:27
回答 4查看 194.8K关注 0票数 163

我是Ubuntu的新手,但我似乎不能让它工作。它在我学校的电脑上运行得很好,我不知道我在做什么。我已经检查了usr/和time.h是否正常。代码如下:

代码语言:javascript
复制
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    timespec time1, time2;
    int temp;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    //do stuff here
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
    return 0;
}

我也使用CodeBlocks作为构建和运行的集成开发环境。任何帮助都是最好的,谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-03-10 23:38:58

在g++命令行末尾添加-lrt。此链接位于librt.so“实时”共享库中。

票数 294
EN

Stack Overflow用户

发布于 2011-11-11 09:48:23

示例:

代码语言:javascript
复制
c++ -Wall filefork.cpp -lrt -O2

对于gcc版本4.6.1,-lrt必须在 filefork.cpp之后设置为,否则会出现链接错误。

一些较旧的gcc版本并不关心位置。

票数 44
EN

Stack Overflow用户

发布于 2015-09-18 18:05:52

从glibc版本2.17开始,不再需要链接-lrt的库。

clock_*现在是主C库的一部分。您可以看到进行此更改的change history of glibc 2.17,其中解释了此更改的原因:

代码语言:javascript
复制
+* The `clock_*' suite of functions (declared in <time.h>) is now available
+  directly in the main C library.  Previously it was necessary to link with
+  -lrt to use these functions.  This change has the effect that a
+  single-threaded program that uses a function such as `clock_gettime' (and
+  is not linked with -lrt) will no longer implicitly load the pthreads
+  library at runtime and so will not suffer the overheads associated with
+  multi-thread support in other code such as the C++ runtime library.

如果您决定升级glibc,如果您担心使用较新的glibc是否会有任何问题,则可以检查compatibility tracker of glibc

要检查系统上安装的glibc版本,请运行以下命令:

代码语言:javascript
复制
ldd --version

(当然,如果您使用的是旧的glibc (<2.17),那么仍然需要-lrt。)

票数 32
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2418157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档