我正在尝试使用php编译并运行本地存储在计算机中的C文件
我使用了下面的代码

$dir = '/home/User/Desktop';
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0744);
 }

 file_put_contents ($dir.'/test.c', $code);

$path = "/home/User/Desktop/test.c";
$Command = "gcc $path 2&>1";
exec($Command,$return_val,$error);

我已经使用chmod和chgrp设置了文件的所有权限
但在执行时,它只是在我的屏幕上呼应“成功”,而不是我的$output value
这是我在
#include <stdio.h>
int main()
{
printf("Hello world");

return 0;

}

但是当我使用GCC执行了下面的程序时,它运行得很好
我现在使用的是Ubuntu 14.04.2lts
我试着用
$Command = "gcc $path 2&>1"; $output = exec($Command); $output = exec(./a.out);

但我还是得不到想要的结果
我试着用
$output = exec($Command,$return_val,$error);

现在当我回显$error时它给我“2”
我试着用系统
$last_line = system('gcc $path', $retval);
echo '
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

我得到以下输出
Last line of the output:
Return value: 4

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

但我允许文件使用
 sudo chmod g+w /home/User/Desktop/test.c

有人能帮我解决这个问题吗?

最佳答案

它不是2&>1,而是2>&1

sh: 1: cannot create 1: Permission denied
gcc: error: 2: No such file or directory

您在这里看到的第一个错误是shell抱怨无法创建名为“1”的文件来重定向stdout。
第二个错误是gcc抱怨缺少名为“2”的输入文件。

关于php - 从PHP执行C代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31079020/

10-13 06:59
查看更多