timepec在time.h中找不到

mat*_*975 16 c struct

我不得不将应用程序从C++重写为C.我在Ubuntu 12.04上使用gcc和Eclipse.这样做我遇到了这个错误

    ../src/TTNoddy.c: In function ‘main’:
    ../src/TTNoddy.c:16:2: error: unknown type name ‘timespec’
Run Code Online (Sandbox Code Playgroud)

这是我的代码片段,可以重现问题

    #include <time.h>

    int main(void) {

        timespec TS;
        TS.tv_nsec = 1;

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

我在这里很困惑 - 我是一名C++程序员,从来没有在我的生活中编写过纯粹的C应用程序,但是man页面clock_gettime清楚地表明timespec我在这里包含的time.h头文件中找到了它.我错过了什么?

Veg*_*ger 20

timespec是一个struct,你需要明确告诉编译器这个.如果您仔细阅读手册页,您可以看到它已经说明了.

这应该工作:

#include <time.h>

int main(void) {
    struct timespec TS;
    TS.tv_nsec = 1;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

附加说明:如果已将其定义为a typedef struct,则无需struct手动添加零件.但是,您应该假设大多数/所有纯C结构都未定义为atypedef

  • 另请注意,timespec 不是 C89/C99 的一部分,而是 POSIX。http://stackoverflow.com/questions/3875197/std-c99-wtf-on-linux (2认同)