23

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).

2
  • 7
    Maybe you are compiling with options to GCC such as -ansi or -std=c99; these prevent the POSIX extensions from being visible. To make them visible, you have to request them, by (for example) specifying #define _XOPEN_SOURCE 500 (or 600 or 700) before you include any system header. Commented Mar 29, 2011 at 6:59
  • nice related post: <stackoverflow.com/questions/5378778/…> Commented Feb 22, 2013 at 15:41

5 Answers 5

17

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

1
  • I replaced -std=c17 with -std=gnu17. Worked. Commented Feb 3, 2022 at 22:35
14

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.

5
  • let me try it out. This is interesting because I have used pipe in the past (almost with the exact same code) and it worked. Any reason why? Commented Mar 29, 2011 at 5:56
  • Hmm, it seemed to calm down half of the popen and pclose complaints. Thanks for your help though. Commented Mar 29, 2011 at 6:03
  • 5
    You should define _POSIX_C_SOURCE or _XOPEN_SOURCE. The others are not (current) standards. _SVID_SOURCE may have been a standard at one time but the age of SVID is something like 15 years past... Commented Mar 29, 2011 at 12:16
  • 1
    I'm also still getting the same error. I take it that the answer is outdated. Commented May 12, 2014 at 12:03
  • 2
    The man page for popen(3) still shows the same requirements. The stdio.h header also clearly shows these requirements: 867 #if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \ 868 defined __USE_MISC) I don't get any errors here with -D_POSIX_C_SOURCE=2. Commented Jul 1, 2014 at 18:24
0

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>
0

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> 
... 
-6

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

1
  • 6
    Do not do that - it is a bad way to fix the problem. Use the standard headers, and get the defines right so that it compiles correctly. And you should probably accept an answer to this question. Commented Jun 16, 2011 at 22:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.