简体   繁体   中英

Compile C++ file Using PHP

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

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.

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 ;-)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM