dongtie0929 2015-10-26 18:23
浏览 93

如何从php编译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

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'

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 :(

  • 写回答

1条回答 默认 最新

  • dongxi3859 2016-08-01 04:12
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100