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.

34 thoughts on “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!””

  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.

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

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

  5. 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?

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

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

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

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

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

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

  12. Nice, but how can I start the executable via the browser?
    The browser just wants to download (ie. save) the file… 🙂
    Does the binary perhaps need to be placed in the cgi-bin directory?
    An example for using the binary under Apache would be nice… TIA

  13. Solved! Here are the neccessary steps:

    1) Enable .shtml in Apache conf file, ie.
    AddOutputFilter INCLUDES .shtml
    2.) Compile the C++ program and put the binary (here myprog.exe) to cgi-bin. You should also get the getpost.h from the followup article (cf. above).
    3.) Call the binary via an .shtml file (here index.shtml) with following content:

    Dynamic HTML with Server Side Includes (SSI)

    4.) Call in web browser:
    http://www.example.com/index.shtml

  14. Solved! Here are the neccessary steps:

    1) Enable .shtml in Apache conf file, ie.
    AddOutputFilter INCLUDES .shtml
    2.) Compile the C++ program and put the binary (here myprog.exe) to cgi-bin. You should also get the getpost.h from the followup article (cf. above).
    3.) Call the binary via an .shtml file (here index.shtml) with following content:
    #
    #
    #
    # Dynamic HTML with Server Side Includes (SSI)
    #
    #
    #
    #
    #

    (remove the leading ‘#’ from all lines above)

    4.) Call in web browser:
    http://www.example.com/index.shtml

  15. sir,
    i want to learn cgi in c++
    in vc++9.0 i built the small program i got .exe file then what we have to do to view output in browser

  16. I Have windows operating system (XP 32 Bit) installed on my Laptop… But My webhost is using linux (i686),

    So, I need help on how to build cgi executables for different operating system architectures…
    Is there any solution or I need to install linux on my laptop.

  17. There is something called cross-compiling. GCC has this feature. Basically, it allows you to compile on one platform for a different platform.

  18. hai sir,,

    iam using codeblocks to implement C++
    but in there’s no feature to compile C++ to cgi

    what editor to support and automaticly compile c++ to cgi??

    iam newbie

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.