Linux的:GCC与-std = C99抱怨不知道结构的timespec [英] Linux: gcc with -std=c99 complains about not knowing struct timespec
问题描述
当我尝试用的gcc -std = C99
编译这个在Linux上,编译器抱怨不知道结构的timespec
。但是如果我编译这个没有 -std = C99
一切工作正常。
When I try to compile this on Linux with gcc -std=c99
, the compiler complains about not knowing struct timespec
. However if I compile this without -std=c99
everything works fine.
#include <time.h>
int main(void)
{
struct timespec asdf;
return 0;
}
这是为什么,是有办法仍然得到它与 -std = C99
?
推荐答案
以timespec来自POSIX,所以你必须启用POSIX定义:
The timespec comes from POSIX, so you have to 'enable' POSIX definitions:
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <time.h>
void blah(struct timespec asdf)
{
}
int main()
{
struct timespec asdf;
return 0;
}
在顶部的节是我目前使用的 - 这从触发基于是否您使用的是C99或C89编译器的单一UNIX规范(SUS)的定义
The stanza at the top is what I currently use - it triggers the definitions from Single UNIX Specification (SUS) based on whether you're using a C99 or C89 compiler.
- 如果您希望 POSIX 2008 (SUS V4)的材料,使用_XOPEN_SOURCE 700
- 如果您希望 POSIX 2004年(SUS V3)的材料,使用_XOPEN_SOURCE 600
- 如果您希望 POSIX 1995年(SUS V2,1997年)的材料,使用_XOPEN_SOURCE 500
- If you want the POSIX 2008 (SUS v4) material, use _XOPEN_SOURCE 700
- If you want the POSIX 2004 (SUS v3) material, use _XOPEN_SOURCE 600
- If you want the POSIX 1995 (SUS v2, 1997) material, use _XOPEN_SOURCE 500
对于我的系统,POSIX 2008并不像广泛使用在2004年,所以这就是我所用 - 但情况因人而异。需要注意的是SUS V3和V4都需要C99编译。在Solaris上,至少,用C89将失败。
For my systems, POSIX 2008 is not as widely available as 2004, so that's what I use - but YMMV. Note that SUS v3 and v4 both require C99 compilation. On Solaris, at least, using C89 will fail.
这篇关于Linux的:GCC与-std = C99抱怨不知道结构的timespec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!