编译C ++文件使用PHP [英] Compile C++ file Using PHP

查看:107
本文介绍了编译C ++文件使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:


  1. 您正在使用双引号,而不是在路径中转义\。

  2. 使用g ++的完整路径。

第一个很重要,因为\后面有一个特殊的含义在这样的字符串您可能知道\\\
为新行),第二个是相关的,因为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆