popen隐式声明,即使添加了#include <stdio.h>

Chr*_*len 14 c declaration pipe stdio implicit

这是我的代码的一小部分.

   #include <stdio.h>
   #include <unistd.h>
   #include <stdlib.h>
   #include <time.h>
   #include <sys/stat.h>
   #include <sys/wait.h>
   #include <sys/types.h>
   #include <string.h>
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <arpa/inet.h>
    ...

   FILE * pipe;
    ...

   pipe = popen ("ls /tmp -1", "r");
    ...
   pclose(pipe);
Run Code Online (Sandbox Code Playgroud)
blarg.c:106: warning: implicit declaration of function ‘popen’

blarg.c:106: warning: assignment makes pointer from integer without a cast

blarg.c:112: warning: implicit declaration of function ‘pclose’

blarg.c:118: warning: assignment makes pointer from integer without a cast
Run Code Online (Sandbox Code Playgroud)

我真的不确定.我查了一下popen,它需要的是stdio.h.缺少什么,或者是我的其余代码中的问题(我真的不想显示更多的代码,因为它是一个赋值).

Con*_*yer 13

正如手册页所说:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE
|| _SVID_SOURCE
Run Code Online (Sandbox Code Playgroud)

所以,你应该#define _BSD_SOURCE还是之前别人的一个#include荷兰国际集团stdio.h.

  • 你应该定义`_POSIX_C_SOURCE`或`_XOPEN_SOURCE`.其他不是(现行)标准.`_SVID_SOURCE`可能曾经是一个标准,但SVID的年龄就像过去的15年...... (4认同)
  • popen(3)的手册页仍然显示相同的要求.stdio.h头文件也清楚地显示了这些要求:867 #if(定义__USE_POSIX2 ||定义__USE_SVID ||定义__USE_BSD ||\868定义__USE_MISC)这里没有任何错误-D_POSIX_C_SOURCE = 2. (2认同)

wea*_*ish 7

更换-std=c99-std=c11用等-std=gnu99-std=gnu11.