Nil*_*ils 40 c posix c99 timespec
当我尝试在Linux上编译它时gcc -std=c99,编译器抱怨不知道struct timespec.但是,如果我编译它没有-std=c99一切正常.
#include <time.h>
int main(void)
{
struct timespec asdf;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样,有没有办法让它继续使用-std=c99?
Jon*_*ler 55
timespec来自POSIX,因此您必须"启用"POSIX定义:
#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;
}
Run Code Online (Sandbox Code Playgroud)
顶部的节是我目前使用的 - 它根据您使用的是C99还是C89编译器触发Single UNIX Specification(SUS)中的定义.
对于我的系统,POSIX 2008并不像2004年那样广泛使用,所以这就是我使用的 - 但是YMMV.请注意,SUS v3和v4都需要C99编译.在Solaris上,至少使用C89会失败.
Bil*_*nch 41
我建议编译-std=gnu99.
详细说明这一点.默认情况下,gcc使用-std = gnu89进行编译.以下是源代码的结果.
#include <time.h>
int main() {
struct timespec asdf;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[1:25pm][wlynch@cardiff /tmp] gcc -std=gnu89 foo.c
[1:26pm][wlynch@cardiff /tmp] gcc -std=gnu99 foo.c
[1:25pm][wlynch@cardiff /tmp] gcc -std=c89 foo.c
foo.c: In function ‘main’:
foo.c:4: error: storage size of ‘asdf’ isn’t known
[1:26pm][wlynch@cardiff /tmp] gcc -std=c99 foo.c
foo.c: In function ‘main’:
foo.c:4: error: storage size of ‘asdf’ isn’t known
Run Code Online (Sandbox Code Playgroud)