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

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.

Related

Setting up PHPunit - PHP CLI is not executing the shell script

I'm in a multi developer program. The other developer installed PHPUnit via composer and successfully is running tests. I took the composer files, ran composer update, but when I try to run the tests, I am receiving this error:
C:\Repos\Project\TDD>php vendor/bin/phpunit
Output:
dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../phpunit/phpunit" && pwd)
if [ -d /proc/cygdrive ]; then
case $(which php) in
$(readlink -n /proc/cygdrive)/*)
# We are in Cygwin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
;;
esac
fi
"${dir}/phpunit" "$#"
This is the content of the phpunit shell script. The shebang first line #!/usr/bin/env sh is not being output, but this is the rest of the file. The problem appears to be that the PHP CLI is not executing the script, just reading it. I can't figure out how to fix that.
I am on a windows environment, and I am not running apache locally.
Things I have tried:
I have tried using full paths (eg, C:/php/php.exe) for both the php and the shell script, and that didn't matter.
I have tried reversing the slashes vendor\bin\phpunit
I can run php -v successfully, but php -f vendor/bin/phpunit still just outputs the file contents.
I have confirmed the PATH for php is accurate and restarted.
All expected vendor files do appear to be present.
Composer update is not throwing errors of any kind.
I've changed my terminal / CLI processor to use powershell instead of the default cmd.exe (same result as above)
I've confirmed encoding of the phpunit file is UTF-8 without BOM
I've tried making the php command execute the phpunit script in powershell and git bash directly with no luck --- it outputs the shell file. It seems to be that php is just not executing it, which makes me think there is a problem with my php.ini or something.
I CAN run the phpunit shell directly, such as phpunit --version, but I can't get it to run my tests.
I set the composer platform to match my cli php -v
"config": {
"platform": {
"php": "7.4.20"
}
}
Edit: Interestingly, I uploaded it all to my LAMP webserver and ran the same script there, and it is not executing there either. It's outputting the files...
There is a lot of stuff you're fiddling with while trouble-shooting your issue, so there can be only giving extended comments on it. Take it with a grain of salt and pick the pieces that make sense to you, it looks to me you're clever and should be close to the point where you connect the dots more soon than later (don't give up):
C:\Repos\Project\TDD>php vendor/bin/phpunit
This on your windows shell prompt command the php binary to execute the (php) script vendor/bin/phpunit.
And PHP does exactly this (everything else would be a surprise, right?), it executes that script.
The output you see confirms it. As php is the PHP CLI SAPI (PHP Command Line Binary), the first line starting with # is not output (you can find this behaviour described in the PHP documentation as well: https://www.php.net/manual/en/features.commandline.usage.php and on antoher place that I can't find right now that exactly describes this difference to the common mode https://www.php.net/manual/en/language.basic-syntax.phpmode.php).
You then correctly analyze:
This is the content of the phpunit shell script.
and make the note on the shebang line:
#!/usr/bin/env sh
So this looks to me that this shell script vendor/bin/phpunit is not to be executed with the given commandline:
php vendor/bin/phpunit
but instead just by:
C:\Repos\Project\TDD>vendor/bin/phpunit
Which won't (or might not) work on your windows system.
Some more background information (this applies to phpunit bin-stubs as well as other when installed as composer(1) dependencies):
composer manages these scripts (for you)
composer handles them for POSIX compatible systems as well as for others (compare the /usr/bin/env sh which is stupid, it's /bin/sh, always, may need a report on the composer project) incl. windows or windows with cygwin or WSL running on Windows, Vagrant box setups etc. pp. (yes they really care since years).
composer handles this at the time of install/update.
for windows (which I can't compare are not using it but from what I remember) composer is adding .bat or .cmd files next to the commands (as that is how Windows handles executables).
So mabye using:
C:\Repos\Project\TDD>composer exec phpunit
already solves your invocation problem.
Or
C:\Repos\Project\TDD>vendor\bin\phpunit.bat
Or
C:\Repos\Project\TDD>vendor\bin\phpunit.cmd
Or
C:\Repos\Project\TDD>vendor\bin\phpunit
does. Point in case is this: https://getcomposer.org/doc/articles/vendor-binaries.md
All in all this looks to me you got instructions from one developer working on a unixoide sytem (Mac, Linux) and you're running on Windows. This should not pose a problem on that level, however the onboarding in your team might be low or it's just the knowledge management (unfortunately after a decade or more of Stackoverflow this got worse) and you've been left alone with the trouble shooting.
It's not a programming problem, but merely getting the tooling to run.
Perhaps there is some regime about composer and running it which prevents you from looking there first. But this is hard to say on Stackoverflow.
I'd start fresh with the project, remove it from disk and install it anew. This must work, always. To not sink the current project you can do this a-new in a second directory (the project might support composer create-project to give it a quick start - that is cloning new and doing the composer install).
Similar it is that you could do a composer update in the existing project (which is intended to modify it and depending on how well the project you work with is integrated with composer and your development platform will enable or break things).
At the end of the day the common workflow is:
> rmdir vendor
(remove the vendor directory, that is the blank slate)
> composer install
(install the vendor directory, that is all PHP dependencies of the project)
> composer exec phpunit
(execute the phpunit test-runner)
Edit: Interestingly, I uploaded it all to my LAMP webserver and ran the same script there, and it is not executing there either. It's outputting the files...
Never install phpunit on a webserver system. Run it in development or CI, but not on the webserver. Period. (this has security implications, and it won't help you to execute it there, you need to run it where you develop when you're doing TDD - keep this focus for troubleshooting)
The true path to the PHP script that is represented by vendor/bin/phpunit is:
vendor/sebastianbergmann/phpunit/phpunit
(you can find it in the composer.json of phpunit https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L66)
Given your original command-line and from the various other information you give, this should work:
php vendor/sebastianbergmann/phpunit/phpunit

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);
?>

Windows 8: .phar files, how do you want to open

I'm trying to run composer on windows with wamp. I installed composer using the cmd prompt, and now I'm trying to run "composer update" for an SDK. However, when I type in "composer.phar update," windows asks what app I want to use to run this program. I want the command prompt to deal with it! How do I just run it through cmd, without this "what app" window coming up?
You have to set php.exe as your default application for phar files.
.phar stands for PHP Archive
Usually .phars take some arguments, so they are intended to be run from command prompt. Linux/BSD/OS X shell or Windows command prompt.
Linux .phar use case scenarios assume .phars are copied to some /bin and renamed to be without .phar extension, so you can use a php archive as if you would use any other linux command. So I recommend following way of doing the same thing with Windows:
Put all your .phar files to one directory like C:\php\phars
Add C:\php\phars to system environment variables (right-click my Computer -> Properties -> Advanced System Settings -> Environment variables)
Start the elevated command prompt (find command prompt in start menu then right-click and select Run as Administrator)
Type the following commands, replacing the path C:\phpdev\php\php542\php.exe with full path to your PHP executable:
ftype PHARFile=C:\phpdev\php\php542\php.exe "%1" %*
assoc .phar=PHARFile
set PATHEXT=%PATHEXT%;.PHAR
Next time your should be able just to run Windows console (keyboard Win+R and type cmd.exe) and type any of your .phar's like apigen.phar followed by any command and it will work
C:\Users\acosonic>apigen.phar help
Usage:
...
Arguments:
command The command to execute
command_name The command name (default: "help")
Options:
--xml To output help as XML
--format To output help in other formats (default: "txt")
--raw To output raw command help
--help (-h) Display this help message.
--quiet (-q) Do not output any message.
--version (-V) Display this application version.
Help:
The help command displays help for a given command:
php C:\phpdev\phars\apigen.phar help list
You can also output the help in other formats by using the --format option:
php C:\phpdev\phars\apigen.phar help --format=xml list
To display the list of available commands, please use the list command.
C:\Users\acosonic>
So this way lets you run .phar archives in a directory where you need to work, for example generating documentation in C:\myproject\controller without specifying full path to .phar as if you would if it's run without adding it to Windows path.
To explain what commands in step 4 did:
Created mapping HKCR.phar → HKCR\PHARFile
Created HKCR\PHARFile\shell\open\command = 'php.exe "%1" %*' [REG_EXPAND_SZ]
Extended HKCU\Environment\PATHEXT = '%PATHEXT%;.PHAR' [REG_EXPAND_SZ]
*.phar gets treated like binary/script, and *.phar execution works as long as a *.phar file is located anywhere in %PATH%.
One can wrap php *.phar with *.bat, then the filename will be the name of the CLI command:
#ECHO OFF
php "C:/Program Files/PHAR/phpDocumentor.phar" %*
One can also use such a wrap to pass along default arguments; eg. wp.bat:
#ECHO OFF
php "C:/Program Files/PHAR/wp-cli.phar" --path="D:/SDK/wordpress" %*
Where pattern %* will capture and forward CLI arguments, as it is supposed to.
Alike this one can run phar alike any other CLI command, in a terminal window.
Keep it simple. Instead of changing .phar's default program, which is not always easy in Windows, try just typing "php" in front of your command.
php composer.phar update

travis-ci script

I'm trying to setup phing to work with travis-ci, but I can't get it to run a setup script to get all the dependencies installed.
My .travis.yml file is:
language: php
php:
- 5.2
script: ./.travis-phing.sh
In travis, I get the error:
/home/travis/build.sh: line 105: ./.travis-phing.sh: Permission denied
What is causing that?
Solved
The script to be set to execute. I used:
chmod a+x .travis-phing.sh
Then simply commit, and push back to github.
Run the script using bash
Another option would be to run the script using bash, this would omit the need to modify the files' permissions.
bash path/to/file.sh
Alternatively:
sh path/to/file.sh
Note that
In this case you're not executing the script itself, you're executing bash or sh which then runs the script. Therefore the script does not need to be executable.
Make sense?
I've found this solution incredibly useful myself. I'm mainly running node & npm projects on travis-ci, those builds make use of the npm test command which you can configure to be anything.
I'm order to modify file permission I need to use sudo chmod ... on my local machine. But you can't always use sudo on travis-ci.
sh file.sh allows me to run my tests both locally and on travis-ci without having to manually update permissions.

yiic - can't run - env: php\r: No such file or directory

On MAC OS X Lion - Running MAMP
I'm trying to execute the following yiic command (from Yii framework);
./yiic message ./app/messages/config.php
I'm getting this message:
env: php\r: No such file or directory
I've looked into yiic file and I see:
#!/usr/bin/env php
<?php
require_once(dirname(__FILE__).'/yiic.php');
I've looked into /usr/bin/env
and I see, nothing related to php (I believe):
Should I have there something related with php ?
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/qq/4k5m37mn16bgfpp6yt8ggljc0000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-L43tVY/Render
TERM_PROGRAM_VERSION=299
TERM_SESSION_ID=1999B4D4-939B-4065-B71C-D9B0563A9EC6
USER=mem
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/tmp/launch-FBmmga/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin
PWD=/usr/bin
SHLVL=1
HOME=/Users/mem
LOGNAME=mem
LC_CTYPE=UTF-8
DISPLAY=/tmp/launch-oXxXFl/org.x:0
_=/usr/bin/env
OLDPWD=/usr
As anyone add this error before?
Is this something to do with the fact that php is not on that env think perhaps?
Please advice
It looks you're just experiencing an encoding issue with your yiic file. The shell is trying to use php\r interpreter to open yiic, instead of php.
Run the following command to replace all CARRIAGE_RETURN characters by LINE_FEED :
perl -pi -e 's/\r/\n/g' yiic.php
Worked pretty well in my case.
I just read this blog: could be the very same problem you are experiencing?
The next step is to tell the yiic application, found in the framework folder, to create a new site. The syntax is
yiic webapp path/to/directory
But before you even begin to use this command, let me explain it a bit, as it’s very important and can be complicated. The yiic file is an executable that runs using the computer’s command-line PHP and that really just invokes the yiic.php script. You may be able to call it using just yiic or using ./yiic (i.e., run the yiic command found in the current directory). Or you can more explicitly call either script using php yiic or php yiic.php. Or you may need to indicate the PHP executable to be used: C:\php\php.exe yiic. You should try the variations on this command, as applicable to your computer, just to make sure you can invoke yiic, prior to trying to create the Web application.
In my case that worked for using xampp:
Creating new project:
X:\xampp\php>php.exe X:/xampp/htdocs/yii/framework/yiic webapp X:/xampp/htdocs/YOUR_PROJECT
yiic shell:
X:\xampp\php>php.exe X:/xampp/htdocs/YOUR_PROJECT/protected/yiic shell X:/xampp/htdocs/YOUR_PROJECT/index.php
Just I changed php version of my system form 4 to 7.
I run this command to salve this issue-
source ~/.bash_profile
This solved my issue.

Categories