Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popen implicitly declared even though #include <stdio.h> is added

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

like image 337
Chris Allen Avatar asked Mar 29 '11 05:03

Chris Allen


2 Answers

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.

like image 141
Conrad Meyer Avatar answered Oct 05 '22 05:10

Conrad Meyer


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

like image 35
weakish Avatar answered Oct 05 '22 04:10

weakish