Php Exec将g ++错误重定向到文件[关闭]

I have a shell_exec in a php script, and I want to receive the error message from compilation ( if it exists ) as a return from shell_exec or to get it from a file...but I can't to this.

Here is a try

$compiler=shell_exec("g++ '$path''$file_name' -o '$path''$evaluate_id' >& '$path''$error'");

shell_exec function can return NULL both when an error occurs or the program produces no output.

It is not possible to detect execution failures using this function.

exec() should be used when access to the program exit code is required.

See http://www.php.net//manual/en/function.shell-exec.php

g++ prints error messages to stderr. shell_exec captures what g++ prints to stdout.

Redirect stderr to stdout or use a different function to invoke g++.

You probably want to use popen (and you want to redirect g++ stderr to its stdout), perhaps something like

 $handle = popen("g++ -Wall -c somesource.c 2>&1", "r");

Of course, replace the command to suit your needs.

You'll need later to use pclose (and you should read the handle with fgets, etc....)