【发布时间】:2018-08-26 16:17:03
【问题描述】:
根据 C11 标准 (7.27.2.5),time.h 中指定了一个函数 timespec_get。我试过几个编译器,包括clang和几个版本的gcc,应该支持C11,但是这个功能总是缺失。宏TIME_UTC 也不见了。
这是一个测试文件mytime.c:
#include <time.h>
#include <stdio.h>
int main() {
printf("C version: %ld\n", __STDC_VERSION__);
fflush(stdout);
struct timespec ts;
timespec_get(&ts, TIME_UTC);
}
以及使用 Clang 的输出:
$ cc --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ cc -std=c11 mytime.c
mytime.c:9:3: warning: implicit declaration of function 'timespec_get' is invalid in C99
[-Wimplicit-function-declaration]
timespec_get(&ts, TIME_UTC);
^
mytime.c:9:21: error: use of undeclared identifier 'TIME_UTC'
timespec_get(&ts, TIME_UTC);
^
1 warning and 1 error generated.
我注释掉了 timespec_get 行,只是为了确保我使用的是 C11,而且我确实是。
对于 gcc 版本 4.8、5 和 6,我得到基本相同的结果。
我使用的是 Mac,OS 10.11.6。
【问题讨论】:
-
函数是在库中定义的,而不是在编译器中。也许 OS X 库根本不支持该功能?
-
我的 Mac 上的 FWIW、gcc 和 clang 也不支持它。我使用
gettimeofday获得亚秒时间。 (Mac 也不支持 Posixclock_gettime。)还有一些 Darwin-specific clock functions。