C ++错误:未定义参照“clock_gettime”和“clock_settime”
题
我很新的Ubuntu的,但我似乎无法得到这个工作。它工作正常,在我学校的电脑,我不知道我没有做什么。我已经检查 usr / include目录并time.h中有就好了。下面是代码:
#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;
}
我使用的代码块作为我的IDE来构建和运行,以及。任何帮助将是巨大的,谢谢你。
解决方案
添加-lrt
至g ++命令行的末尾。此链接在librt.so“实时”共享库。
其他提示
示例:
c++ -Wall filefork.cpp -lrt -O2
有关gcc
版本4.6.1,-lrt
必须之后 filefork.cpp 的,否则你得到一个链接错误。
一些较旧版本gcc
不关心的位置。
我所遇到的相同的错误。我的链接器命令也有RT库包括-lrt
这是正确的,这是工作了一段时间。后重新安装Kubuntu的它停止工作。
有一个独立的论坛主题提出来的项目目标文件后所需要的-lrt
。
移动-lrt
固定这个问题对我来说是命令的结束,虽然我不知道为什么的细节。
,因为Glibc版本2.17,库链接-lrt
不再需要。
在clock_*
现在是主要的C库的一部分。你可以看到改变的glibc 2.17的历史其中这种变化是做解释这一变化的原因是:
+* 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 跟踪器>如果您担心是否会有使用较新的glibc的任何问题。
要检查安装在系统上glibc的版本,运行下面的命令:
ldd --version
(当然,如果你使用的是老的glibc(<2.17),那么你仍然需要-lrt
。)
不隶属于 StackOverflow