LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2007, 06:03 PM   #1
Kimbo
LQ Newbie
 
Registered: Feb 2005
Distribution: Ubuntu 7.04
Posts: 19

Rep: Reputation: 0
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.
 
Old 07-27-2007, 07:46 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.

Last edited by wjevans_7d1@yahoo.co; 07-27-2007 at 07:48 PM.
 
Old 07-28-2007, 05:33 AM   #3
Kimbo
LQ Newbie
 
Registered: Feb 2005
Distribution: Ubuntu 7.04
Posts: 19

Original Poster
Rep: Reputation: 0
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?
 
Old 07-28-2007, 05:50 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,141

Rep: Reputation: 233Reputation: 233Reputation: 233
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";
 
Old 07-28-2007, 06:26 AM   #5
Kimbo
LQ Newbie
 
Registered: Feb 2005
Distribution: Ubuntu 7.04
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks Guttorm! This solved my little problem.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using popen and execl cdog Linux - Software 3 02-28-2007 06:44 PM
popen for windows csfalcon Programming 6 11-30-2005 08:21 AM
popen output not appear dimsh Programming 4 10-22-2005 02:56 AM
Multithreading and os.popen in wxPython wapcaplet Programming 0 12-29-2004 07:58 PM
C Popen gold5angel Programming 2 04-17-2004 07:07 PM


All times are GMT -5. The time now is 02:00 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration