#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main ()
{
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
printf("%d\n", time.tv_sec);
return 0;
}
Compiles fine with "gcc example.c", but not with "gcc -ansi example.c":
error: storage size of ‘time’ isn’t known
warning: implicit declaration of function ‘clock_gettime’
error: ‘CLOCK_REALTIME’ undeclared (first use in this function)
According to clock_gettime(3), <time.h> is the right #include.
I have yet to find a way to use clock_gettime when -ansi is used :P
Tried GCC 4.2.3 as well as GCC 4.3.0; same result. Any suggestions?
(Except not using -ansi of course :P)
It's not an ANSI function (not in the spec).
It's in only SUSv2, POSIX.1-2001
Defining _POSIX_C_SOURCE=199309L or 200112L on
the gcc command line, or in a header file, might
expose clock_gettime (per the clock_gettime 'man'
page).
Thanks for the heads-up; this does the trick. Should have payed more
attention to the man page.
Strangely though, g++ does not bark with -ansi. "g++ -ansi example.c"
compiles just fine without defining _POSIX_C_SOURCE.
> I have yet to find a way to use clock_gettime when -ansi is used :P
> Tried GCC 4.2.3 as well as GCC 4.3.0; same result. Any suggestions?
> (Except not using -ansi of course :P)
On Linux, add -D_POSIX_C_SOURCE=199309
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
> Strangely though, g++ does not bark with -ansi. "g++ -ansi example.c"
> compiles just fine without defining _POSIX_C_SOURCE.
One difference between 'gcc' and 'g++' is that the latter uses
-D_GNU_SOURCE.