在在线c编译器中显示错误

时间:2013-01-12 14:50:42

标签: php c compiler-errors

大家好我有一个c编译器并运行显示输出但问题是它不会显示错误....

      shell_exec("gcc xyz.c -o ab.out ");

      $output=exec("./ab.out");
      echo $output;

因此它显示输出但不会在编译时发生任何错误。 任何帮助都得到了适当的赞赏。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

从您运行的命令输出的任何错误都将转到STDERR,并且exec,shell_exec函数都不会为您提供。一种方法是重定向

exec("gcc test.c 2>&1", $out);

最干净的方法是使用proc_open功能。

$descriptorspec = array(
   1 => array("pipe", "w"),  // stdout
   2 => array("pipe", "w") // stderr 
);

$process = proc_open('gcc test.c', $descriptorspec, $pipes);

if (is_resource($process)) {
    $stderr = stream_get_contents($pipes[2]);
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    $return_value = proc_close($process);
}