【问题标题】:Using strdup in C11在 C11 中使用 strdup
【发布时间】:2013-10-28 17:39:09
【问题描述】:

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

什么变化导致了这个警告?

【问题讨论】:

标签: c gcc c11


【解决方案1】:

阅读strdup的手册

man strdup

你可以找到

glibc 的功能测试宏要求(参见 feature_test_macros(7)):

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

表示strdup符合SVr4、4.3BSD、POSIX.1-2001。

所以你可以摆脱警告

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

我猜这些警告是由于 c11 没有启用上述宏之一。

【讨论】:

  • _BSD_SOURCE 是一个不寻常的选择。使用 -D_POSIX_C_SOURCE=200809L 是更广泛和有用的标准。
【解决方案2】:

你想要 --std=gnu11 或 --std=c11 -D_GNU_SOURCE

【讨论】:

    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 2018-01-10
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多