LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-24-2004, 02:19 AM   #1
manikantha
Member
 
Registered: Sep 2004
Location: Guntur,India
Distribution: FC3 - 64-Bit
Posts: 56

Rep: Reputation: 21
"Compiling c prog using gcc in PHP"


Hi all,

I know that we can use UNIX commands in PHP by the 'system' or 'exec' functions in PHP ....

Main Problem is this:::

When I do like this..

$ls=system("ls");
echo $ls;

/* It is printing all the files in the directory */

But when I do this ::::

$filename="somefile.txt" /* Remember it is a text file */

$output=system("gcc $filename");


/* This is certainly an error And I want the error to be printed on to the page */

print $output; /* Is not giving me what I want */

Can neone tell me how to get the error messages like which we usually get when we compile a text file in LInux with gcc compiler or any other compiler


ThanX in Anticipation.
 
Old 09-24-2004, 03:28 AM   #2
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
go to read function popen
http://sg2.php.net/manual/en/function.popen.php
 
Old 09-24-2004, 03:30 AM   #3
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
or alternatively sh_exec()
http://sg2.php.net/manual/en/function.shell-exec.php
 
Old 09-24-2004, 03:30 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I take it that somefile.txt is not a C source file (which is a special case of text file). In this case, do you want to capture the errors produced by gcc?

Error messages are usually sent to the standard error output stream, rather than the standard output that you are collecting with the system() command. You can use the shell to redirect standard error to standard output, so it can be captured, using 2>&1 like this:
Code:
gcc somefile.txt 2>&1
I don't know if PHP will send the parameter to system() through a shell. If not, you'll have to invoke one explicitly:
Code:
$output=system("sh -c 'gcc $filename 2>&1'");
The sh invokes the shell, the -c passes the next parameter as a command to the shell, and the single quotes around everything else keeps it together as one parameter.

Hope that helps,

— Robert J. Lee
 
Old 09-24-2004, 04:05 AM   #5
manikantha
Member
 
Registered: Sep 2004
Location: Guntur,India
Distribution: FC3 - 64-Bit
Posts: 56

Original Poster
Rep: Reputation: 21
ThanQ U For U help..... It's working fine
 
  


Reply



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
GCC Compiling (Linux), "Segmentation Fault" Kenji Miyamoto Programming 2 01-13-2005 01:44 AM
When "function pointer" meets "template"... I can't get rid of this compiling er cyu021 Linux - Software 3 12-17-2004 07:52 PM
Redhat 7.3 PHP 4.3.4 "upgrade" not compiling fatbaldbob Red Hat 0 03-02-2004 04:40 PM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM
php ide with "break" and "trace" Kayaker Programming 3 04-25-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration