0

I really read every answer in this website, but I could not solve my problem.

I want to compile C file via gcc, and after various tests, I didn't get nothing.

Here's my php code:

    $filename = "file.c";
        $pathfile = exec("readlink -f " . $filename);
        $path = str_replace($filename, '', $pathfile);
        exec("gcc-4.8 ".$pathfile." -o ".$path."runner.out 2>&1",$output,$status);

        print_r($output);
        print_r($status);

This is the actual command that php executes:

gcc-4.8 /home/vagrant/sites/test/codetest/file.c -o /home/vagrant/sites/test/codetest/runner.out 2>&1

If I try to execute this line on terminal it works.

And this is the output if I try it via php:

Array ( [0] => gcc-4.8: error trying to exec 'cc1': execvp: No such file or directory ) 1

I can't figure out what's the problem, I also changed the permission of the codetest folder, and the permission to the gcc-4.8 to 777, it still not working.

AskMan
  • 65
  • 1
  • 1
  • 10
  • 2
    cc1 is probably not in the path of whatever shell php's firing up to handle your exec request. – Marc B Dec 21 '15 at 16:15
  • @MarcB how can I solve it? – AskMan Dec 21 '15 at 16:17
  • @user3621499 can you post the actual command run by PHP? I expect "gcc-4.8 /home/vagrant/sites/test/codetest/file.c -o /home/vagrant/sites/test/codetest/runner.out 2>&1" isn't the command being run. $command = "gcc-4.8 " . $pathfile . " -o " . $path . "runner.out 2>&1"; print_r($command); and see if those paths really do exist and have the file(s) as Marc B mentions. – Anthony Eischens Dec 21 '15 at 16:29
  • @AnthonyEischens if I run this code (http://i.imgur.com/j0hc9pr.png ), I get this output (http://i.imgur.com/9yvJ3U6.png ) – AskMan Dec 21 '15 at 18:44
  • @MarcB is still right, see Freddy's answer at http://stackoverflow.com/questions/8878676/gcc-error-trying-to-exec-cc1-execvp-no-such-file-or-directory-when-compil for some more explanation. – Anthony Eischens Dec 21 '15 at 19:58
  • @AnthonyEischens Marc is right, I read the Freddy's comment but I really cannot find the path of the cc1 file (and I cannot find it even if I do a 'whereis'), is there a way to force php using the same shell I'm using when the compiling command works? – AskMan Dec 21 '15 at 21:09
  • 1
    A few things to note:: 1) a server, for instance `apache2` has configuration files. Those configuration files can allow or deny any activity, that activity can be an external reference, a user, a group of users, a directory, etc etc etc. 2) PHP is running on the server, not your local computer. 3) all activities have to go thorough the server. It could well be that users are not allowed to access gcc as that is a security risk. – user3629249 Dec 25 '15 at 01:14

0 Answers0