LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   popen() doesn't behave as expected (https://www.linuxquestions.org/questions/programming-9/popen-doesnt-behave-as-expected-572851/)

Kimbo 07-27-2007 06:03 PM

popen() doesn't behave as expected
 
Hi!
I'm writing a small program that allow the user to compile a C++ program, but the problem is that popen() works on the commented code
Code:

//char *command="ls -l";
but not
Code:

char *command="g++ /home/kim/Programming/QPort/hello.cpp -o /home/kim/Programming/QPort/hello";
Why doesn't it show compiler output?
Here is my code so far:

Code:

void Simple::fileCompile()
{
  textOutput->insertPlainText("compiling...\n");

  FILE *fpipe;
  //char *command="ls -l";
  char *command="g++ /home/kim/Programming/QPort/hello.cpp -o /home/kim/Programming/QPort/hello";
  char line[256];
  QString stored_result;

  if ( !(fpipe = (FILE*)popen(command,"r")) )
  {  // If fpipe is NULL
      perror("Problems with pipe");
      exit(1);
  }

  while ( fgets( line, sizeof line, fpipe))
  {
    printf("%s", line);
    stored_result = line;
    textOutput->insertPlainText(stored_result);
  }
  pclose(fpipe);

  textOutput->insertPlainText("done!\n");
}

My program is using Qt4, but I doesn't think that have anything to do with the problem.

wjevans_7d1@yahoo.co 07-27-2007 07:46 PM

I tested whether your use of popen() was correct. Here's what I did:

Code:

wally:~/friday$ cat g.cpp
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  FILE *fpipe;

  //char *command="ls -l";
  char *command1="g++ /home/kim/Programming/QPort/hello.cpp -o /home/kim/Programming/QPort/hello";
  char *command2="g++ h.cpp -o h";
  char line[256];

  if ( !(fpipe = (FILE*)popen(command1,"r")) )
  {  // If fpipe is NULL
      perror("Problems with pipe");
      exit(1);
  }

  while ( fgets( line, sizeof line, fpipe))
  {
    printf("%s", line);
  }
  pclose(fpipe);

  if ( !(fpipe = (FILE*)popen(command2,"r")) )
  {  // If fpipe is NULL
      perror("Problems with pipe");
      exit(1);
  }

  while ( fgets( line, sizeof line, fpipe))
  {
    printf("%s", line);
  }
  pclose(fpipe);
}
wally:~/friday$ g++ g.cpp -o g
wally:~/friday$ ls
f.cpp  g  g.cpp  h.cpp  typescript
wally:~/friday$ g
g++: /home/kim/Programming/QPort/hello.cpp: No such file or directory
g++: no input files
wally:~/friday$ ls
f.cpp  g  g.cpp  h  h.cpp  typescript
wally:~/friday$ # Note that the file "h" has appeared.
wally:~/friday$

Everything worked. It ain't popen().

Edit:

Owe. Weight. You wanted "compiler output" perhaps to the screen?

If the compile goes perfectly well, the compiler doesn't send anything to standard output or standard error. That's why the file h "silently" appeared after running the above compile.

Hope this helps.

Kimbo 07-28-2007 05:33 AM

I have narrowed the problem to this line:

Code:

printf("%s", line);
It only works for "ls -l" but not "g++ myapp.cpp -o myapp"
Why is the variable "line" empty even if the compiler failed? :scratch:

Guttorm 07-28-2007 05:50 AM

Hi

It's because the error messages are sent to stderr, not stdout. And popen only read stdout. To get stderr when using popen, append 2>&1 to the command:

char *command1="g++ /home/kim/Programming/QPort/hello.cpp -o /home/kim/Programming/QPort/hello 2>&1";

Kimbo 07-28-2007 06:26 AM

Thanks Guttorm! This solved my little problem. :)


All times are GMT -5. The time now is 03:58 AM.