10.08.07
Introduction to C++ CGI
In this post and its follow ups I intend to cover the basics of CGI programming in C++. There are great performance gain in writing CGIs in C++ compared to interpreted languages such as PHP and it’s usually it’s even faster than PHP scripts which are interpreted via mod_php. On the other hand PHP and other traditional web development languages are well suited for the task, by means of libraries and development time. However developing small highly efficient CGI scripts in C++ is easier than you think.
We shall start with a writing a basic “Hello World” CGI. Basically, all output to stdout is directed by the web server to the client’s web browser. However, the web server requires the CGI application to declare in the first line on output what is the MIME type of the content it is producing. This line should be followed by a blank line. The common MIME types are text/plain for unformatted text and text/html for HTML formated content. The MIME type declaring should look something like this:
cout<<"Content-type: text/html"<<endl<<endl;
So getting back to our “Hello World” CGI, it will look like this:
#include <iostream> using namespace std; int main() { cout<<"Content-type: text/plain"<<endl<<endl; cout<<"Hello World!"<<endl; return 0; }
Congratulations! You have just created your first C++ CGI. To see it in action, move it to a directory on the web server configured to run CGI scripts, this directory is usually called cgi-bin. Now just point your web browser to the the address of CGI app to see in in action.
Now that you have seen the basic example, let’s move on to a bit more complex example.
#include <iostream> using namespace std; int main() { cout<<"Content-type: text/html"<<endl<<endl; cout<<"<html><body>"<<endl; cout<<"<h1>Hello World!</h1>"<<endl; cout<<"This is HTML formatted Hello World C++ CGI"<<endl; cout<<"</body></html>"<<endl; return 0; }
As you see, even writing pages with HTML content isn’t that hard. Yes, C++ may not be the easiest programming language for developing web applications, but it may turn out easier than you thought.
In this post I covered creating basic CGI that don’t relay on user input. As proccessing user input in one of the goals of CGI, I will cover it next time.






Dave said,
December 23, 2007 at 8:43 pm
You don’t need to call cout 5 times
This is a waste of resourses, call it once
cout”Hello World!”"
Guy said,
December 23, 2007 at 11:47 pm
Yes, you’re it is a waste of resources, but I think it is clearer this way. If you want to gain this extra bit of performance you can just go ahead and use for the example printf(). It won’t make the program complicated (as this example is pretty straight forward) but it will give you an extra edge.
When giving example, one tries to simplify stuff and focus on the important. When one tries to focus on everything, the entire point of the example is lost.
shantu said,
February 12, 2008 at 9:08 pm
plz i nid mor
Guy said,
February 12, 2008 at 10:18 pm
There is a follow up tutorial:
http://www.guyrutenberg.com/2007/09/07/introduction-to-c-cgi-processing-forms/
it discusses how to process forms using C++ (forms like in GET and POST).
If there is a topic your still interested about please write to me, and I will cover it.
Astra said,
April 14, 2008 at 1:16 pm
What web server should I use? and if it’s Apache, how should I configure it to treat the executable as a CGI file?
Guy said,
April 14, 2008 at 1:32 pm
Apache is a good choice. Please notice that running regular CGIs is quite slow under heavy load. If you try to build something for production you should use FastCGI or SCGI (they are very similar in the way you work with them).
The default configuration of Apache would be fine, sometimes you will have to give your executables “.cgi” extension. Remember to make sure that the CGI executables that you created are indeed executable by the web-server user.