Thread: Compiler error: warning: implicit declaration of function 'strdup'

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Well I'm using the gcc compiler.

    Quote Originally Posted by brewbuck View Post
    EDIT: Haha, nevermind. I forgot that strdup() isn't standard C, so if your compiler doesn't support it... That's tough. Write it yourself:

    Code:
    char *strdup(const char *str)
    {
        int n = strlen(str) + 1;
        char *dup = malloc(n);
        if(dup)
        {
            strcpy(dup, str);
        }
        return dup;
    }
    Thanks. I'm not sure why strdup() not being in standard C effects anything. According to the gcc API, strdup is supported in gcc. However, I'm compiling with the ansi and pedantic flags--does this somehow exclude the strdup() from being #include'd when I include string.h?

    Anyways thanks for the implementation of strdup...I was afraid that I'd have to waste brain cells writing my own strdup

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TalonStriker View Post
    Thanks. I'm not sure why strdup() not being in standard C effects anything.
    Because if it's not standard, then there is no standard place where the prototype will live. On my gcc installation here, strdup() is defined in string.h. On yours, it clearly is not. Is that not enough to convince you that non-standard features are, in fact, non-standard?

    According to the gcc API, strdup is supported in gcc. However, I'm compiling with the ansi and pedantic flags--does this somehow exclude the strdup() from being #include'd when I include string.h?
    That MIGHT have something to do with it. Not sure though.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you compile with -ansi the compiler will not use non-standard functions, and if you compile with -pedantic it will also warn about some non-standard behaviours in the code you write. It won't make errors of it unless you use -Werror [which will make errors of all warnings]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by matsp View Post
    It won't make errors of it unless you use -Werror [which will make errors of all warnings]--
    Mats
    Ah... I see... suppose I hadn't used -Werror, it would have created the object code...but would that object code be "wrong" (i.e. not run properly after linking)?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Possibly. I actually would recommend using -Werror for new projects - it may not work on old projects which already have warnings [or if you dramatically change the compiler version, e.g. changing from a 3.4 to a 4.2 version of gcc may throw a few new warnings in the mix, and in a large project that could cause major issues - of course, changing the compiler on large pieces of code is always a "interesting" project]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Because if it's not standard, then there is no standard place where the prototype will live. On my gcc installation here, strdup() is defined in string.h. On yours, it clearly is not. Is that not enough to convince you that non-standard features are, in fact, non-standard?
    Actually, I didn't quite understand what "standard" meant in this context. From your explanation, for a function to be standard, its prototype should exist in the same library in all compilers....or am I jumping to conclusions?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TalonStriker View Post
    Actually, I didn't quite understand what "standard" meant in this context. From your explanation, for a function to be standard, its prototype should exist in the same library in all compilers....or am I jumping to conclusions?
    For a function to be standard, it must be described in the document ISO-9899 (i.e. the "C standard"). That's what I mean.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 2
    Last Post: 11-08-2007, 02:33 AM
  3. Replies: 12
    Last Post: 10-24-2007, 08:10 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM