我在Windows机器上使用PHP。我也使用Dev C ++。我可以使用这个命令在CMD上完美编译.cpp文件:
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现在我想做的是使用php system()函数运行相同的命令,这是:
Now what I am trying to do is running the same command using php system() function, so it looks like this:
系统(g ++ c:\wamp\www\grader\hello.cpp -O3 -o C:\wamp\www\\ \\grader\hello.exe);
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脚本编译c ++文件并使用输入文件运行可执行文件
推荐答案p>
Two things:
第一个很重要,因为\后面有一个特殊的含义在这样的字符串您可能知道\\\为新行),第二个是相关的,因为PHP环境可能有不同的搜索路径。
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.
解决方案可能是
system("c:\\path\\to\\g++ c:\\wamp\\www\\grader\\hello.cpp -O3 -o C:\\wamp\\www\\grader\\hello.exe");或者,您可以使用单引号,双引号,它们使用diffeent, / p>
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");你做的是你的选择,而许多人可能认为第一个丑陋,最后一个在Windows上作为不良风格; - )
What you do is your choice, while many might consider the first one as ugly, and the last one as bad style on Windows ;-)
更多推荐
编译C ++文件使用PHP
发布评论