c - 在 C11 中使用 strdup

标签 c gcc c11

我可以使用 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/

相关文章:

c - gcc 链接器如何在包含文件中工作

c - 在 C 中扩展宏

c - 相邻的字符和字符串文字标记

c - 具有内部链接的变量的暂定定义具有不完整的非数组类型 : conforming implementations show different behavior

c - 使用 scanf() 停止控制台输入

c - 程序未检测 if 语句

使用递归函数反转字符串的概念

c - 是否有 C 的库存储库?

optimization - 为什么 GCC 不在严格模式下使用内置函数?

c++ - MacOS 的 "-std=gnu++0x"选项