1

大家好,我有 ac 编译器启动并运行显示输出,但问题是它不显示错误....

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

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

所以它显示输出但在编译时没有发生任何错误。任何帮助都将受到应有的赞赏。提前致谢。

4

1 回答 1

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);
}
于 2013-01-12T15:06:39.417 回答