Warnings with gcc

Discussion in 'C Programming' started by thibault.langlois@di.fc.ul.pt, Nov 22, 2005.

  1. Guest

    Hello,
    I get a warning when I compile:
    #include <string.h>

    int main (int argc, char ** argv)
    {
    char * s;
    s = strdup("a string");
    return 0;
    }

    with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :

    $ gcc -Wall -ansi warning.c
    warning.c: In function `main':
    warning.c:7: warning: implicit declaration of function `strdup'
    warning.c:7: warning: assignment makes pointer from integer without a
    cast

    Does anybody knows how can I supress this warning ?

    if I put a cast : s = (char *) strdup("a string");
    I get another warning message:
    warning.c:7: warning: implicit declaration of function `strdup'

    I do not understand why this happens because strdup is declared in
    string.h

    Thanks,

    Thibault Langlois
    , Nov 22, 2005
    #1
    1. Advertising

  2. rayw Guest

    <> wrote in message
    news:...
    > Hello,
    > I get a warning when I compile:
    > #include <string.h>
    >
    > int main (int argc, char ** argv)
    > {
    > char * s;
    > s = strdup("a string");
    > return 0;
    > }
    >
    > with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :
    >
    > $ gcc -Wall -ansi warning.c
    > warning.c: In function `main':
    > warning.c:7: warning: implicit declaration of function `strdup'
    > warning.c:7: warning: assignment makes pointer from integer without a
    > cast
    >
    > Does anybody knows how can I supress this warning ?
    >
    > if I put a cast : s = (char *) strdup("a string");
    > I get another warning message:
    > warning.c:7: warning: implicit declaration of function `strdup'
    >
    > I do not understand why this happens because strdup is declared in
    > string.h


    Well, it can't be can it - unless the compiler's wrong?

    Have you confirmed that it's in [your] string.h?

    Does your preprocessed output contain it?

    Does it link?

    rayw
    rayw, Nov 22, 2005
    #2
    1. Advertising

  3. pete Guest

    pete, Nov 22, 2005
    #3
  4. Richard Bos Guest

    "" <> wrote:

    > I get a warning when I compile:
    > #include <string.h>
    >
    > int main (int argc, char ** argv)
    > {
    > char * s;
    > s = strdup("a string");
    > return 0;
    > }


    > warning.c:7: warning: implicit declaration of function `strdup'
    >
    > I do not understand why this happens because strdup is declared in
    > string.h


    strdup() is not ISO C, and should therefore _not_ be declared in
    <string.h> in a conforming implementation. Looks like gcc with -ansi is
    conforming in this respect (as in most, perhaps all, others).

    Richard
    Richard Bos, Nov 22, 2005
    #4
  5. wrote:
    > Hello,
    > I get a warning when I compile:
    > #include <string.h>
    >
    > int main (int argc, char ** argv)


    this is better than int main () or void main (void) which we see too
    often around here, but since you are not using either of these
    variables, why not just int main (void)?

    > {
    > char * s;
    > s = strdup("a string");


    strdup is not a Standard C function, it is a BSD extension. You
    specified the -ansi option when you tried to compile this telling the
    compiler you want to compile as a strictly conforming program. This
    option defines the macro __STRICT_ANSI__ which will keep the prototype
    for strdup from being visible from string.h. You can either remove the
    -ansi switch, or place the following define before your include
    directive:
    #define _GNU_SOURCE

    > return 0;
    > }
    >
    > with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :
    >
    > $ gcc -Wall -ansi warning.c
    > warning.c: In function `main':
    > warning.c:7: warning: implicit declaration of function `strdup'


    The prototype for strdup is not in scope, hence the diagnostic, see
    above.

    > warning.c:7: warning: assignment makes pointer from integer without a
    > cast


    Since the prototype for strdup is not in scope, the compiler assumes it
    returns an int which you are storing into a pointer without a cast,
    this is required to produce a diagnostic.

    > Does anybody knows how can I supress this warning ?


    The -w switch turns off all warnings but this justs hides the problem,
    fix the issue instead.

    > if I put a cast : s = (char *) strdup("a string");
    > I get another warning message:
    > warning.c:7: warning: implicit declaration of function `strdup'


    Since you cast the result of strdup, which the compiler thinks is an
    int, to a char * a diagnostic is not required. You still have the
    issue of strdup not being declared. If you are using a cast to supress
    a compiler diagnostic, this is a good clue you are doing something
    wrong.

    Robert Gamble
    Robert Gamble, Nov 22, 2005
    #5
  6. Barry Guest

    <> wrote in message
    news:...
    > Hello,
    > I get a warning when I compile:
    > #include <string.h>
    >
    > int main (int argc, char ** argv)
    > {
    > char * s;
    > s = strdup("a string");
    > return 0;
    > }
    >
    > with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :
    >
    > $ gcc -Wall -ansi warning.c
    > warning.c: In function `main':
    > warning.c:7: warning: implicit declaration of function `strdup'
    > warning.c:7: warning: assignment makes pointer from integer without a
    > cast
    >
    > Does anybody knows how can I supress this warning ?
    >
    > if I put a cast : s = (char *) strdup("a string");
    > I get another warning message:
    > warning.c:7: warning: implicit declaration of function `strdup'
    >
    > I do not understand why this happens because strdup is declared in
    > string.h
    >
    > Thanks,
    >
    > Thibault Langlois
    >


    drop the "-ansi" strdup is a non-ANSI extension (at least according to gcc).

    barry
    Barry, Nov 22, 2005
    #6
  7. Skarmander Guest

    Barry wrote:
    > <> wrote in message
    > news:...
    >
    >>I get a warning when I compile:
    >>#include <string.h>
    >>
    >>int main (int argc, char ** argv)
    >>{
    >> char * s;
    >> s = strdup("a string");
    >> return 0;
    >>}
    >>
    >>with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :
    >>
    >>$ gcc -Wall -ansi warning.c
    >>warning.c: In function `main':
    >>warning.c:7: warning: implicit declaration of function `strdup'

    <snip>
    >
    > drop the "-ansi" strdup is a non-ANSI extension (at least according to gcc).
    >

    That's not quite it. gcc would in fact be allowed to provide strdup()
    even in ANSI mode, but it chooses not to, presumably to help people
    catch conformance errors (which is -ansi's primary purpose).

    (To be even more precise, it actually depends on the standard library
    implementation used with gcc.)

    S.
    Skarmander, Nov 22, 2005
    #7
  8. Barry Guest

    "Skarmander" <> wrote in message
    news:43830ee7$0$11076$4all.nl...
    > Barry wrote:
    > > <> wrote in message
    > > news:...
    > >
    > >>I get a warning when I compile:
    > >>#include <string.h>
    > >>
    > >>int main (int argc, char ** argv)
    > >>{
    > >> char * s;
    > >> s = strdup("a string");
    > >> return 0;
    > >>}
    > >>
    > >>with gcc (GCC) 3.3.5 (Debian 1:3.3.5-8ubuntu2) :
    > >>
    > >>$ gcc -Wall -ansi warning.c
    > >>warning.c: In function `main':
    > >>warning.c:7: warning: implicit declaration of function `strdup'

    > <snip>
    > >
    > > drop the "-ansi" strdup is a non-ANSI extension (at least according to

    gcc).
    > >

    > That's not quite it. gcc would in fact be allowed to provide strdup()
    > even in ANSI mode, but it chooses not to, presumably to help people
    > catch conformance errors (which is -ansi's primary purpose).
    >
    > (To be even more precise, it actually depends on the standard library
    > implementation used with gcc.)
    >
    > S.


    Thanks for the correction. My post was based solely on the comments in the
    gcc
    header files.

    barry
    Barry, Nov 22, 2005
    #8
  9. jacob navia Guest

    pete wrote:
    > wrote:
    >
    >
    >>I do not understand why this happens because strdup is declared in
    >>string.h

    >
    >
    > strdup is not in string.h
    >
    > news:gnu.gcc
    >
    > http://www.ungerhu.com/jxh/clc.welcome.txt
    >

    strdup is in string.h in my linux system with
    gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)

    jacob
    jacob navia, Nov 22, 2005
    #9
  10. Skarmander Guest

    jacob navia wrote:
    > pete wrote:
    >
    >> wrote:
    >>
    >>
    >>> I do not understand why this happens because strdup is declared in
    >>> string.h

    >>
    >>
    >>
    >> strdup is not in string.h
    >>
    >> news:gnu.gcc
    >>
    >> http://www.ungerhu.com/jxh/clc.welcome.txt
    >>

    > strdup is in string.h in my linux system with
    > gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)
    >

    strdup() is *not necessarily* in string.h. It's a nonstandard function;
    the standard doesn't define it. Implementations are allowed to define
    additional functions beginning with "str" in <string.h>, however (and if
    <string.h> is included, a program may not define such functions).

    S.
    Skarmander, Nov 22, 2005
    #10
  11. pete Guest

    jacob navia wrote:
    >
    > pete wrote:


    > > strdup is not in string.h
    > >
    > > news:gnu.gcc
    > >
    > > http://www.ungerhu.com/jxh/clc.welcome.txt
    > >

    > strdup is in string.h in my linux system with
    > gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)


    Maybe there's a newsgroup or two,
    listed in the above quoted URL,
    where that's on topic.

    --
    pete
    pete, Nov 22, 2005
    #11
  12. Flash Gordon Guest

    Skarmander wrote:

    <snip>

    > strdup() is *not necessarily* in string.h. It's a nonstandard function;
    > the standard doesn't define it. Implementations are allowed to define
    > additional functions beginning with "str" in <string.h>, however (and if
    > <string.h> is included, a program may not define such functions).


    We've had this discussion here before, and not too many weeks back IIRC.
    programs are not allowed to declare such identifiers with external
    linkage even if they *don't* include string.h
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Nov 22, 2005
    #12
  13. In article <438319e2$0$6670$> jacob navia <> writes:
    ....
    > strdup is in string.h in my linux system with
    > gcc version 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)


    Unconditional? strdup is also in string.h here (Solaris), as follows:

    #if defined(__EXTENSIONS__) || (__STDC__ == 0 && \
    !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || \
    defined(_XPG4_2)
    extern char *strdup(const char *);
    #endif

    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 22, 2005
    #13
  14. wrote:
    > Hello,
    > I get a warning when I compile:
    > #include <string.h>
    >
    > int main (int argc, char ** argv)
    > {
    > char * s;
    > s = strdup("a string");
    > return 0;
    > }


    You should. strdup() isn't an ansi compliant function.


    > Does anybody knows how can I supress this warning ?


    Don't use strdup()

    > I do not understand why this happens because strdup is declared in
    > string.h


    Because you told gcc when using the -ansi directive.



    Igmar
    Igmar Palsenberg, Nov 22, 2005
    #14
  15. Jordan Abel Guest

    On 2005-11-22, Flash Gordon <> wrote:
    > Skarmander wrote:
    >
    > <snip>
    >
    >> strdup() is *not necessarily* in string.h. It's a nonstandard function;
    >> the standard doesn't define it. Implementations are allowed to define
    >> additional functions beginning with "str" in <string.h>, however (and if
    >> <string.h> is included, a program may not define such functions).

    >
    > We've had this discussion here before, and not too many weeks back IIRC.
    > programs are not allowed to declare such identifiers with external
    > linkage even if they *don't* include string.h


    What about with static linkage?

    [incidentally, stdlib.h as well as string.h for the 'str' prefix]
    Jordan Abel, Nov 22, 2005
    #15
  16. Skarmander Guest

    Flash Gordon wrote:
    > Skarmander wrote:
    >
    > <snip>
    >
    >> strdup() is *not necessarily* in string.h. It's a nonstandard
    >> function; the standard doesn't define it. Implementations are allowed
    >> to define additional functions beginning with "str" in <string.h>,
    >> however (and if <string.h> is included, a program may not define such
    >> functions).

    >
    >
    > We've had this discussion here before, and not too many weeks back IIRC.
    > programs are not allowed to declare such identifiers with external
    > linkage even if they *don't* include string.h


    That's quite possible. I didn't mean to imply otherwise. They are
    unambiguously not allowed to declare such functions regardless of
    linkage if <string.h> *is* included, and that is all I said.

    S.
    Skarmander, Nov 22, 2005
    #16
  17. Guest

    Thank you (and all other posters) for your answer.
    I found that using -sdt=gnu99 instead of -ansi is enough to supress the
    warning.

    I am a bit confused by ansi/iso features and someone may be kind engouh
    to explain me or give some pointers where I can find the information.
    1. The ansi/iso standards are about the language itself or about the
    what K&R call the "standard library" ? or both ?
    2. My source of info is the K&R book, the edition with the "ansi C" on
    the cover. In the appendix the authors write about the "Standard
    Library ... defined by the ANSI standard". The say also : "The standard
    liabrary is not part of the C language proper but an environment that
    supports the standard C and will provide the function declarations
    [...] of this library."
    Among the standard headers there is the string.h that should contain
    strdup().
    That is why I was expecting that -ansi would not cause such warnings.
    Is it a gcc-specific feature/bug ?

    Thibault Langlois
    , Nov 22, 2005
    #17
  18. Skarmander Guest

    wrote:
    > Thank you (and all other posters) for your answer.
    > I found that using -sdt=gnu99 instead of -ansi is enough to supress the
    > warning.
    >

    This does not "suppress" the warning, actually, because with -std=gnu99,
    strdup() is defined by <string.h>. Using -std=gnu99 enables many other
    features that are not part of standard C, by the way.

    > I am a bit confused by ansi/iso features and someone may be kind engouh
    > to explain me or give some pointers where I can find the information.
    > 1. The ansi/iso standards are about the language itself or about the
    > what K&R call the "standard library" ? or both ?


    Both.

    > 2. My source of info is the K&R book, the edition with the "ansi C" on
    > the cover. In the appendix the authors write about the "Standard
    > Library ... defined by the ANSI standard". The say also : "The standard
    > liabrary is not part of the C language proper but an environment that
    > supports the standard C and will provide the function declarations
    > [...] of this library."


    And that's right. It's not part of the language proper, but the standard
    covers conforming implementations of C environments. That includes the
    library.

    > Among the standard headers there is the string.h that should contain
    > strdup().


    No. If K&R says this, it's wrong. But read carefully if that's what's
    really said.

    > That is why I was expecting that -ansi would not cause such warnings.
    > Is it a gcc-specific feature/bug ?
    >

    Not really. strdup() doesn't have to exist, period. If your
    implementation provides it, that's nice. Relying on its existence makes
    your program unportable to platforms that don't have it.

    strdup() is actually defined by the POSIX standard and it is available
    on many platforms, it's just not part of ANSI C.

    S.
    Skarmander, Nov 22, 2005
    #18
  19. Flash Gordon Guest

    Skarmander wrote:
    > Flash Gordon wrote:
    >> Skarmander wrote:
    >>
    >> <snip>
    >>
    >>> strdup() is *not necessarily* in string.h. It's a nonstandard
    >>> function; the standard doesn't define it. Implementations are allowed
    >>> to define additional functions beginning with "str" in <string.h>,
    >>> however (and if <string.h> is included, a program may not define such
    >>> functions).

    >>
    >> We've had this discussion here before, and not too many weeks back
    >> IIRC. programs are not allowed to declare such identifiers with
    >> external linkage even if they *don't* include string.h

    >
    > That's quite possible. I didn't mean to imply otherwise. They are
    > unambiguously not allowed to declare such functions regardless of
    > linkage if <string.h> *is* included, and that is all I said.


    OK, it's just that the way you worded it someone might think that if
    they did not include string.h they could declare their own external
    objects with such names.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Nov 22, 2005
    #19
  20. Flash Gordon Guest

    Jordan Abel wrote:
    > On 2005-11-22, Flash Gordon <> wrote:
    >> Skarmander wrote:
    >>
    >> <snip>
    >>
    >>> strdup() is *not necessarily* in string.h. It's a nonstandard function;
    >>> the standard doesn't define it. Implementations are allowed to define
    >>> additional functions beginning with "str" in <string.h>, however (and if
    >>> <string.h> is included, a program may not define such functions).

    >> We've had this discussion here before, and not too many weeks back IIRC.
    >> programs are not allowed to declare such identifiers with external
    >> linkage even if they *don't* include string.h

    >
    > What about with static linkage?


    The relevant part of section 7.1.3 from N1124 is:
    | — All identifiers with external linkage in any of the following
    | subclauses (including the future library directions) are always
    | reserved for use as identifiers with external linkage.157)
    | — Each identifier with file scope listed in any of the following
    | subclauses (including the future library directions) is reserved for
    | use as a macro name and as an identifier with file scope in the same
    | name space if any of its associated headers is included.

    So I would say that you can have:
    static int strcpy(void) {return 0};
    if you want and have not included string.h or stdlib.h

    > [incidentally, stdlib.h as well as string.h for the 'str' prefix]


    Yes, you are correct. str[a-z] are reserved if you include stdlib.h
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Nov 22, 2005
    #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. Charlie Zender
    Replies:
    12
    Views:
    1,077
    Peter Pichler
    Jan 3, 2004
  2. Charlie Zender

    Removing GCC compiler warnings from fabsf(), sqrtf()...

    Charlie Zender, Jan 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    621
    Ben Pfaff
    Jan 4, 2004
  3. asdf

    gcc warnings

    asdf, Jun 26, 2006, in forum: C++
    Replies:
    4
    Views:
    368
  4. asdf

    gcc warnings again

    asdf, Jun 26, 2006, in forum: C++
    Replies:
    12
    Views:
    583
    Jerry Coffin
    Jun 29, 2006
  5. Ted Sung
    Replies:
    1
    Views:
    307
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page