php - 在 Wamp 服务器上编译 C 代码

标签 php c web-applications

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

问题是:有什么方法可以在 WAMP 服务器上安装和使用 GCC 编译器吗?或者我需要构建一个其他服务器(例如 Ubuntu Server)并使用 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>";
?>

我测试了一下:)

enter image description here

进一步阅读 DOC

注意:gcc 是否应该安装在您的服务器上并且您应该有权限

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

关于php - 在 Wamp 服务器上编译 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29748598/

相关文章:

有人可以告诉我给定代码中第二个 printf 语句中的引用流吗?

java - 使用 Spring 和 Webapp 属性占位符查找属性

php - 从帖子标题集合中生成热门主题

php - HTML POST 和获取数组

php - Codeigniter 事件记录从 mysql 获取记录,其中 ID 等于数组中的记录

c - 编译 .c 代码时未声明 socklen_t

php - PHP 项目的 Textmate 自动完成和类大纲

c - 使用 htons 确定字节顺序

php - 一个用户名适用于所有数据库还是多个 MySQL 帐户?

docker - Dockerized Webapp-热重装