1

I'm trying to compile C++ programs through PHP's exec() function. The command works when running directly from the command line and the program compiles perfectly, but in PHP exec() the error g++: error trying to exec 'cc1plus': execvp: No such file or directory surfaces.

I am running PHP-FPM 7.1 with the user www-data.

In PHP the command in question is exec('g++ /home/username/example.cpp -o main.o 2>&1', $out);

2
  • 1
    The shell in which g++ is executing does not have the same value for the PATH variable that your command line shell has. Commented Nov 8, 2017 at 14:05
  • 1
    ... and if your web server really has the ability to execute your compiler, you better hope that your web server is not Internet-accessible. Commented Nov 8, 2017 at 14:08

1 Answer 1

1

Your problem is that the user your php script runs as has a different PATH variable than your own user.
By using exec( echo $PATH) you should see that the PATH variable is different than if you use echo $PATH on your command line.
Try to use the full path to g++, you can find it by using which g++.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting collect2: fatal error: cannot find 'ld' compilation terminated., what should I do?