GCC with -std=c99 complains about not knowing struct timespec

Viewed 19800

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;
}

Why is this and is there a way to still get it to work with -std=c99?

3 Answers

Adding -D_GNU_SOURCE to your CFLAGS will also work.

gcc test.c -o test -std=c99 -D_GNU_SOURCE

Have a look at /usr/include/time.h. This is the preprocessor conditional that wraps the timespec definition. _GNU_SOURCE enables __USE_POSIX199309.

#if (!defined __timespec_defined                    \
 && ((defined _TIME_H                       \
  && (defined __USE_POSIX199309                 \
      || defined __USE_ISOC11))                 \
 || defined __need_timespec))
 # define __timespec_defined 1                                                                                                                                                                                                                 
 struct timespec
 {
    __time_t tv_sec;        /* Seconds.  */
    __syscall_slong_t tv_nsec;  /* Nanoseconds.  */
 }; 
Related