C-programming - How to execute c binary as cgi script, Compile C source as cgi

How to execute c binary as cgi script

How to execute C binary as cgi script?

Compile C source as cgi

You can execute a C source as a cgi script on web servers. To do so follow the steps given below. CGI scripts are nothing but perl script.

C Source
- Save the C program as text.c
#include <stdio.h>
int main(void) {
  printf("Content-Type: text/plain;charset=us-ascii\n\n");
  printf("Hello world\n\n");
  return 0;
You can use the above C program that just prints Hello world but preceded by HTTP headers as required by the CGI interface. Here the header specifies that the data is plain ASCII text.

How to compile the C program as cgi binary?
# gcc -o test.cgi test.c

Compile the C program using gcc compiler and save the output to test.cgi. The test.c program is compiled and the compiled executable is stored in test.cgi.

After compiling, loading, and uploading the cgi script in cgi-bin folder, Make sure that the file has 755 permission for executing the script. Now you should be able to test the script simply by entering the URL in the browser's address bar. (eg: http://yourdomain.com/cgi-bin/test.cgi)

Using a C program as a CGI script

To set C program as CGI script the C code has to be converted into a binary executable program. Always make sure that your gcc compiled source is supported on linux server. Mostly the source compiled in windows machine has problem on linux server. This is often problematic, since people largely work on Windows whereas servers often run some version of UNIX or Linux. The system where you develop your program and the server where it should be installed as a CGI script may have quite different architectures, so that the same executable does not run on both of them.

This may create an unsolvable problem. If you are not allowed to log on the server and you cannot use a binary-compatible system (or a cross-compiler) either, you are out of luck. Many servers, however, allow you log on and use the server in interactive mode, as a "shell user," and contain a C compiler.

You need to compile and load your C program on the server (or, in principle, on a system with the same architecture, so that binaries produced for it are executable on the server too).

The C language was originally designed for an environment where only ASCII characters were used. Nowadays, it can be used with caution for processing 8-bit characters. There are various ways to overcome the limitation that in C implementations, a character is generally an 8-bit quantity.

The topic on C-programming - How to execute c binary as cgi script is posted by - Maha

Hope you have enjoyed, C-programming - How to execute c binary as cgi scriptThanks for your time

Tech Bluff