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

问题描述 投票:0回答:5

这是我的代码的一小段。

   #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);

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

我真的不确定。我查了一下popen,它所需要的只是提供的stdio.h。缺少什么,或者是我的代码的其余部分有问题(我真的不想显示更多代码,因为它是一个作业)。

c declaration pipe implicit stdio
5个回答
16
投票

-std=c99
-std=c11
等替换为
-std=gnu99
-std=gnu11


14
投票

正如手册页所说:

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

popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE
|| _SVID_SOURCE

所以你应该在

#define _BSD_SOURCE
ing
#include
之前
stdio.h
或其他之一。


0
投票

我在MinGW中遇到了这个问题;在它的 stdio.h 中我发现:

#ifndef NO_OLDNAMES
_CRTIMP __cdecl __MINGW_NOTHROW  FILE *  popen (const char *, const char *);
_CRTIMP __cdecl __MINGW_NOTHROW  int     pclose (FILE *);
#endif

事实证明,我的 gcc 命令行上有

-DNO_OLDNAMES=1
来修复另一个我什至不记得的另一个源文件中的一些模糊问题。这是我的简单修复:

#ifdef NO_OLDNAMES
  #undef NO_OLDNAMES
#endif
#include <stdio.h>

0
投票

正如@Conrad Mayer 等其他人评论的那样。

简洁,只需添加

#define _POSIX_C_SOURCE 200809L  // Define this before any includes

#include <stdlib.h>
... rest of code ...

解释

popen() 函数是 POSIX 标准的一部分,其声明可能取决于正确定义的功能测试宏。这应该确保 popen() 的必要声明可用。

如果问题仍然存在,您可以尝试在包含标头之前定义 _GNU_SOURCE,因为 popen() 也是 GNU 扩展:

#define _GNU_SOURCE
#include <stdlib.h> 
... 

-6
投票

我将 popen 和 pclose 的原型放在代码的顶部。看来问题已经解决了。

© www.soinside.com 2019 - 2024. All rights reserved.