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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:50:53  来源:igfitidea点击:

C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

c++linuxubuntuposixtime.h

提问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 -lrtto 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 -lrtwhich 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 -lrtneeded to come after the project object files. Moving the -lrtto 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 gccversion 4.6.1, -lrtmust be afterfilefork.cppotherwise you get a link error.

对于gcc4.6.1 版,-lrt必须filefork.cpp之后,否则会出现链接错误。

Some older gccversion doesn't care about the position.

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

回答by P.P

Since glibc version 2.17, the library linking -lrtis 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.)