如何在Windows平台上用PHP编译C / C ++代码?

时间:2022-09-10 09:38:51

Can anyone help me with the compilation of C++ code in PHP on the Windows platform? I am using Microsoft Visual C++ 6.0.

谁能帮助我在Windows平台上用PHP编译C ++代码?我使用的是Microsoft Visual C ++ 6.0。

I have tried the following options which I knew, but none of them work:

我已经尝试了以下我知道的选项,但它们都不起作用:

system('test.c') 
exec('test.c')

The file 'test.c' has been placed in my php/www/ folder.

文件'test.c'已放在我的php / www /文件夹中。

I need to first compile the code, which makes test.exe and then using exec( ) I need to run the exe file.

我需要先编译代码,这使得test.exe然后使用exec()我需要运行exe文件。


Edit:

In this code "test.c", I am reading from a file "data.txt". The contents of data.txt is changed every 5 seconds. That's why first I need to compile in PHP and then run the new exe file.

在这段代码“test.c”中,我正在读取文件“data.txt”。 data.txt的内容每5秒更改一次。这就是为什么我首先需要在PHP中编译然后运行新的exe文件。

4 个解决方案

#1


Do a .bat file that:

做一个.bat文件:

  • runs the vcvars32.bat file that comes with Visual Studio
  • 运行Visual Studio附带的vcvars32.bat文件

  • runs "cl.exe yourprogram.c"
  • 运行“cl.exe yourprogram.c”

Then launch that .bat file from PHP. (dunno how you would do that btw)

然后从PHP启动该.bat文件。 (不知道你会怎么做btw)

#2


You need to call the compiler (cl.exe) and linker explicitly (See compiler reference) or use makefiles (g++ reference, but shows the point)

您需要显式调用编译器(cl.exe)和链接器(请参阅编译器参考)或使用makefile(g ++参考,但显示要点)

#3


I am assuming you want to invoke your compiler from a php script?

我假设你想从php脚本调用你的编译器?

If that's the case, you'll first have to invoke the compiler itself - not just pass the source code's filename to system. In the case of MSVC++, you will want to look for "cl.exe".

如果是这种情况,您首先必须调用编译器本身 - 而不仅仅是将源代码的文件名传递给系统。对于MSVC ++,您将需要查找“cl.exe”。

Also, if the corresponding path isn't set in your environment, you will need to use the full path.

此外,如果未在您的环境中设置相应的路径,则需要使用完整路径。

Make sure to first understand, how to invoke cl.exe manually, from a command prompt. Once you understand that, you can try to invoke it from a php script.

请务必首先了解如何从命令提示符手动调用cl.exe。一旦理解了,就可以尝试从php脚本中调用它。

However, you need to realize that there are additional factors to consider, such as for example the time required to compile a file vs. the time allowed for a php script to execute before timing out.

但是,您需要意识到还有其他因素需要考虑,例如编译文件所需的时间与PHP脚本在超时之前执行所允许的时间。

I think, you may need to provide us with additional information on what exactly you want to do, and why you want to do it.

我想,您可能需要向我们提供有关您想要做什么以及为什么要这样做的其他信息。

#4


http://4answered.com/questions/view/7e1694/Compile-C-file-Using-PHP This helps me to compile and run C program (see video on this link). Use this I wrote this php code and it wokrs

http://4answered.com/questions/view/7e1694/Compile-C-file-Using-PHP这有助于我编译和运行C程序(请参阅此链接上的视频)。使用这个我写了这个PHP代码和它的wokrs

<?php

putenv("PATH=C:\\MinGW\\bin");
exec("gcc C:\\test\\q.c -o C:\\test\\q1.exe");
system("C:\\test\\q1.exe",$output);
echo $output;

my q.c

#include <stdio.h>

int main(int argc, char* argv[])
{
  printf("Hello world");
  return 0;
}

So you will see "Hello world0" in $output

所以你会在$ output中看到“Hello world0”

#1


Do a .bat file that:

做一个.bat文件:

  • runs the vcvars32.bat file that comes with Visual Studio
  • 运行Visual Studio附带的vcvars32.bat文件

  • runs "cl.exe yourprogram.c"
  • 运行“cl.exe yourprogram.c”

Then launch that .bat file from PHP. (dunno how you would do that btw)

然后从PHP启动该.bat文件。 (不知道你会怎么做btw)

#2


You need to call the compiler (cl.exe) and linker explicitly (See compiler reference) or use makefiles (g++ reference, but shows the point)

您需要显式调用编译器(cl.exe)和链接器(请参阅编译器参考)或使用makefile(g ++参考,但显示要点)

#3


I am assuming you want to invoke your compiler from a php script?

我假设你想从php脚本调用你的编译器?

If that's the case, you'll first have to invoke the compiler itself - not just pass the source code's filename to system. In the case of MSVC++, you will want to look for "cl.exe".

如果是这种情况,您首先必须调用编译器本身 - 而不仅仅是将源代码的文件名传递给系统。对于MSVC ++,您将需要查找“cl.exe”。

Also, if the corresponding path isn't set in your environment, you will need to use the full path.

此外,如果未在您的环境中设置相应的路径,则需要使用完整路径。

Make sure to first understand, how to invoke cl.exe manually, from a command prompt. Once you understand that, you can try to invoke it from a php script.

请务必首先了解如何从命令提示符手动调用cl.exe。一旦理解了,就可以尝试从php脚本中调用它。

However, you need to realize that there are additional factors to consider, such as for example the time required to compile a file vs. the time allowed for a php script to execute before timing out.

但是,您需要意识到还有其他因素需要考虑,例如编译文件所需的时间与PHP脚本在超时之前执行所允许的时间。

I think, you may need to provide us with additional information on what exactly you want to do, and why you want to do it.

我想,您可能需要向我们提供有关您想要做什么以及为什么要这样做的其他信息。

#4


http://4answered.com/questions/view/7e1694/Compile-C-file-Using-PHP This helps me to compile and run C program (see video on this link). Use this I wrote this php code and it wokrs

http://4answered.com/questions/view/7e1694/Compile-C-file-Using-PHP这有助于我编译和运行C程序(请参阅此链接上的视频)。使用这个我写了这个PHP代码和它的wokrs

<?php

putenv("PATH=C:\\MinGW\\bin");
exec("gcc C:\\test\\q.c -o C:\\test\\q1.exe");
system("C:\\test\\q1.exe",$output);
echo $output;

my q.c

#include <stdio.h>

int main(int argc, char* argv[])
{
  printf("Hello world");
  return 0;
}

So you will see "Hello world0" in $output

所以你会在$ output中看到“Hello world0”