4

I am using PHP on Windows machin. I also use Dev C++. I can perfectly compile .cpp file on CMD using this command:

g++ hello.cpp -O3 -o hello.exe

Now what I am trying to do is running the same command using php system() function, so it looks like this:

system("g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe");

but it doesn't compile. I am lost, please tell me what am I missing?

I also looked up at this question and thats exactly what I need, but I couldnt find a usefull solution for my case there:

Php script to compile c++ file and run the executable file with input file

2
  • Are you sure that g++ is installed, and visible in the PATH, on your Windows server? Commented Nov 24, 2011 at 23:13
  • Also, are you sure that the account running PHP has modify permissions for the c:\wamp\www\grader directory? Commented Nov 24, 2011 at 23:21

3 Answers 3

7

Use the PHP exec command.

echo exec('g++ hello.cpp -O3 -o hello.exe');

should work.

There's a whole family of different exec & system commands in PHP, see here:

http://www.php.net/manual/en/ref.exec.php

If you want the output into a variable, then use :

$variable = exec('g++ hello.cpp -O3 -o hello.exe');

If that doesn't work, then make sure that g++ is available in your path, and that your logged in with sufficient enough privliges to allow it to execute.

You may find also that it's failing beacuse PHP is essentially being executed by your web server (Unless your also running PHP from the cmd prompt) , and the web server user ID may not have write access to the folder where G++ is trying to create the output file.

Temporarily granting write access to 'Everybody' on the output folder will verify if that is the case.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for reply, I will try to work out your approach. Since I am new to windows commandline (I come from linux background) I am not comfortable with windows conventions. I used to run shell_exec() on linux and it worked fine.
The PHP manual page, will help you immensly, as it will explain all the differences. I'd be pretty sure however, that your root issue is going to be permissions related. Let me know how you get on. :-)
It wasn't permission actually, the problem was with full path to g++.exe. Since I had g++ in my PATH, i assumed I don't need to write the full path. Apparently I need to :)
Yup, I noticed the update above after I answered. :-) Paths work a little different to how the work in *nix, It's caught me out many times too. I would still be mindfull of permissions for future refrence though, esp if your running PHP under the windows IIS web server.
4

Two things:

  1. You are using double quotes and are not escaping the \ inside the path.
  2. You are not using a full path to g++.

The first one is important as \ followed by something has a special meaning in such a string (you might know \n as new line), the second one is relevant since the PHP environment might have a different search path.

A solution might be

system("c:\\path\\to\\g++ c:\\wamp\\www\\grader\\hello.cpp -O3 -o C:\\wamp\\www\\grader\\hello.exe");

Alternatively you can use single quotes, intead of double quotes, they use diffeent,less strict escaping rules

system('c:\path\to\g++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\grader\hello.exe');

or use / instead of \, which is supported by windows, too.

system("c:/path/to/g++ c:/wamp/www/grader/hello.cpp -O3 -o C:/wamp/www/grader/hello.exe");

What you do is your choice, while many might consider the first one as ugly, and the last one as bad style on Windows ;-)

1 Comment

Thank you, I used the first approach and it worked! Since I wrote PATH rules for g++, i thought i wouldn't need a full path to g++.
0

Thanks to everyone. I tried to run the codes given in above posts and it worked like a charm.

I ran the following code using my browser

$var =  exec("g++ C:/wamp/www/cpp/hello.cpp -O3 -o C:/wamp/www/cpp/hello.exe");
echo $var;

The exe file is created. I am able to see the result when i run the exe file but the problem is when i run the above code in the browser, the result is not displayed on the webpage. I gave full access permission to all users but still give does not show the result on the webpage.

I really need help on this since i am doing a project on simulated annealing where i want to get the result from compiled c++ program and display it in the webpage with some jquery highcharts.

Thanks again to all, it has helped me alot and i have learnt alot as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.