0

I want to build an online compiler with PHP, but when I want to compile the C file, I get an error.

When I use the terminal to compile the C file it was normal, but through the PHP to do that will I get the error message:

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

My OS:

CentOS Linux release 7.6.1810 (Core)

gcc --version:
        gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
        Copyright (C) 2015 Free Software Foundation, Inc.
        This is free software; see the source for copying conditions.  There is 
        NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
        PURPOSE.

which gcc :
        /usr/bin/gcc

I have been run

sudo yum install gcc-c++
sudo yum  install cpp-4.8.5-36.el7_6.2.x86_64

The cc1 file can be found under the /usr/libexec/gcc/x86_64-redhat-linux/4.8.5. I also set the PATH to ensure that /usr/bin was at the beginning, by

PATH=/usr/bin:$PATH

I simply take the code that someone says will work, but that still failed for me. Below is the code I found.

<?php
$data = '#include<stdio.h> 

int main(){
printf("Hello World");
return 0;
}

';

$my_file = 'code.c';
file_put_contents($my_file, $data);

system("gcc {$my_file} &> error.txt");

$error = file_get_contents("error.txt");

if($error=='')
    system("./a.out");
else
    echo $error;
?>

I have searched for the solution for a long time, but I still can't get a proper answer. I try to reinstall gcc and g++ many times, but the error remains..

starlitsky
  • 41
  • 2
  • How far does the code get? What is the error? For example, is the `code.c` file created successfully? – ficuscr Jun 07 '19 at 20:30
  • Meh, take a step back. Remove PHP from the mix. Can you get `gcc` working with the most basic build and using the command line? As it stands, sounds like an environment problem: [Gcc error: gcc: error trying to exec 'cc1': execvp: No such file or directory](https://stackoverflow.com/questions/11912878/gcc-error-gcc-error-trying-to-exec-cc1-execvp-no-such-file-or-directory) – ficuscr Jun 07 '19 at 20:33
  • The `gcc` working with the command line is normal, and file`code.c` also create successfully. – starlitsky Jun 08 '19 at 06:23
  • Finally, I use `clang` to replace the `gcc` then it works normally, but I still hope to know the problem that occurred. – starlitsky Jun 08 '19 at 06:26

0 Answers0