How to compile with gcc in php? - php

I'm trying to compile *.c file using shell_exec(gcc -o code.c code);
when I die dump I get that gcc is not recognized as a command but I do have it installed on my command. I can do shell_exec('ls') I also have the gcc path set I cain't think of anything else. can you? thanks ahead !

Please bear in mind at the moment I'm getting errors but gcc is actually compiling from a php function call in the web browser.
2>&1 means redirect stderr to stdout this link helped hope it can help you http://wiki.bash-hackers.org/howto/redirection_tutorial.
function compile()
{
$output = shell_exec("C:\MinGW\bin\gcc.exe -o $code 2>&1");
dd($output);
}
gcc.exe: fatal error: no input files\n
compilation terminated.\n
I can now use gcc with php shell_exec() function the errors I'm getting are now a simple understanding of the absolute paths my files have and making the correct gcc command to compile, the next step once I have actually managed to compile the code.c in .a.out will be to execute the .a.out file. I will share updates here once it is accomplished. Thanks for your help.

Related

Execution failing while running from browser

I am building online compiler for C and C++ using php, so we are using gcc compiler to execute using "shell_exec" function ,
test.php
<?php
$output = shell_exec("gcc /var/www/test/main.c 2>&1");
print_R($output);
?>
If I execute in terminal like php test.php, is working fine and it is creating a.out compiled file.
But If I try to run in the browser like localhost/test.php, its giving below error
gcc: error trying to exec 'cc1': execvp: No such file or directory
I gave full permission (0777) to test.php,
$ whereis cc1 // cc1:
Please find below version I am using
gcc version 5.4.0
PHP 7.0.32
OS: ubuntu 16.04
server : Nginx
Is there any alternate to run C and C++ using PHP or how to resolve this issue whille running from the browser.
Reading the error message, the problem comes from two issues:
php cannot find gcc since $path variable is not correctly set, in php file, you should type something like PATH=/usr/bin && gcc /var/www/test/main.c. In this example, gcc and other stuff are in /usr/bin directory. To know where are located cc1 for instance, type find / -name cc1 and add it to $PATH: PATH=/some/path:/some/other/path
Current directory of php could no be writtable, so indicate where the generated file must be created: gcc /var/www/test/main.c -o /var/www/test/main
Directory /var/www/test must be writable by user running php program.
Note that the user running the php program is not necessary the root...
So your php file should looks like:
<?php
$output = shell_exec("PATH=/usr/bin && gcc /var/www/test/main.c -o/var/www/test/main 2>&1");
print_R($output);
?>

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

PHP exec function on nginx doesn't recognize system environment variables

I try to use exec function in PHP to compile a source file with gcc with the following code.
<?php
exec("gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
I got the following output when calling via web browser (I use nginx as a web server).
gcc: error trying to exec 'cc1': execvp: No such file or directory
However, if I run gcc -o hello hello.c on the command shell directly or call with php my_file.php on the shell directly, both ways compile successfully.
If I append the absolute path to gcc in my PHP code like this:
<?php
exec("/usr/bin/gcc -o hello hello.c 2>&1", $output, $return_value);
echo $output[0];
I got the following output.
collect2: fatal error: cannot find 'ld'
So, I think the problem is my webserver (nginx) doesn't know the system path environment variable to find /usr/bin which gcc and other gcc-dependencies resides in.
How can I let PHP exec function recognize system environment variables on nginx?
OS: Ubuntu 14.04
nginx: 1.6.2
PHP 5.5.9
I'm stuck on the same problem (exactly the same...) using nginx 1.10 and PHP 5.6 on Arch Linux.
The same PHP code was working on Apache/Debian.
When trying the Arch/Nginx server, I had the cc1 error... I replaced gcc by /usr/bin/gcc.... and I am now stuck on "collect2: fatal error: cannot find 'ld'"
The same compilation works with a shell... and ld is in /usr/bin. It just does not work when using "exec" in PHP.
Not satisfying (but working...) solution
By running gcc -v .....(just add -v to your compilation line) I could see :
...
COLLECT_GCC_OPTIONS='-v' '-D' 'exit=noexit' '-D' '_exit=noexit' ...
/usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/collect2 -plugin ... (<= very long line)
collect2: fatal error: cannot find 'ld'
Then I did :
cd /usr/lib/gcc/x86_64-pc-linux-gnu/6.1.1/ # <- path to collect2 (see above)
ln -s /usr/bin/ld ld
Now ld prog is available in the same dir as collect2.
And it works.
I am now looking for a better solution... :)
Set the PATH variable for PHP, as it may rely on its own environment variables and ignore the system's PATH variable.
For example, I have the following line at the bottom of my .env file:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
With this, executing gcc (without giving the full path /usr/bin/gcc) works correctly.
Duplicate of How compile GCC from php. Thought I'd post the answer here as well, since this question received a lot more attention than the other one.

Run C++ code with PHP

I have a some C++ code I would like to have run on a server and return the output. I know that
exec("./myprogram.exe", $out);
should run the program and $out will hold the output. Currently I can't get output from my program... it outputs it using cout and I compiled it on Windows, the server I want to run it on is Linux based. Any pointers?
First, you need to recompile your C++ code on Linux, using first g++ -Wall -g (order of arguments to g++ matters a lot) -then some other compiler arguments- since -Wall asks for all warnings and -g for debugging information. Once your code is debugged on Linux you could also pass -O2 to ask GCC to optimize.
Then you need to use the popen function of PHP to get the output of your command (thru a pipe). As documented, use e.g. fgets to read from the pipe handle, and don't forget to pclose it. See also this answer.

Php script to compile c++ file and run the executable file with input file

I need a php script to compile C/C++ file.
After compiling and getting the executable of that file i need to run that file with a argument of input.txt file and then compare the result with output.txt file
How can i do this ?
Call a command-line C++ compiler using system, exec, or backticks. There is no C++ compiler written in PHP.
Is PHP required? If not consider GNU Make. It was especially developed to solve such problems as your. With this simple Makefile:
OUT = app
CFLAGS = -O2 -Wall
$(OUT): main.o
clean:
rm -rf $(OUT) main.o
You get ability to easy compile your program with different compiler flags and performing clean operations:
make # for compiling
make clean # for remove binaries
Make has a good manual. Also, there is similar tool from Microsoft called nmake.

Categories