8

I am trying to measure the running time of a function using clock_gettime(). I am including time.h, I added -lrt to the Makefile, and added the right path on Eclipse CDT. However, when I try to compile I keep getting these kinds of errors:

experiments.c: In function ‘main’:
experiments.c:137:2: error: unknown type name ‘timespec’
timespec time1, time2;
^
experiments.c:139:2: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
^
experiments.c:139:16: error: ‘CLOCK_PROCESS_CPUTIME_ID’ undeclared
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

This happens with any type of CLOCK_ I try to use. I've been reading plenty of questions/answers and tutorials but haven't been able to find something that helps.

The headers I'm including are:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

I'm on Ubuntu 13.10 32 bit and compiling on gcc with the following CFLAGS: -g -Wall -pedantic -std=c99

If I add the flag -D_POSIX_C_SOURCE=199309L I get error: unknown type name ‘timespec’ and warnings about using timespec.

This is the part of the code, just in case it helps:

timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
.
.
.
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
/*code stuff*/
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

Thanks

  • What header files are you including? – Grantly Nov 05 '14 at 23:30
  • There is a note in the man page about `_POSIX_C_SOURCE >= 199309L`, what are you trying to compile this on? – David C. Rankin Nov 05 '14 at 23:49
  • @DavidC.Rankin compiling on gcc with the following flags: -g -Wall -pedantic -std=c99 – Francisca Concha-Ramírez Nov 05 '14 at 23:57
  • @DavidC.Rankin added the flag `-D_POSIX_C_SOURCE=199309L` but getting `error: unknown type name ‘timespec’` (edited to add all of this to the question) – Francisca Concha-Ramírez Nov 06 '14 at 00:02
  • place the '-lrt' at the END of the compile line, because a function is not included from a lib by the linker until AFTER it is referenced in code and the compile/link line is evaluated left to right. – user3629249 Nov 06 '14 at 05:02
  • the calls to clock_gettime() return an error code. A return of 0 indicates success, a return of -1 indicates a failure. your code should be checking that return code on every call to clock_gettime(). Also, the prototype for clock_gettime() is defined in the time.h header file, so your code should not include a prototype for the function. – user3629249 Nov 06 '14 at 05:17
  • For me the answer was contained in the question: the code (part of xenomai 3.0.5 install) didn't have `include ` at the top – kjohnsen Feb 04 '20 at 15:26

1 Answers1

6

Putting this and this answers together I was able to make it work. I had to add the _POSIX_C_SOURCE macro, to make sure the preprocessor was getting the library features correctly, which I did by adding this line before all my includes:

#define _POSIX_C_SOURCE 199309L

Then I started getting a unknown type name timespec error, which was happening because you have to tell the compiler explicitly that timespec is a struct. Fixed it by writing:

struct timespec time1, time2; 

instead of just timespec time1, time2;.

Community
  • 1
  • 1
  • 7
    Using 1993 is a bit retrograde. You'd probably succeed with `#define _XOPEN_SOURCE 700` which requests POSIX 2008 support (though you could use `600` for the prior version, 2004, which might be more widely available). You could also use `-std=gnu99` (or `-std=gnu11`) to get them defined without writing a `#define` at the top of your code. As for using `struct timespec`, that is a question of reading the specification for the functions; that's what it says, so that's what you need to use unless you provide `typedef struct timespec timespec;` (but note that you can't do that for `struct stat`). – Jonathan Leffler Nov 06 '14 at 00:36