编译C ++文件使用PHP

编程入门 行业动态 更新时间:2024-10-26 10:36:31
本文介绍了编译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:

  • 您正在使用双引号,而不是在路径中转义\。
  • 使用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

    本文发布于:2023-11-10 06:37:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1574630.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件   PHP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!