Guy Rutenberg

Keeping track of what I do

Introduction to C++ CGI

with 13 comments

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.

Share and Enjoy:
  • del.icio.us
  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google Bookmarks
  • Simpy

Written by Guy

August 10th, 2007 at 12:22 am

Posted in C/C++, Tutorials

Tagged with

13 Responses to 'Introduction to C++ CGI'

Subscribe to comments with RSS or TrackBack to 'Introduction to C++ CGI'.

  1. You don’t need to call cout 5 times
    This is a waste of resourses, call it once

    cout”Hello World!”"

    Dave

    23 Dec 07 at 20:43

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

    Guy

    23 Dec 07 at 23:47

  3. plz i nid mor

    shantu

    12 Feb 08 at 21:08

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

    Guy

    12 Feb 08 at 22:18

  5. What web server should I use? and if it’s Apache, how should I configure it to treat the executable as a CGI file?

    Astra

    14 Apr 08 at 13:16

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

    Guy

    14 Apr 08 at 13:32

  7. I really like this tutorial that you made. This is one of the few websites that I’ve found that teaches a C++ CGI. Everything works for me until I get to the point of having it work on the web server. I’m somewhat confused on what to do there. I create a console application, compile it, then I put the .cpp file on the server and rename it to .cgi, but it doesn’t work. Also, the compiled version doesn’t appear to have a simple .exe. Do you have or know any way that this could be resolved?

    hopefulcd

    1 Jun 08 at 00:00

  8. Hi, you should put the compiled version on the server and in most cases you should add a .cgi extension to executable. You will probably have to compile the code directly on the server unless you have the same OS as the server. On what kind of server have you used (Apache, LIghttpd or something else? What operating system?

    Guy

    1 Jun 08 at 07:21

  9. I think you found the error that I’ve been searching for. The OS that the code was compiled on was a windows based OS. The server that I put the code on was a Linux based OS. Is there a program to use or a way that the script could be compiled to work on multiple OS ?

    hopefulcd

    1 Jun 08 at 12:08

  10. Hi again, as far as I know, there is no way the same compiled binary will work both on Windows and Linux, as each system uses completely different binary format for executables. But usually it’s not hard to write portable C++ code that will compile both on Linux and Windows. If you can’t compile the code directly on your server, I suggest using a Linux live-cd, such as Knoppix, compile the code on it, and then transfer it to the server.

    Guy

    1 Jun 08 at 13:59

  11. Hi, thanks for sharing. You may want to write CGI class and reduce usage of std::cout to speed up the outputting :-)

    Viet

    6 Jul 08 at 16:32

  12. do you type it(the code) into notepad and then how do you save it and run it with your browser?

    crazy_e

    9 Mar 10 at 04:42

  13. You’ll need to compile it using a c++ compiler and configure your web-server to use the code. The process is compiler and server specific.

    Guy

    9 Mar 10 at 07:30

Leave a Reply