简体   繁体   中英

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? I am using 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.

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


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.

Do a .bat file that:

  • runs the vcvars32.bat file that comes with Visual Studio
  • runs "cl.exe yourprogram.c"

Then launch that .bat file from PHP. (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). Use this I wrote this php code and it 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

#include <stdio.h>

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

So you will see "Hello world0" in $output

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

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".

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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