这是我的一小段代码。
#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。缺少什么,或者是我的其余代码中的问题(我真的不想显示更多代码,因为它是一个作业)。
最佳答案
正如手册页所说:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE
|| _SVID_SOURCE
因此,在 #include
ing stdio.h
之前,您应该#define _BSD_SOURCE
或其他之一。
关于c - 即使添加了 #include <stdio.h> 也隐式声明了 popen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5468326/