Linux C++ 错误:未定义对“clock_gettime”和“clock_settime”的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2418157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ error: undefined reference to 'clock_gettime' and 'clock_settime'
提问by naspinski
I am pretty new to Ubuntu, but I can't seem to get this to work. It works fine on my school computers and I don't know what I am not doing. I have checked usr/includeand time.h is there just fine. Here is the code:
我对 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;
}
I am using CodeBlocks as my IDE to build and run as well. Any help would be great, thank you.
我也使用 CodeBlocks 作为我的 IDE 来构建和运行。任何帮助都会很棒,谢谢。
采纳答案by Dmitry Yudakov
Add -lrt
to the end of g++ command line. This links in the librt.so "Real Time" shared library.
添加-lrt
到 g++ 命令行的末尾。这链接在 librt.so “实时”共享库中。
回答by Adam
I encountered the same error. My linker command did have the rt library included -lrt
which is correct and it was working for a while. After re-installing Kubuntu it stopped working.
我遇到了同样的错误。我的链接器命令确实包含了 rt 库-lrt
,这是正确的,并且它已经工作了一段时间。重新安装 Kubuntu 后,它停止工作。
A separate forum thread suggested the -lrt
needed to come after the project object files.
Moving the -lrt
to the end of the command fixed this problem for me although I don't know the details of why.
一个单独的论坛帖子建议-lrt
需要在项目目标文件之后。移动-lrt
到命令的末尾为我解决了这个问题,尽管我不知道原因的细节。
回答by jing kang
example:
例子:
c++ -Wall filefork.cpp -lrt -O2
For gcc
version 4.6.1, -lrt
must be afterfilefork.cppotherwise you get a link error.
对于gcc
4.6.1 版,-lrt
必须在filefork.cpp之后,否则会出现链接错误。
Some older gcc
version doesn't care about the position.
一些旧gcc
版本不关心位置。
回答by P.P
Since glibc version 2.17, the library linking -lrt
is no longer required.
从 glibc 2.17 版开始,-lrt
不再需要库链接。
The clock_*
are now part of the main C library. You can see the change history of glibc 2.17where this change was done explains the reason for this change:
在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.
If you decide to upgrade glibc, then you can check the compatibility tracker of glibcif you are concerned whether there would be any issues using the newer glibc.
如果您决定升级 glibc,那么您可以检查glibc的兼容性跟踪器,如果您担心使用较新的 glibc 是否会出现任何问题。
To check the glibc version installed on the system, run the command:
要检查系统上安装的 glibc 版本,请运行以下命令:
ldd --version
(Of course, if you are using old glibc (<2.17) then you will still need -lrt
.)
(当然,如果你使用旧的 glibc (<2.17) 那么你仍然需要-lrt
.)