我是Ubuntu的新手,但我似乎不能让它工作。它在我学校的电脑上运行得很好,我不知道我在做什么。我已经检查了usr/和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;
}
我也使用CodeBlocks作为构建和运行的集成开发环境。任何帮助都是最好的,谢谢。
发布于 2010-03-10 23:38:58
在g++命令行末尾添加-lrt
。此链接位于librt.so“实时”共享库中。
发布于 2011-11-11 09:48:23
示例:
c++ -Wall filefork.cpp -lrt -O2
对于gcc
版本4.6.1,-lrt
必须在 filefork.cpp之后设置为,否则会出现链接错误。
一些较旧的gcc
版本并不关心位置。
发布于 2015-09-18 18:05:52
从glibc版本2.17开始,不再需要链接-lrt
的库。
clock_*
现在是主C库的一部分。您可以看到进行此更改的change history of 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是否会有任何问题,则可以检查compatibility tracker of glibc。
要检查系统上安装的glibc版本,请运行以下命令:
ldd --version
(当然,如果您使用的是旧的glibc (<2.17),那么仍然需要-lrt
。)
https://stackoverflow.com/questions/2418157
复制相似问题