即使 #include也隐式声明 popen已添加

发布于 2024-10-28 05:48:02 字数 906 浏览 3 评论 0原文

这是我的代码的一小段。

   #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

将军与妓 2024-11-04 05:48:02

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

Replace -std=c99 or -std=c11 etc with -std=gnu99 or -std=gnu11.

呢古 2024-11-04 05:48:02

正如手册页所述:

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

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

因此,您应该在 #include stdio.h 之前 #define _BSD_SOURCE 或其他其中之一。

As the man page says:

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

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

So you should #define _BSD_SOURCE or one of the others before #includeing stdio.h.

情何以堪。 2024-11-04 05:48:02

我在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>

I ran into this problem in MinGW; in its stdio.h I found:

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

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:

#ifdef NO_OLDNAMES
  #undef NO_OLDNAMES
#endif
#include <stdio.h>
━╋う一瞬間旳綻放 2024-11-04 05:48:02

正如@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> 
... 

As others like @Conrad Mayer has commented.

Succinty, just add

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

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

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:

#define _GNU_SOURCE
#include <stdlib.h> 
... 
夏の忆 2024-11-04 05:48:02

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

I put the prototypes of popen and pclose at the top of my code. It seemed to have settled the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文