LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "Compiling c prog using gcc in PHP" (https://www.linuxquestions.org/questions/programming-9/compiling-c-prog-using-gcc-in-php-234619/)

manikantha 09-24-2004 02:19 AM

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

onnyloh 09-24-2004 03:28 AM

go to read function popen
http://sg2.php.net/manual/en/function.popen.php

onnyloh 09-24-2004 03:30 AM

or alternatively sh_exec()
http://sg2.php.net/manual/en/function.shell-exec.php

rjlee 09-24-2004 03:30 AM

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

manikantha 09-24-2004 04:05 AM

ThanQ U For U help..... It's working fine


All times are GMT -5. The time now is 11:54 AM.