nas*_*ski 153 c++ linux ubuntu posix time.h
我对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;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用CodeBlocks作为我的IDE来构建和运行.任何帮助都会很棒,谢谢.
Dmi*_*kov 277
添加-lrt
到g ++命令行的末尾.这链接在librt.so"实时"共享库中.
小智 42
例:
c++ -Wall filefork.cpp -lrt -O2
Run Code Online (Sandbox Code Playgroud)
对于gcc
版本4.6.1,-lrt
必须在 filefork.cpp 之后,否则会出现链接错误.
一些旧gcc
版本并不关心这个位置.
小智 26
我遇到了同样的错误.我的链接器命令确实包含了-lrt
正确的rt库,它正在工作一段时间.重新安装Kubuntu后,它停止工作.
一个单独的论坛帖子建议-lrt
在项目对象文件之后需要.移动-lrt
到命令的末尾为我解决了这个问题,虽然我不知道为什么的细节.
P.P*_*.P. 26
由于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.
Run Code Online (Sandbox Code Playgroud)
如果您决定升级glibc,那么您可以检查glibc的兼容性跟踪器,如果您担心使用较新的glibc是否会出现任何问题.
要检查系统上安装的glibc版本,请运行以下命令:
ldd --version
Run Code Online (Sandbox Code Playgroud)
(当然,你使用旧的glibc(<2.17),然后你仍然需要-lrt
.)