gcc -std=c99 fail to compile clock_gettime?

Discussion in 'C Programming' started by Ethan, Nov 13, 2010.

  1. Ethan

    Ethan Guest

    /* 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.)
     
    Ethan, Nov 13, 2010
    #1
    1. Advertising

  2. Ethan

    Seebs Guest

    On 2010-11-13, Ethan <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Nov 13, 2010
    #2
    1. Advertising

  3. Ethan <> writes:
    > /* 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 13, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    680
    Kevin P. Fleming
    Nov 6, 2003
  2. Replies:
    3
    Views:
    3,759
    Chris Torek
    Feb 20, 2006
  3. Stefano Sabatini

    -D_HAVE_ISOC9X_SOURCE and gcc -std=c99

    Stefano Sabatini, Apr 27, 2008, in forum: C Programming
    Replies:
    1
    Views:
    270
    Keith Thompson
    Apr 27, 2008
  4. Ethan
    Replies:
    1
    Views:
    2,247
  5. Ethan
    Replies:
    2
    Views:
    2,273
    Keith Thompson
    Nov 13, 2010
Loading...

Share This Page