Question

I am able to compile the following using gcc version 4.7.2

   #include <string.h>

   int main(){
    char text[] = "String duplicate";
    char* dup = strdup(text);
    return 0;

   }

But when I used the --std=c11 flag, I get the following warning:

warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
warning: initialization makes pointer from integer without a cast [enabled by default]

What changed to cause this warning?

Was it helpful?

Solution

Read the manual of strdup by

man strdup

You can find that

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

strdup(): _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L

It denotes that strdup conforms to SVr4, 4.3BSD, POSIX.1-2001.

So you can get rid of the warnings by

gcc -D_BSD_SOURCE -std=c11 <your source file>

I guess the warnings are caused by c11 not enabling one of the above macros.

OTHER TIPS

you want --std=gnu11 or --std=c11 -D_GNU_SOURCE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top