在c99中使用clock_gettime时出现编译错误

Xin*_* NG 9 c c99

当我在我的代码片段中使用clock_gettime,并使用标志-std = c99进行编译时,我收到如下错误:

warning: implicit declaration of function 'clock_gettime'
error: 'CLOCK_REALTIME' undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)

我已经包含了'time.h'文件.任何人都知道如何解决它.

mli*_*bby 7

在原始代码中使用-std = c99,尝试添加

#define _POSIX_C_SOURCE >= 199309L
Run Code Online (Sandbox Code Playgroud)

clock_gettime的手册页表明这是必要的功能测试宏要求.

  • 实际上,该符号无意直接更改.更喜欢使用不同的编译器标志,例如--std = gnu99.它将对旗帜产生更可靠和一致的影响. (10认同)
  • 请注意,此定义必须高于`time.h`的#include指令. (3认同)