即使 #include也隐式声明 popen已添加
这是我的代码的一小段。
#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。缺少什么,或者是我的代码的其余部分有问题(我真的不想显示更多代码,因为它是一个作业)。
This is tiny snippet of my code.
#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
I'm really unsure. I looked up popen and all it requires is stdio.h which is provided. What is missing, or is the problem in the rest of my code (I don't really want to show more code because its an a assignment).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
-std=c99
或-std=c11
等替换为-std=gnu99
或-std=gnu11
。Replace
-std=c99
or-std=c11
etc with-std=gnu99
or-std=gnu11
.正如手册页所述:
因此,您应该在
#include
stdio.h
之前#define _BSD_SOURCE
或其他其中之一。As the man page says:
So you should
#define _BSD_SOURCE
or one of the others before#include
ingstdio.h
.我在MinGW中遇到了这个问题;在它的 stdio.h 中我发现:
事实证明我在 gcc 命令行上有
-DNO_OLDNAMES=1
来修复另一个我什至不记得的另一个源文件中的一些模糊问题。这是我的简单修复:I ran into this problem in MinGW; in its stdio.h I found:
And it turns out I had
-DNO_OLDNAMES=1
on my gcc command line to fix some obscure problem in another source file that I can't even recall anymore. This was my easy fix:正如@Conrad Mayer 等其他人评论的那样。
简洁,只需添加
解释
popen() 函数是 POSIX 标准的一部分,其声明可能取决于正确定义的功能测试宏。这应该确保 popen() 的必要声明是可用的。
如果问题仍然存在,您可以尝试在包含标头之前定义 _GNU_SOURCE,因为 popen() 也是 GNU 扩展:
As others like @Conrad Mayer has commented.
Succinty, just add
Explanation
The popen() function is part of the POSIX standard, and its declaration might depend on the feature test macros being properly defined.This should ensure that the necessary declarations for popen() are available.
If the issue persists, you can try defining _GNU_SOURCE before including headers, as popen() is also a GNU extension:
我将 popen 和 pclose 的原型放在代码的顶部。问题似乎已经解决了。
I put the prototypes of popen and pclose at the top of my code. It seemed to have settled the problem.