我可以使用 gcc 4.7.2 版编译以下内容
#include <string.h>
int main(){
char text[] = "String duplicate";
char* dup = strdup(text);
return 0;
}
但是当我使用 --std=c11 标志时,我收到以下警告:
warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
warning: initialization makes pointer from integer without a cast [enabled by default]
是什么改变导致了这个警告?
最佳答案
阅读 strdup 的手册
man strdup
你可以找到
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
表示strdup符合SVr4, 4.3BSD, POSIX.1-2001。
所以你可以通过以下方式摆脱警告
gcc -D_BSD_SOURCE -std=c11 <your source file>
我猜这些警告是由于 c11 没有启用上述宏之一引起的。
关于c - 在 C11 中使用 strdup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19641460/