Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C from PHP with exec error trying to exec 'cc1'

I am trying to compile a C program from PHP with exec, and with the Laravel Framework. But I dont think this is the problem, because I can compile and execute C programs from terminal without problems. And if you know from tinker in Laravel 5, so the problem is from PHP. But I can`t find the error I think the problem is form different versions of GCC but why let me compile from terminal.

I get this error when I do that from PHP. If I compile from terminal it works but from php not.

    $path = public_path("testing/cosas.out");
    exec("gcc testing/pruebaC.c -o testing/from.out 2>&1",$output,$status);
    dd($output,$status); //is like var_dump

AND I GET THIS !!

gcc: error trying to exec 'cc1': execvp: No such file or directory"

I checked the permissions and are right (in fact I did chmod 777 in my desperation).

Also I tried to reinstall everything, but it does not work.

like image 398
Marco Feregrino Avatar asked Oct 23 '15 01:10

Marco Feregrino


2 Answers

The problem is that your application when invoked through a browser functions through the user that is processing the Apache instance. If this is not the root (or another privileged user), then it may not have access. Also, this will likely dictate what directory the application attempts to execute from.

When you execute from the CLI, the user is whomever owns the instance of the terminal (unless su'd of course).

like image 150
Ohgodwhy Avatar answered Nov 07 '22 16:11

Ohgodwhy


Here's a minimal example of how to make this work:

First, create a new directory and cd to it. In that directory, create index.php with this content:

<?php
exec("gcc /var/www/html/test.c -o /tmp/a.out 2>&1",$compile_output,$compile_status);
var_dump($compile_output);
var_dump($compile_status);

exec("/tmp/a.out 2>&1",$run_output,$run_status);
var_dump($run_output);
var_dump($run_status);
?>

And create test.c with this content:

#include <stdio.h>
int main(void) {
        puts("Hello from C compiled by PHP!");
        return 0;
}

Then do docker run -p 8080:80 -v /whatever/directory/you/created:/var/www/html php:apache. Finally, go to http://localhost:8080 and the PHP script will compile and run that C program.

If this works in Docker but not in a "real" environment, then your environment is somehow set up incorrectly. In particular, check the PATH to make sure you're using the gcc that you think you are, and check the output of gcc -print-search-dirs and make sure that cc1 can indeed be found somewhere that it's looking.

If it works from the terminal but not from PHP, then put the debugging commands in the PHP script until you find the difference that's breaking it.

If you're missing cc1 entirely, then do sudo apt --reinstall install build-essential, or whatever the equivalent is to reinstall gcc and its dependencies on your distro.

like image 36
Joseph Sible-Reinstate Monica Avatar answered Nov 07 '22 15:11

Joseph Sible-Reinstate Monica