c-为什么-std=c11和gcc在stdio.h中隐藏popen



我想使用popen。它在stdio.h中。我包括了它,但编译器看不到它与-std=c11。它在没有-std=c11的情况下编译。

#include <stdio.h>
int main(void)
{
popen("ls *","r");
}

gcc-std=c11 popen_test.c

popen_test.c:在函数'main'中:
popen_test.c:5:4:警告:函数'popen'的隐式声明[-Wimplicit函数声明]

popen("ls *","r");
^~~~~

它与一起隐藏在stdio.h中

#ifdef __USE_POSIX2

手册页显示,如果:

_POSIX_C_SOURCE>=2||/*Glibc版本<=2.19:*/_BSD_SOURCE ||_SVID_SOURCE

popen不是C的一部分。要获得它,您需要在包含任何内容之前使用功能测试宏启用它。

最简单的方法是在顶部使用#define _GNU_SOURCE(或者在编译器调用中使用-D_GNU_SOURCE(。

使用-std=c11:编译

#define _GNU_SOURCE
#include <stdio.h>
int main(void)
{
popen("ls *","r");
}

最新更新