Code Breaker
Code Breaker

Reputation: 499

Compile C code with out making a .C file

I am try to compile a 'C' code in php. I am using windowsXP and cygwin to compile the code. I run the code as:

PHP Code

$a = ' int main(){ int i; for(i= 0;i!=10;i++) printf("Its Worked");return 0;}';

$o = exec('echo '.$a.' | c:\cygwin\bin\gcc-3 -xc - -o "c:\o\ank.exe"', $errD , $err);

It is perfectly work and make a .exe file as I want.

but if there is a new line in the code i.e. the code is now

$a = ' int main()
{ 
int i;
for(i= 0;i!=10;i++)
 printf("Its Worked");
return 0;
} ';

$o = exec('echo '.$a.' | c:\cygwin\bin\gcc-3 -xc - -o "c:\o\ank.exe"', $errD , $err);

It gives compile error.

Can some one tell me the way to compile such code with out making a .c file. One more problem with this I can not include any header file because it need a new line.

It is also not recognized special characters like '%' etc.

All answers and suggestions are welcome.

Upvotes: 2

Views: 320

Answers (1)

Yonatan Koral
Yonatan Koral

Reputation: 17

use before exc command: $a = preg_replace("/\n/",' ',$a); // remove all new line so the code actually run contains one line only...

Upvotes: 1

Related Questions