Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GCC doesn't provide clock_gettime() when using -ansi

4,980 views
Skip to first unread message

Nikos Chantziaras

unread,
Mar 27, 2008, 6:22:26 PM3/27/08
to
This example program:

#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)

Larry Smith

unread,
Mar 27, 2008, 10:43:04 PM3/27/08
to

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).

Nikos Chantziaras

unread,
Mar 27, 2008, 11:18:06 PM3/27/08
to
Larry Smith wrote:
> Nikos Chantziaras wrote:
>> [...]

>> 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.

Paul Pluzhnikov

unread,
Mar 28, 2008, 1:36:39 AM3/28/08
to
Nikos Chantziaras <rea...@arcor.de> writes:

> 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.

Paul Pluzhnikov

unread,
Mar 28, 2008, 1:42:25 AM3/28/08
to
Nikos Chantziaras <rea...@arcor.de> writes:

> 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.

0 new messages