18

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
mathematician1975
  • 21,161
  • 6
  • 59
  • 101

5 Answers5

23

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

Krizz
  • 11,362
  • 1
  • 30
  • 43
Veger
  • 37,240
  • 11
  • 105
  • 116
  • Thanks. I knew it was a struct, it is just coming from C++ where my code works I never knew you had to explicitly declare each timespec with struct first. I guess there will be many differences that I am going to find like this. – mathematician1975 Jun 22 '12 at 09:24
  • 2
    Also note that timespec is not part of C89/C99 but POSIX. http://stackoverflow.com/questions/3875197/std-c99-wtf-on-linux – Morpfh Sep 22 '12 at 21:55
  • Actually my mingw32 compiler doesnt define timespec in time.h at all. But rather pthread.h –  Mar 05 '20 at 19:09
3

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

Jay
  • 24,173
  • 25
  • 93
  • 141
2

I got this error when trying to compile a working project under Visual Studio 2015.

The solution was to add HAVE_STRUCT_TIMESPEC to the Preprocessor Definitions.

Through the GUI: Project Properties (pan) > Property Pages (icon) > Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions > Edit > Add HAVE_STRUCT_TIMESPEC

Or manually: Edit each project file and replace each instance of <PreprocessorDefinitions> (there can be several per file) with something like:

<PreprocessorDefinitions>HAVE_STRUCT_TIMESPEC;WIN32;__GNU_LIBRARY__;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>

NB: I found this answer somewhere on a Github issue, so I'm posting it here.

KrisWebDev
  • 9,342
  • 4
  • 39
  • 59
  • 1
    Also see [Timespec redefinition error](http://stackoverflow.com/q/33557506) and [Timespec :struct type Error c2011](http://stackoverflow.com/q/33114535). – jww Aug 14 '16 at 06:02
2

This issue gave me problems for a while, what I ended up doing was defining struct timespec in my code. (just copied it over, straight from man nanosleep)

#include <time.h>

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};

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

    return 0;
}
gilbert
  • 21
  • 2
2

I know this is an old question but I was having the same problem after upgrading from gcc 6.3 to 7.1. After looking at the changes you have to define _GNU_SOURCE to include struct_timespec.h.

Mike Seeds
  • 400
  • 3
  • 10