2

For my graduation I need to build an online IDE. I have thinked to use a web-based IDE (Codiad), but I can't compile C code. I'm using WAMP server.

This is the question: is there any method to have and use GCC compiler on WAMP Server? Or I need to build an other server (for example Ubuntu Server) and use shell command for compiling C code through PHP functions?

Thank you in advance and sorry for my English!

db92
  • 183
  • 3
  • 16

1 Answers1

0

You can do that using PHP shell_exec() or backticks (``)

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>";
?>

I test it :)

enter image description here

Further reading DOC

N.B : do that gcc shold be installed on your server and you should have permissions

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

Alupotha
  • 9,710
  • 4
  • 47
  • 48
  • I have installed WAMP on my PC, how can I install GCC on the wamp directory? – db92 Apr 20 '15 at 13:15
  • I forgot your in windows machine ! now I am in linux I couldn't check further but this will be the solution http://sourceforge.net/projects/mingw/files/ – Alupotha Apr 20 '15 at 13:41
  • 1
    i think you need to add 'echo `./outputfile`;' to execute. – Suchit kumar Apr 20 '15 at 13:42
  • Yeah, I have copied the MinGW files in the Wamp directory, but I don't know how to put them. Tomorrow I'm going to set up and Ubuntu Server on virtual machine for resolving the problem – db92 Apr 20 '15 at 14:01
  • I updated the answer and posted complete code ! if it is match with your problem, don't forget to mark as answer ! good luck – Alupotha Apr 20 '15 at 14:07
  • However, it could report the syntax error of C code. – wshcdr Nov 03 '15 at 09:36