Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timespec not found in time.h

Tags:

c

struct

I am having to rewrite an application from C++ to C. I am using gcc and Eclipse on Ubuntu 12.04. In doing so I have come across this error

../src/TTNoddy.c: In function ‘main’:
../src/TTNoddy.c:16:2: error: unknown type name ‘timespec’

Here is my code snippet that reproduces the problem

#include <time.h>

int main(void) {

    timespec TS;
    TS.tv_nsec = 1;

    return 0;
}

I am confused here - I am a C++ coder and never written a pure C application in my life, but the man page for clock_gettime clearly indicates that timespec is found in the time.h header file which I am including here. What have I missed?

like image 516
mathematician1975 Avatar asked Jun 22 '12 09:06

mathematician1975


2 Answers

timespec is a struct, you need to explicitly tell the compiler this. If you carefully read the man page you can see it is stated so.

This should work:

#include <time.h>

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

    return 0;
}

Additional note: If it had been defined as a typedef struct, you would not have needed to add the struct part manually. But, you should assume that most/all pure C structs are not defined as a typedef

like image 151
Veger Avatar answered Sep 25 '22 11:09

Veger


It should not be just timespec as timespec is a struct. It should be struct timespec. Please modify your code accordingly.

like image 22
Jay Avatar answered Sep 24 '22 11:09

Jay