如何从PHP编译GCC [英] How compile GCC from php

查看:274
本文介绍了如何从PHP编译GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个函数从PHP:

  exec(gcc testC.c -o runner.out 2>& amp 1\" ,$输出,$地位); 

但是当我打印$输出时,我得到这个错误:(:

  gcc:尝试exec'cc1'时出错:execvp:没有这样的文件或目录\ $ 

如果我运行

  exec(/ usr / bin / gcc testing / pruebaC.c -o testing / frommm.out 2>& 1,$ output,$ status); 

我得到那个

  collect2:致命错误:找不到'ld'\\\

我的用户:

  group vagrant 
vagrant:vagrant root adm cdrom sudo dip www-data plugdev lpadmin sambashare

我的gcc目录

  / usr / bin $ ls -l gcc 
lrwxrwxrwx 1 root vagrant 7 Apr 7 2014 gcc - > gcc-4.8

如果我从终端运行所有工作,我可以编译并执行, PHP我不能:(


解决方案

我不确定发生了什么评论,但我已经找出了问题的根源,我正在回答这个问题只是为了澄清任何未来的读者。

@zneak是正确的。如果你尝试 echo $ _ENV [PATH] 并且没有收到任何回应,这意味着PHP正在执行 gcc 和任何其他可执行文件,而无需在 PATH 变量中查找它们。因此,它有时找不到其中的任何一个。 @Marco解释为在bash中回显 PATH 变量,并执行 echo $ PATH 。但是,这两个变量是分开的,并且在bash PATH 时,PHP PATH 通常是未设置的。



为了解决这个问题,您必须在其环境变量中为PHP定义 PATH 。如果您不确定要放置什么,请参阅bash中定义的 PATH 变量。例如,我目前的Laravel项目包含以下行:



PATH = / usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin



位于 .env 文件,现在只执行 gcc (不提供完整路径 / usr / bin / gcc )正常工作。


I tried this function from php with :

exec("gcc testC.c -o runner.out 2>&1",$output,$status);

but when I print $output I get this error :(:

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

If I run

exec("/usr/bin/gcc testing/pruebaC.c -o testing/frommm.out 2>&1",$output,$status);

I get that

collect2: fatal error: cannot find 'ld'\n

My user :

groups vagrant
vagrant : vagrant root adm cdrom sudo dip www-data plugdev lpadmin sambashare

My gcc directory

/usr/bin$ ls -l gcc
lrwxrwxrwx 1 root vagrant 7 Apr  7  2014 gcc -> gcc-4.8

If I run from terminal all work, I can compile and execute but from PHP I cant :(

解决方案

I'm not sure what happened in the comments, but I've figured out the root of the problem and I'm answering this just to clarify for any future readers.

@zneak is right. If you try and echo $_ENV["PATH"] and receive nothing in response, this means that PHP is executing gcc and any other executables without looking for them in a PATH variable. As a result, it sometimes can't find any of them. @Marco interpreted as echoing the PATH variable in bash, and executed echo $PATH instead. However, these two variables are separate, and often the PHP PATH is unset while the bash PATH is.

In order to fix this issue, you'll have to define PATH for PHP in its environment variables. If you're unsure what to put, refer to the PATH variable defined in bash. For example, my current Laravel project has the line

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

at the bottom of it's .env file, and now executing just gcc (without giving the full path /usr/bin/gcc) works correctly.

这篇关于如何从PHP编译GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆