Skip to content
September 26, 2007 / ranacse05

Compile C program using PHP-5


To day i successfully compile a .c program using PHP-5. It just simple.I just use “System()” function on PHP-5. 🙂

Here is an example

<?php
$data = '#include<stdio.h> 

int main(){
printf("Hello World");
return 0;
}

';

$my_file = 'code.cpp';
file_put_contents($my_file, $data);

system("gcc {$my_file} &> error.txt");

$error = file_get_contents("error.txt");

if($error=='')
    system("./a.out");
else
    echo $error;
?>
Advertisements

43 Comments

Leave a Comment
  1. Sona / Mar 22 2008 8:58 am

    how did u compiled…. plz tell me how??
    i used system(“cc sam.c”); but itz not working …

  2. ranacse05 / Mar 26 2008 5:27 am

    i compile it using system(“gcc main.c”); remember u have to make the directory permission ‘0777’ .

  3. php programmer / May 15 2008 12:33 pm

    shell_exec() function can also be called with command line arguments.
    should give the same result as system()?

  4. ranacse05 / May 16 2008 9:09 am

    @php programmer
    ya u r right 🙂

  5. ashish / May 26 2008 11:39 am

    i want to run a c program via php. the program also has scanf commands. how do i give these inputs. i canntouse command line arguments as the inputs are to be given from dynamically collected data over the time.

    • ranacse05 / Oct 25 2009 8:10 pm

      how about u read the inputs form a file ?

      i think that can help u .

  6. Rahid Hasan Robin / Oct 22 2009 8:30 am

    I want to see all compiling error in php. How could it possible pls help me.

    Thanks for this helpful post

    • ranacse05 / Oct 25 2009 8:08 pm

      u need to call an shell script for that in that shell script put the compile command and save the output of those command on another file then read the file and do what ever u want .

      there maybe some easy way too.

  7. Miljan / Apr 24 2010 3:11 pm

    i am making online compiler for college… i need to now when i assign text filed values to variable what i should put in system(gcc and then?) ? pls help its urgent

    • ranacse05 / May 17 2010 7:50 pm

      u have to filter the text before u pass that to the system, bcoz u need make sure that there is not harmful command , or u can lost ur server data 😀

  8. sharmilli / Feb 23 2011 10:04 am

    I tried shell_exec(‘gcc -0 Compile Compile’); , system(‘gcc -0 Compile Compile’);, exec(‘gcc -0 Compile Compile’); to run my C code but its not working.

    • ranacse05 / Feb 23 2011 5:48 pm

      You need to allow those functions on php.ini files , usually those are not allowed in shared hosting .

      • sharmilli / Feb 24 2011 4:22 am

        I could use the command for running ‘./compile’ but.

  9. Tinesh / Aug 8 2011 7:31 pm

    i’m trying to design an online compiler.. , i’m not able to run this command system(‘gcc hello.c) from my php file… ,but system(‘./a.out’) works

  10. rock / Nov 24 2011 12:48 am

    wud u plz send me the code i m in trble…
    my emailid:rock389@yahoo.com

    • ranacse05 / Nov 24 2011 12:57 am

      Hey Rock please check the phpinfo() for the exec, system etc functions most of the hosting dont allow to call those functions , you can try in your VPS or local server .

  11. Subbaiyan / May 8 2012 2:13 pm

    c & C++ Program code input textarea in PHP and out put in div any one do this work replay to me and send the php code

  12. Dirk / Aug 21 2012 12:56 am

    I absolutely love your website.. Pleasant colors & theme. Did you make this site yourself?

    Please reply back as I’m planning to create my own site and would like to know where you got this from or exactly what the theme is called. Kudos!

  13. Rohit Gupta / Sep 12 2012 6:02 pm

    can anyone mail me those php codes
    rohit.1290@gmail.com

    thanks in advance…

  14. Praveer Grover / Oct 4 2012 10:57 pm

    Is it for LINUX systems or can be used for windows as well?

  15. mridula / Nov 18 2012 11:18 pm

    I need to compile code of C using php, but how to make it intrective as it need all scanf value at one time, not while needed…
    I need to resolve it as soon as possible, please help

  16. nirmala / Nov 30 2012 11:50 am

    I have written c c++ code in the text area in php5 and now i have to compile these code on localhost?
    What i should do ,anyone help me?

  17. var / Feb 22 2013 2:01 pm

    can someone plzz mail me the code for compiling c/c++ program via php on windows 7 platform.
    i m using xampp on my local machine.
    i hv no idea abt hw to do dat…plzzzz help me..
    .its very urgent…

  18. monisha / Aug 9 2013 2:35 pm

    hi, i have to do my php project i.e online c compiler.. can you help me how to convert text file into c file???

  19. neeraj / Aug 19 2013 3:05 am

    sir how can i run c program though php on windows plzz give sample example

  20. E.Rameshkumar / Aug 27 2013 11:19 am

    hai sir, i want to build an online c/c++ compiler through using php. as i complete my design work. in my design page i have one richtext box and one command button and one label box. i go to put my c coding from richtext box and click the button. after click the button my code was compile and exc as background process. then my result will be display to my label box. this is my situation. how will create a coding for this. i request you to plz sent appropriate code for the following mail. (rameshkumarbhavani@gmail.com,ph.no:8883308700). plz help me sir.

  21. Vidhya / Sep 6 2013 12:56 pm

    How to compile c code in php and get the output???Could u plz post the code ..

  22. Rosiya / Sep 6 2013 12:57 pm

    How to compile c code in php………????? plz anyone post the code…plzzzzzzzzzzzzzz…

  23. Vidhya / Sep 6 2013 1:00 pm

    How to compile and execute a c code in php program??Plz post the code…

  24. Sri Harsha Chilakapati / Jul 22 2014 10:44 pm

    Nice post, but is it really safe? Users can type malicious code right?

    • ranacse05 / Jul 22 2014 10:46 pm

      Before you run the code just check for system() and comment that line and you’ll be fine .

    • Praveer / Jul 23 2014 10:35 am

      No its not anybody can drop a fork bomb anytime. To make it safe you have to 1st create a sandbox and run the programs in that sandboxed environment.

  25. Pankaj / Aug 6 2014 5:03 pm

    i run this code on mu local xampp server but give blank …….how do i this pls comment

    • ranacse05 / Aug 6 2014 6:43 pm

      Check if system() is turn off or not in your system .

      • Pankaj / Aug 7 2014 10:55 am

        thanks for fast reply .how to check system() is turn off or not in your system.i am compile my c code on gcc through command prompt. and if you have tutorial regarding this pls mail my id pankaj.tak@outlook.com Thanks

  26. causing havoc / Sep 3 2014 2:56 am

    It’s an awesome paragraph designed for all the internet viewers; they will obtain benefit
    from it I am sure.

  27. Jekil / Mar 30 2015 11:32 pm

    Hey i want to make C++ compiler online means i want to embed terminal into website because i want to make Graphics program to run online is it possible ? if yes then how to do it ? please tell me my email id is jekilhansora901@gmail.com Thank You

  28. mahendra / Apr 3 2015 11:38 am

    sir when i run this code ,didnt work, it generate error

    • ranacse05 / Apr 3 2015 4:02 pm

      make sure you have the permission to run the function .

  29. vicky / Apr 6 2015 6:56 pm

    sir would you please tell how to check these permissions . i am desining a online compiler n finding it very hard to design

  30. vicky / Apr 6 2015 7:04 pm

    and are these are woking in windows based os or linux

  31. venkat / Sep 23 2016 12:34 pm

    How I can execute above code on turbo c compiler with windows7 specification.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: