简体   繁体   English

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

[英]How can I compile C/C++ code in PHP on the Windows platform?

Can anyone help me with the compilation of C++ code in PHP on the Windows platform? 谁能帮助我在Windows平台上用PHP编译C ++代码? I am using Microsoft Visual C++ 6.0. 我正在使用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". 在这段代码“ test.c”中,我正在从文件“ data.txt”中读取。 The contents of data.txt is changed every 5 seconds. data.txt的内容每5秒更改一次。 That's why first I need to compile in PHP and then run the new exe file. 这就是为什么我首先需要在PHP中进行编译,然后运行新的exe文件的原因。

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. 然后从PHP启动该.bat文件。 (dunno how you would do that btw) (不知道你怎么会那样做)

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

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). http://4answered.com/questions/view/7e1694/Compile-C-file-Using-PHP这可以帮助我编译和运行C程序(请参阅此链接上的视频)。 Use this I wrote this php code and it wokrs 使用这个我写了这个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 qc 我的qc

#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”

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. 确保首先从命令提示符下了解如何手动调用cl.exe。 Once you understand that, you can try to invoke it from a php script. 了解这些内容后,您可以尝试从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. 我认为,您可能需要向我们提供有关您真正想做什么以及为什么要这样做的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM