【问题标题】:got compile error when use clock_gettime in c99在 c99 中使用 clock_gettime 时出现编译错误
【发布时间】:2012-10-15 16:56:14
【问题描述】:

当我在代码 sn-p 中使用 clock_gettime 并使用标志 -std=c99 进行编译时,出现如下错误:

warning: implicit declaration of function 'clock_gettime'
error: 'CLOCK_REALTIME' undeclared (first use in this function)

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

【问题讨论】:

  • 如果您提及您的环境的细节、什么编译器、什么 libc 等,这将有所帮助。还有助于显示有问题的代码,例如 #include 任何您拥有 #define 的相关宏.
  • clock_gettime 不是标准 C99,CLOCK_REALTIME 也不是。但它们是 POSIX。
  • 谢谢,我已经用flag -std=gnu99编译成功了

标签: c c99


【解决方案1】:

在带有 -std=c99 的原始代码中,尝试添加

#define _POSIX_C_SOURCE >= 199309L

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

【讨论】:

  • 请注意,此定义必须高于time.h 的#include 指令。
  • 其实这个符号并不打算直接改变。更喜欢使用不同的编译器标志,例如 --std=gnu99。它将对旗帜产生更可靠和一致的影响。
  • gcc 9.3.0 不喜欢这样:错误:运算符 '>=' 没有左操作数 30 | #define _POSIX_C_SOURCE >= 199309L
猜你喜欢
  • 2021-11-07
  • 2012-09-27
  • 1970-01-01
  • 2020-09-19
  • 2017-03-23
  • 2017-01-23
  • 2013-06-16
  • 2013-10-28
  • 1970-01-01
相关资源
最近更新 更多