9

when I use clock_gettime in my code snippet, and compile with flag -std=c99, I got a error like this:

warning: implicit declaration of function 'clock_gettime'
error: 'CLOCK_REALTIME' undeclared (first use in this function)

and I have included the file 'time.h'. anyone knows how to fix it.

3
  • 1
    It will help if you mention specifics of your environment, what compiler, what libc, etc. Also helps to show the code in question, like the #include any relevant macros you have #define for.
    – mlibby
    Oct 25, 2012 at 13:35
  • 2
    clock_gettime is not standard C99, nor is CLOCK_REALTIME. But they are POSIX.
    – effeffe
    Oct 25, 2012 at 13:36
  • 4
    thanks, I have compiled successfully with flag -std=gnu99
    – Xinyu NG
    Oct 25, 2012 at 13:40

1 Answer 1

8

in your original code with -std=c99, try adding

#define _POSIX_C_SOURCE >= 199309L

the man page for clock_gettime indicates this is a necessary feature test macro requirement.

3
  • 3
    Please note that this definition must be above the #include directive for time.h. Jan 4, 2014 at 18:45
  • 12
    Actually, the symbol is not intended to be changed directly. Prefer instead using a different compiler flag such as --std=gnu99. It will have a more reliable and consistent impact on the flag.
    – TheRealNeo
    Nov 19, 2014 at 21:02
  • 1
    gcc 9.3.0 doesn't like this: error: operator '>=' has no left operand 30 | #define _POSIX_C_SOURCE >= 199309L
    – PeterT
    May 6, 2020 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.