C++ with CGI

Before doing example, You have to configure cgi-bin with apache server.
If its done then try your first example.

Open an empty file and write following c++ code.

#include <iostream>
using namespace std;

int main ()

cout << "Content-type:text/html \n\n";
cout << "Hello World - First CGI Program";

return 0;

Now save this file with .cpp extension(example.cpp) in public_html/cgi-bin folder.
Then compile that program.
$ g++ -o example example.cpp

This will create example file which is executable.
Then open browser and open following link”


This will show you following o/p on browser.

Hello World – First CGI Program

If you find any problem then share it as comments.

5 Replies to “C++ with CGI”

  1. ‘/n’ or ‘endl’ didn’t work for me. Then how to add a new line without using tag.

      1. Mandeep Simak says: Reply

        n or endl only works if context-type is plain otherwise you have to use </br> tag for new line in html context.

        1. You mean context-type:text/plain ?
          Will it work if I include both plain and html context-types?

          1. Mandeep Simak says:

            Yes, Content-Type: text/plain for plain text.
            If you include both then only first type will be executed and according to that text type will be used i.e. html or plain.

Leave a Reply

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