Reputation: 527
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?
Upvotes: 14
Views: 5923
Reputation: 1536
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.
Upvotes: 9