gcc -std=c99 fail to compile clock_gettime?

E

Ethan

/* rtclock.c */
#include
<time.h>

int main
()
{
struct timespec
ts;
clock_gettime(CLOCK_REALTIME,
ts);

return
0;
}

gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

it compiles with
$> gcc -c -g rtclock.c

but why doesn't this compile with
$> gcc -std=c99 -c -g rtclock.c
rtclock.c: In function ‘main’:
rtclock.c:5: error: storage size of ‘ts’ isn’t known
rtclock.c:6: warning: implicit declaration of function ‘clock_gettime’
rtclock.c:6: error: ‘CLOCK_REALTIME’ undeclared (first use in this
function)
rtclock.c:6: error: (Each undeclared identifier is reported only once
rtclock.c:6: error: for each function it appears in.)
 
S

Seebs

but why doesn't this compile with
$> gcc -std=c99 -c -g rtclock.c

Because you just told it not to define any symbols that aren't part of
standard C. Such as POSIX features, or GNU features.

-s
 
K

Keith Thompson

Ethan said:
/* rtclock.c */
#include <time.h>

int main ()
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, ts);

return 0;
}

I've taken the liberty of reformatting your code for legibility.
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

it compiles with
$> gcc -c -g rtclock.c

but why doesn't this compile with
$> gcc -std=c99 -c -g rtclock.c
rtclock.c: In function ‘main’:
rtclock.c:5: error: storage size of ‘ts’ isn’t known
rtclock.c:6: warning: implicit declaration of function ‘clock_gettime’
rtclock.c:6: error: ‘CLOCK_REALTIME’ undeclared (first use in this
function)
rtclock.c:6: error: (Each undeclared identifier is reported only once
rtclock.c:6: error: for each function it appears in.)

Probably because the standard C version of <time.h> doesn't declare
any of those identifiers, and in a conforming C implementation
it may not do so. I might have expected it to be accepted if
you don't specify "-pedantic", but gcc's behavior is conforming.
Consult the gcc documentation for more information.

Experiment shows that most of the error messages go away if you use
"-std=gnu99" rather than "-std=c99". (Finding the one remaining
error is left as an exercise.)

I see that the man page for clock_gettime refers to
feature_test_macros(7); you might want to look into that as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top