根据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操作系统10.11.6。
发布于 2018-08-26 17:27:09
Mac标准库不符合任何现代版本的C或POSIX。它停留在C99和POSIX 2001中,即使在这些方面也存在一致性问题。
发布于 2021-04-02 21:47:07
Mac10.15支持timespec_get
。这是取自/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/time.h
的
#if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) && \
((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
(defined(__cplusplus) && __cplusplus >= 201703L))
/* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
#define TIME_UTC 1 /* time elapsed since epoch */
__API_AVAILABLE(macosx(10.15), ios(13.0), tvos(13.0), watchos(6.0))
int timespec_get(struct timespec *ts, int base);
#endif
当包含C11或C++17或更新版本时,您需要使用time.h
进行构建。
对于gettimeofday
或timespec_get
在Mac10.15上是否更适合这个目的,我还没有做过任何时间或其他方面的调查,只有timespec_get
变得可用。
https://stackoverflow.com/questions/52028083
复制相似问题