在 Wamp 服务器上编译 C 代码

Compile C code on Wamp Server

为了毕业,我需要建立一个在线 IDE。我曾想过使用基于网络的 IDE (Codiad),但我无法编译 C 代码。我正在使用 WAMP 服务器。

问题来了:有什么方法可以在WAMP Server上安装和使用GCC编译器吗?或者我需要建立一个其他服务器(例如 Ubuntu 服务器)并使用 shell 命令通过 PHP 函数编译 C 代码?

提前谢谢你,对不起我的英语!

您可以使用 PHP shell_exec() 或反引号 (``)

PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec().

<?php
   $myfile = fopen("newfile.c", "w") or die("Unable to open file!");
    $txt = "#include <stdio.h>

    int main()
    {
      printf(\"Hello PHP-C world\");
      return 0;
    }";
    fwrite($myfile, $txt);
    fclose($myfile);
    $tmp = `gcc -o outputfile newfile.c`;
    echo "<pre>$tmp</pre>";
    $output = `./outputfile`;
    echo "<pre>$output</pre>";
?>

我测试了一下:)

进一步阅读DOC

N.B : gcc 应该安装在你的服务器上并且你应该有权限

The backtick operator is disabled when safe mode is enabled or shell_exec() is disabled.