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..