views:

70

answers:

4

I have a file named gcc.exe and I have a php page... I want to use:

gcc test.c

And, if there's some compilation error, I want to show it on the php page... But I can't. What happens is: If the file is correct, it generates the .exe file, but if it's wrong, nothing happens. What I want here, again, is to show all errors.

Anyone has an idea?

Edit[1]: Some code:

<?php
exec("gcc teste.c",$results);
print_r($results)
?>

Output:

*Nothing*

Edit[2]: I tryed put gcc output on a text file: This way (on prompt):

gcc test.c > teste.txt

Teorically, everythings on the screen go to text file. But this was unsucessfuly!



Edit[3]: quantumSoup: I tryed it, and...

array(0) { } int(1)

Nothing... Anything more?

+1  A: 

exec() reference

string exec ( string $command [, array &$output [, int &$return_var ]] )

Try

exec('gcc test.c', $output, $return);

var_dump($output);
var_dump($return);
quantumSoup
Might want to capture the return var as well. If gcc doesn't output anything, capturing the success exit code would be useful.
Marc B
I tryed it, but simply don't show nothing!
Richard
@Richard: Try my edited answer and post the results
quantumSoup
+3  A: 

gcc is likely to generate error output on stderr, which it would appear exec doesn't capture in the $results array. Solutions to this are likely to be kinda platform-specific. If you're working in a unix-like environment, it would be possible to do something like gcc foo.c 2>&1 to redirect stderr to stdout. I've no clue how one might achieve that on Windows, or if analogous concepts even exist. Perhaps someone else can elaborate if your question is platform-specific.

This is supported by the fact that piping to a file didn't work either.

Gian
Hum, i'm running on Windows (sht... rs). But is temporally... i want to stable it to run on Windows and on an Unix!
Richard
Try proc_open() and set up the third descriptiorspec/pipe to get stderr output.
mario
Exactly on Linux!
Richard
A: 

Use proc_open to capture stderr:

<?php
$process = proc_open('gcc test.c',
    array(
        1 => array("pipe", "w"),  //stdout
        2 => array("pipe", "w")   // stderr
    ), $pipes);

echo stream_get_contents($pipes[2]);

gives, for example:

sh: gcc: command not found
Artefacto
A: 

We use a PHP-based system that performs builds and captures results and errors under windows. We had the best success by having the PHP script create a batch file containing all the build commands, and then exec-ing that batch file. When it creates the batch file, it redirects the standard output of the compiler to one logfile and the standard error to another (commandname >>log.txt 2>>err.txt). Since your output is split between two files, it also helps to have the script generate lines that echo out descriptions of what is happening (such as "Building library libsockets.dll") so that you can better match up the two logs. Sometimes, it even helps to echo out every command before it is executed. Usually your build process will halt when an error is encountered (at least it probably should), so the contents of the error log typically match up with the tail end of the normal log.

bta