3

I have seen this solution but I am getting the same errors. For a class, we had to switch to the c99 compiler instead of gnu but now timespec seems to be broken. Along with "storage size of timespec isn't known", I am getting a bunch of other errors like "CLOCK_MONOTONIC undeclared", "dereferencing a pointer to incomplete type 'struct timespec'", and unused variable warnings but I assume these would go away once I work out the compiler issue.

I wouldn't imagine there is anything wrong with the way I am declaring timespec variables,

struct timespec startTime;

Community
  • 1
  • 1
grizzle
  • 69
  • 1
  • 5
  • 1
    "we had to switch to the c99 compiler instead of gnu" - That is definitively not a valid reason. gcc supports C99 as well as C11, just learn about the `-std=` option. Another questions why you learn a version of C which has been canceled 6 years ago with the current and only standard C11. Wrt your problem: read [ask] and provide a [mcve]. As it looks now, you seem to access internals of a `struct` you are not supposed to use. Which means your code is broken by design. – too honest for this site Mar 04 '17 at 15:35
  • In our new assignment details, he told us specifically to use the flag `-std=c99`. Sorry, I meant to put this in the description. I'm not choosing to use this, we have to. Also could you elaborate on trying to access the internals of a struct. – grizzle Mar 04 '17 at 15:40
  • 1
    In your question you imply you don't use gcc because is it not c99 compliant. But according to your comment you **do use** gcc (contradicting your statement in the question), just with the option I recommend. You should first understand what's going on before programming. And you should read my previous comment carefully and try to understand. This is not a spoon-feeding service! – too honest for this site Mar 04 '17 at 16:43
  • It's hard to answer a question like this without code and some basic other information like the OS you're using. – anonymoose Mar 04 '17 at 17:20
  • 1
    @Olaf "gnu" is a gcc flag, -std=gnu99. – n. m. could be an AI Mar 05 '17 at 13:40
  • If you are told to use C99, use C99. `struct timespec` and `CLOCK_MONOTONIC` are not a part of C99. Use something else. – n. m. could be an AI Mar 05 '17 at 13:42
  • @n.m.: As you wrote: `gnu99`, not `gnu`. And that still would be c99, just with the gcc extensions. The rest of the line adds to the confusion, as there is no distinct c99 compiler (`-std=c99` just deactivates the extensions and reports warnings about some of them. – too honest for this site Mar 05 '17 at 16:20
  • 1
    @Olaf yes OP is using some sloppy terminology, but the message is clear, no need to go full pedantic mode. The extensions *are* the point here. `struct timespec` is one of them. – n. m. could be an AI Mar 05 '17 at 16:57

2 Answers2

12

As this kind of "clock" support isn't C99 but POSIX, it's not enabled by default.

To enable it you want to pass the option -D_POSIX_C_SOURCE=199309L to the compiler or put a

#define _POSIX_C_SOURCE 199309L

at very beginning of the source file(s) in question.

See also man clock_gettime for reference.

alk
  • 69,737
  • 10
  • 105
  • 255
0

Set C dialect flag to GNU dialect of ISO C99:

-std=gnu99

Danijel
  • 8,198
  • 18
  • 69
  • 133