PDA

View Full Version : Compiling C/++ CGI apps



cracker
10-11-2008, 11:52 AM
I have search on this forum as well as doing a fair bit of googling and haven't been able to come up with the solution to my problem. I spent an hour at least trying to compile and run a simple CGI app. I would be extremely grateful if someone would be able to help me out on this as I would really like to run CGI apps.

I found a single topic on the forum regarding how to compile a CGI app with a PHP script. I used the code and the object file gets produced as well as the cgi binary but I get an error when trying to run it:


500 Server Error
A misconfiguration on the server caused a hiccup. Check the server logs, fix the problem, then try again.

g++ shows no output even with --pass-exit-codes so it's hard to determine what is going on. :rolleyes:


<?php
echo "compiling...<br>";
shell_exec('g++ -c test.c');
shell_exec('g++ test.o -o ./cgi-bin/test.cgi');
echo "done compiling!";
?>



#include <stdio.h>
#include <stdlib.h>

int main() {
cout << "this is a test";
return 0;
}

Atte
10-11-2008, 08:38 PM
You need to echo at least the Content-Type header before the actual data. So you should actually be printing:
Content-Type: text/html\r\n
\r\n
this is a test

cracker
10-11-2008, 09:12 PM
Thanks a lot! All is working well now. Server-side programming is a bit overwhelming even though I not a novice in desktop programming. I also found the proper PHP script to use to compile as well which will yield a proper output if there is an error in compilation, linking:


<?php
echo "compiling...<br>";
$output=shell_exec('g++ -c test.c 2>&1');
$output2=shell_exec('g++ test.o -o ./cgi-bin/test.cgi 2>&1');
echo "$output<br>$output2<br>";
echo "done compiling!";
?>