1

I am trying to compile a a c++ file on the server. I have a Windows 7 64 bit install along with mingw32 c/c++ compiler installed on the system running WampServer2.0 (php 5.3.0). I have already set the env variables to relative paths (c:\MinGW\bin). The following piece of code does not produce any executable in the Dest_folder, however the command runs perfectly well when executed from cmd (command prompt) and generates the executable

<?php
$string = system("g++ -o C:\\wamp\\www\\Dest_folder\\file.exe C:\\wamp\\www\\Dest_folder\\file.cpp", $retval);
echo $retval;
echo $string;
?> 

The $string variable should returns the last line of the command output on success, and FALSE on failure (which in this case does not return anything) and the $retval variable returns 1. Am I doing anything wrong? Also I noticed, every successful system() call (all other calls except for g++ compilation) produces 0 as $retval.

Seeder
  • 113
  • 7

2 Answers2

3

The backslash has special meaning in PHP string literals, so you need to escape it:

$string = system("g++ -o C:\\wamp\\www\\Dest_folder\\file.exe C:\\wamp\www\\Dest_folder\\file.cpp", $retval);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • @Seeder: Can `g++` be found by your shell? Specify the whole path. – Kerrek SB Mar 26 '12 at 00:16
  • I did set the path and environment variables for C:\MinGW\bin (using windows powershell). Nevertheless, I did try by specifying the entire path, still doesnt work. As I said it works fine when `g++ -o C:\wamp\www\Dest_folder\file.exe C:\wamp\www\Dest_folder\file.cpp` is executed from command prompt but not when I call it using system(). – Seeder Mar 26 '12 at 00:28
  • @Seeder: The command line's environment might not be available in your PHP session, though so it's best to double check. What if you say `C:\\MinGW\\bin\\g++` in your PHP? – Kerrek SB Mar 26 '12 at 01:11
  • Tried `C:\\MinGW\\bin\\g++.exe` too, but doesnt work. Another thing I noticed that compilation calls produce 1 in $retval (while all other system() calls are producing 0, and are executing well) – Seeder Mar 26 '12 at 01:40
  • @Seeder: Did you check the error message? Write everything to stdout by appending `2>&1` and look at the return string. – Kerrek SB Mar 26 '12 at 05:13
  • there wasn't any error message, and the system() returns the string that is last line of the output (there wasn't any return string too) – Seeder Mar 26 '12 at 14:09
  • 1
    Make a batch file that redirects the output of the g++ call to a file and call that instead... That way you know what's happening – rep_movsd Mar 29 '12 at 15:18
0

If fixing the backslash did not solve your problem, check in your php.ini file for the line: disable_functions . And if you see "system" written, remove it.

regards

grifos
  • 3,321
  • 1
  • 16
  • 14
  • The actual code has quite many system() calls, all of them work well (as of now), except for this one. So I guess this rules out the possibility of system() being disabled in php.ini – Seeder Mar 26 '12 at 00:14
  • Does your cpp includes .h files? – grifos Mar 26 '12 at 00:19
  • Do you mean, the standard #includes in the beginning? It uses iostream, fstream, vector.h etc. No additional .h files to be linked – Seeder Mar 26 '12 at 00:32
  • All other commands work fine with exec(), except for the g++ compilation one – Seeder Mar 26 '12 at 00:53
  • I found this link maybe that can help you http://stackoverflow.com/questions/8263316/compile-c-file-using-php The only difference seems to be the parameter order. – grifos Mar 26 '12 at 01:02
  • Again, all these commands works fine when executed from command prompt (with or without full g++ path, I already set the path/envt variables). But doesnt generate any exe when executed thru system(). The return value of `$retval` (in my question above) in each case (in the link you gave) is 1. Does it mean anything? – Seeder Mar 26 '12 at 01:29
  • Usually in the linux world, if a program returns 0 after execution that means the program executed successfully. If it returns 1 there was an error at some point, that cause abnormal or incomplete termination . So g++ seems to execute but fail at some point. – grifos Mar 26 '12 at 01:47
  • Can you force your php script to display the cpp file and to create an arbitrary file in C:\\wamp\www\\Dest_folder\\, maybe it is a permission problem but judging from your folder i don't think. If you open the command line shell (cmd.exe not powershell) and type g++, does the compiler respond? – grifos Mar 26 '12 at 01:52
  • display the cpp file, you mean in the browser window? Yes, I can create some arbit files (added `$handle1 = fopen("C:\\wamp\www\\Dest_folder\\text.txt", "a");` to the existing code and added some text to the file). My actual code is supposed to copy a cpp file from one location to `C:\\wamp\\www\\Dest_folder\\` then open it in +r mode, add some more lines to the copied cpp file and the compile it and the last compilation step is where it gets stuck, rest everything works fine. – Seeder Mar 26 '12 at 14:25