compile error

Discussion in 'C Programming' started by Ceriousmall, Feb 18, 2011.

  1. Ceriousmall

    Ceriousmall Guest

    Why is this program not compiling when the Gcc command is used . This
    is a out of the K&R book, Gcc 4.4.4 Slackware 13.1 x86_64 . the error
    is........conflicting types for 'getline'
    note
    previous declaration of 'getline' was here
    It does however compile with the g++ command..........This is the
    code..........................
    ..
    ..
    /* program prints longest input line */
    #include <stdio.h>

    #define MAXLINE 1000 /* maximum input line size */

    int getline(char line[], int maxline);
    void copy(char to[], char from[]);

    int main(void)
    {
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here */

    max = 0;

    while ((len = getline(line, MAXLINE)) > 0)
    if (len > max) {
    max = len;
    copy(longest, line);
    }
    if (max > 0) /* there was a line */
    printf("%s", longest);

    return 0;
    }

    /* getline: read a line into s, return length */
    int getline(char s[], int lim)
    {
    int c, i;

    for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
    s = c;
    if (c == '\n') {
    s = c;
    ++i;
    }
    s = '\0';

    return i;
    }

    /* copy: copy 'from' into 'to'; assume to is big enough */
    void copy(char to[], char from[])
    {
    int i;

    i = 0;

    while ((to = from ) != '\0')
    ++i;
    }
     
    Ceriousmall, Feb 18, 2011
    #1
    1. Advertising

  2. Ceriousmall

    Ian Collins Guest

    On 02/18/11 04:45 PM, Ceriousmall wrote:
    > Why is this program not compiling when the Gcc command is used . This
    > is a out of the K&R book, Gcc 4.4.4 Slackware 13.1 x86_64 . the error
    > is........conflicting types for 'getline'


    Your system headers probably do a naughty and declare their own getline
    in <stdio.h>.

    Try invoking gcc in a conforming mode (gcc -ansi -pedantic -Wall).

    --
    Ian Collins
     
    Ian Collins, Feb 18, 2011
    #2
    1. Advertising

  3. Ceriousmall

    Seebs Guest

    On 2011-02-18, Ceriousmall <> wrote:
    > Why is this program not compiling when the Gcc command is used . This
    > is a out of the K&R book, Gcc 4.4.4 Slackware 13.1 x86_64 . the error
    > is........conflicting types for 'getline'


    The reason is that the glibc developers are utterly and totally opposed
    to any kind of basic sanity in their development process.

    Basically, glibc declares a function with the name "getline" and makes it
    part of the system implementation. It does this despite the fact that
    the name in question was widely used (including, as you note, being used
    in examples in K&R) and was in a namespace reserved for users.

    This is, I believe, the single dumbest implementation decision I have ever
    seen in any C compiler or C library. And that is, I will point out, a
    field with very stiff competition.

    -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, Feb 18, 2011
    #3
  4. Ceriousmall

    Seebs Guest

    On 2011-02-18, Jonathan Leffler <> wrote:
    > getline() is a recent addition to the POSIX 2008 standard that is being
    > exposed to you. Everybody who has their own implementation of getline()
    > and does not ensure that POSIX functions are not made visible (or who
    > needs other POSIX functions from <stdio.h>) will get burned by it until
    > they rename their function.


    ....

    I take back what I said about glibc.

    This is the single stupidest standardization decision I have ever seen. It
    would never have occurred to me that the POSIX people could do something
    *this* stupid. ... And yes, I know how extreme a statement that is. :)

    -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, Feb 18, 2011
    #4
  5. Ceriousmall

    Ian Collins Guest

    On 02/19/11 08:26 AM, Seebs wrote:
    > On 2011-02-18, Jonathan Leffler<> wrote:
    >> getline() is a recent addition to the POSIX 2008 standard that is being
    >> exposed to you. Everybody who has their own implementation of getline()
    >> and does not ensure that POSIX functions are not made visible (or who
    >> needs other POSIX functions from<stdio.h>) will get burned by it until
    >> they rename their function.

    >
    > ....
    >
    > I take back what I said about glibc.
    >
    > This is the single stupidest standardization decision I have ever seen. It
    > would never have occurred to me that the POSIX people could do something
    > *this* stupid. ... And yes, I know how extreme a statement that is. :)


    This isn't the only POSIX function to be found in <stdio.h>. The
    problem is gcc includes POSIX extensions in its default mode.

    --
    Ian Collins
     
    Ian Collins, Feb 18, 2011
    #5
  6. Ian Collins <> writes:
    > On 02/19/11 08:26 AM, Seebs wrote:
    >> On 2011-02-18, Jonathan Leffler<> wrote:
    >>> getline() is a recent addition to the POSIX 2008 standard that is being
    >>> exposed to you. Everybody who has their own implementation of getline()
    >>> and does not ensure that POSIX functions are not made visible (or who
    >>> needs other POSIX functions from<stdio.h>) will get burned by it until
    >>> they rename their function.

    >>
    >> ....
    >>
    >> I take back what I said about glibc.
    >>
    >> This is the single stupidest standardization decision I have ever seen. It
    >> would never have occurred to me that the POSIX people could do something
    >> *this* stupid. ... And yes, I know how extreme a statement that is. :)

    >
    > This isn't the only POSIX function to be found in <stdio.h>. The
    > problem is gcc includes POSIX extensions in its default mode.


    How much of a problem is that, though? I suspect that most gcc users
    would rather have POSIX available by default than not.

    (I really wish POSIX had been able to avoid adding non-standard
    functions to the C standard headers, but some of those headers,
    including what are now POSIX-specific declarations, predate the
    C standard.)

    --
    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, Feb 18, 2011
    #6
  7. Ceriousmall

    Ian Collins Guest

    On 02/19/11 10:42 AM, Keith Thompson wrote:
    > Ian Collins<> writes:
    >> On 02/19/11 08:26 AM, Seebs wrote:
    >>> On 2011-02-18, Jonathan Leffler<> wrote:
    >>>> getline() is a recent addition to the POSIX 2008 standard that is being
    >>>> exposed to you. Everybody who has their own implementation of getline()
    >>>> and does not ensure that POSIX functions are not made visible (or who
    >>>> needs other POSIX functions from<stdio.h>) will get burned by it until
    >>>> they rename their function.
    >>>
    >>> ....
    >>>
    >>> I take back what I said about glibc.
    >>>
    >>> This is the single stupidest standardization decision I have ever seen. It
    >>> would never have occurred to me that the POSIX people could do something
    >>> *this* stupid. ... And yes, I know how extreme a statement that is. :)

    >>
    >> This isn't the only POSIX function to be found in<stdio.h>. The
    >> problem is gcc includes POSIX extensions in its default mode.

    >
    > How much of a problem is that, though? I suspect that most gcc users
    > would rather have POSIX available by default than not.


    In the context of this post it is the problem!

    --
    Ian Collins
     
    Ian Collins, Feb 18, 2011
    #7
  8. Ceriousmall

    Seebs Guest

    On 2011-02-18, Ian Collins <> wrote:
    > On 02/19/11 08:26 AM, Seebs wrote:
    >> This is the single stupidest standardization decision I have ever seen. It
    >> would never have occurred to me that the POSIX people could do something
    >> *this* stupid. ... And yes, I know how extreme a statement that is. :)


    > This isn't the only POSIX function to be found in <stdio.h>. The
    > problem is gcc includes POSIX extensions in its default mode.


    No, the problem is POSIX inventing a new function and giving it a name
    that was not only in the user's namespace, *but very widely used*. In
    programs that had been running on UNIX machines for years.

    -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, Feb 18, 2011
    #8
  9. Ceriousmall

    Ian Collins Guest

    On 02/19/11 11:30 AM, Seebs wrote:
    > On 2011-02-18, Ian Collins<> wrote:
    >> On 02/19/11 08:26 AM, Seebs wrote:
    >>> This is the single stupidest standardization decision I have ever seen. It
    >>> would never have occurred to me that the POSIX people could do something
    >>> *this* stupid. ... And yes, I know how extreme a statement that is. :)

    >
    >> This isn't the only POSIX function to be found in<stdio.h>. The
    >> problem is gcc includes POSIX extensions in its default mode.

    >
    > No, the problem is POSIX inventing a new function and giving it a name
    > that was not only in the user's namespace, *but very widely used*. In
    > programs that had been running on UNIX machines for years.


    Did they define a new function, or standardise existing (glibc)
    practice? I've never used getline, but I'm sure it has been 'standard"
    on Linux for a long time.

    --
    Ian Collins
     
    Ian Collins, Feb 18, 2011
    #9
  10. Ceriousmall

    Seebs Guest

    On 2011-02-18, Ian Collins <> wrote:
    > Did they define a new function, or standardise existing (glibc)
    > practice? I've never used getline, but I'm sure it has been 'standard"
    > on Linux for a long time.


    Sort of. If you look at the dozens of compilation problems introduced by
    this, the thing is that a lot of GNU tools *provide their own*... using a
    standard interface.

    Which is different from this one.

    -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, Feb 18, 2011
    #10
  11. Ceriousmall

    Jorgen Grahn Guest

    On Fri, 2011-02-18, Ian Collins wrote:
    > On 02/19/11 08:26 AM, Seebs wrote:
    >> On 2011-02-18, Jonathan Leffler<> wrote:
    >>> getline() is a recent addition to the POSIX 2008 standard that is being
    >>> exposed to you. Everybody who has their own implementation of getline()
    >>> and does not ensure that POSIX functions are not made visible (or who
    >>> needs other POSIX functions from<stdio.h>) will get burned by it until
    >>> they rename their function.

    >>
    >> ....
    >>
    >> I take back what I said about glibc.
    >>
    >> This is the single stupidest standardization decision I have ever seen. It
    >> would never have occurred to me that the POSIX people could do something
    >> *this* stupid. ... And yes, I know how extreme a statement that is. :)

    >
    > This isn't the only POSIX function to be found in <stdio.h>. The
    > problem is gcc includes POSIX extensions in its default mode.


    It should be well-known by now that you shouldn't use gcc's default
    mode. Say -ansi or -std=c99, and the problem goes away.

    I don't know why they haven't made -ansi their default. For a long
    time I believed it was so ancient pre-ANSI code would compile, but it
    turns out that was the -traditional switch, which has been removed
    some time during the last decade.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 19, 2011
    #11
  12. Ceriousmall

    Seebs Guest

    On 2011-02-19, Jorgen Grahn <> wrote:
    > It should be well-known by now that you shouldn't use gcc's default
    > mode. Say -ansi or -std=c99, and the problem goes away.


    > I don't know why they haven't made -ansi their default. For a long
    > time I believed it was so ancient pre-ANSI code would compile, but it
    > turns out that was the -traditional switch, which has been removed
    > some time during the last decade.


    Of the last million or so lines of code I've built with gcc, I'd
    guess less than 1% would have built with -ansi but not without it,
    and well over 50% would not have built with -ansi but did by
    default.

    -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, Feb 19, 2011
    #12
  13. In article <>,
    Jorgen Grahn <> wrote:
    ....
    >It should be well-known by now that you shouldn't use gcc's default
    >mode. Say -ansi or -std=c99, and the problem goes away.


    And causes a bunch of other problems.

    >I don't know why they haven't made -ansi their default. For a long


    Sure you do. For the same reason that the default mode of my car
    doesn't require me to pump the pedals to make it go (in the manner that,
    unfortunately, my bicycle does require).

    --
    Faced with the choice between changing one's mind and proving that there is
    no need to do so, almost everyone gets busy on the proof.

    - John Kenneth Galbraith -
     
    Kenny McCormack, Feb 19, 2011
    #13
  14. On Feb 18, 9:42 pm, Keith Thompson <> wrote:
    > Ian Collins <> writes:


    <snip>

    > > This isn't the only POSIX function to be found in <stdio.h>.  The
    > > problem is gcc includes POSIX extensions in its default mode.

    >
    > How much of a problem is that, though?  I suspect that most gcc users
    > would rather have POSIX available by default than not.


    really? why? gcc runs on plenty of non-Posix systems. I prefer my
    compilers to be standard straight out of the box. The fact that none
    of them are is another issue.

    > (I really wish POSIX had been able to avoid adding non-standard
    > functions to the C standard headers, but some of those headers,
    > including what are now POSIX-specific declarations, predate the
    > C standard.)
     
    Nick Keighley, Feb 19, 2011
    #14
  15. Ceriousmall

    Jorgen Grahn Guest

    On Sat, 2011-02-19, Seebs wrote:
    > On 2011-02-19, Jorgen Grahn <> wrote:
    >> It should be well-known by now that you shouldn't use gcc's default
    >> mode. Say -ansi or -std=c99, and the problem goes away.

    >
    >> I don't know why they haven't made -ansi their default. For a long
    >> time I believed it was so ancient pre-ANSI code would compile, but it
    >> turns out that was the -traditional switch, which has been removed
    >> some time during the last decade.

    >
    > Of the last million or so lines of code I've built with gcc, I'd
    > guess less than 1% would have built with -ansi but not without it,
    > and well over 50% would not have built with -ansi but did by
    > default.


    I guess it's not because you use those weird gcc-specific extensions
    to the language, but because you actually use POSIX/BSD/etc stuff from
    the libc?

    I use -ansi in combination with -D_POSIX_SOURCE (or whatever it is I'm
    using.) Seems much cleaner than trying to remember what not saying
    anything actually means.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 19, 2011
    #15
  16. Ceriousmall

    Tim Rentsch Guest

    Seebs <> writes:

    > On 2011-02-19, Jorgen Grahn <> wrote:
    >> It should be well-known by now that you shouldn't use gcc's default
    >> mode. Say -ansi or -std=c99, and the problem goes away.

    >
    >> I don't know why they haven't made -ansi their default. For a long
    >> time I believed it was so ancient pre-ANSI code would compile, but it
    >> turns out that was the -traditional switch, which has been removed
    >> some time during the last decade.

    >
    > Of the last million or so lines of code I've built with gcc, I'd
    > guess less than 1% would have built with -ansi but not without it,
    > and well over 50% would not have built with -ansi but did by
    > default.


    What fraction would have built with -ansi plus the relevant
    defines to include POSIX functions?

    What fraction would have built with -std=c99 plus the relevant
    defines to include POSIX functions?

    Hopefully at least one of those numbers is fairly large...
     
    Tim Rentsch, Feb 21, 2011
    #16
  17. Ceriousmall

    Ian Collins Guest

    On 02/21/11 05:09 PM, Tim Rentsch wrote:
    > Seebs<> writes:
    >
    >> On 2011-02-19, Jorgen Grahn<> wrote:
    >>> It should be well-known by now that you shouldn't use gcc's default
    >>> mode. Say -ansi or -std=c99, and the problem goes away.

    >>
    >>> I don't know why they haven't made -ansi their default. For a long
    >>> time I believed it was so ancient pre-ANSI code would compile, but it
    >>> turns out that was the -traditional switch, which has been removed
    >>> some time during the last decade.

    >>
    >> Of the last million or so lines of code I've built with gcc, I'd
    >> guess less than 1% would have built with -ansi but not without it,
    >> and well over 50% would not have built with -ansi but did by
    >> default.

    >
    > What fraction would have built with -ansi plus the relevant
    > defines to include POSIX functions?
    >
    > What fraction would have built with -std=c99 plus the relevant
    > defines to include POSIX functions?
    >
    > Hopefully at least one of those numbers is fairly large...


    Don't hold your breath if a good part of those million lines are the
    Linux kernel!

    --
    Ian Collins
     
    Ian Collins, Feb 21, 2011
    #17
  18. Ceriousmall

    Seebs Guest

    On 2011-02-19, Jorgen Grahn <> wrote:
    > I guess it's not because you use those weird gcc-specific extensions
    > to the language, but because you actually use POSIX/BSD/etc stuff from
    > the libc?


    I think I'm using some gcc-specific stuff, in fact, I'm actively selecting
    some GNU extensions from glibc. So I actually run with -std=gnu99. But
    I am sorta the Toolchain Guy for $dayjob, so I am the first point of contact
    for basically all compiler problems. It's VERY rare for them to be caused
    by the compiler including GNU extensions by default -- partially because
    they're mostly things that have been compiled on such systems before. But
    a TON of them wouldn't work if you didn't have those extensions on by default.

    I think it really is the right default for most Linux-like systems, at
    least. Most programs are not intended to be strictly conforming.

    -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, Feb 23, 2011
    #18
  19. Seebs <> writes:

    > On 2011-02-19, Jorgen Grahn <> wrote:
    >> I guess it's not because you use those weird gcc-specific extensions
    >> to the language, but because you actually use POSIX/BSD/etc stuff from
    >> the libc?

    >
    > I think I'm using some gcc-specific stuff, in fact, I'm actively selecting
    > some GNU extensions from glibc. So I actually run with -std=gnu99.


    But that changes the language as well as the library. Is it not
    possible to use std=c99 and turn on the library extensions with macros?
    Maybe what you need is not exactly obtainable but that route, but it
    often is.

    > But
    > I am sorta the Toolchain Guy for $dayjob, so I am the first point of contact
    > for basically all compiler problems. It's VERY rare for them to be caused
    > by the compiler including GNU extensions by default -- partially because
    > they're mostly things that have been compiled on such systems before. But
    > a TON of them wouldn't work if you didn't have those extensions on by default.
    >
    > I think it really is the right default for most Linux-like systems, at
    > least. Most programs are not intended to be strictly conforming.


    In terms of the library, no, but a great many more could be made
    portable (between compilers) by relying only on library extensions
    rather than language ones.

    --
    Ben.
     
    Ben Bacarisse, Feb 23, 2011
    #19
  20. Ceriousmall

    Tom St Denis Guest

    On Feb 18, 9:46 pm, (Kenny McCormack)
    wrote:
    > In article <>,
    > Jorgen Grahn  <> wrote:
    > ...
    >
    > >It should be well-known by now that you shouldn't use gcc's default
    > >mode. Say -ansi or -std=c99, and the problem goes away.

    >
    > And causes a bunch of other problems.


    If you use non-standard semantics.

    > >I don't know why they haven't made -ansi their default. For a long

    >
    > Sure you do.  For the same reason that the default mode of my car
    > doesn't require me to pump the pedals to make it go (in the manner that,
    > unfortunately, my bicycle does require).
    >


    Get pedals with clips, then you're never "pumping" the pedal as every
    part of the motion is involved in moving the bike.

    Tom
     
    Tom St Denis, Feb 24, 2011
    #20
    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. Nagaraj
    Replies:
    1
    Views:
    891
    Lionel B
    Mar 1, 2007
  2. timjowers
    Replies:
    7
    Views:
    4,373
    heyjude
    Feb 2, 2011
  3. Replies:
    1
    Views:
    460
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  4. Carter
    Replies:
    2
    Views:
    518
    Carter
    Mar 4, 2009
  5. fAnSKyer
    Replies:
    2
    Views:
    547
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page