Sunday, August 29, 2010

Simple C++ code on g++ compiler

I remember my encounter with the g++ compiler, it was just before a coding competition. Owing my background to Windows and turboc, it was a good experience to work on linux(Ubuntu) and g++ compiler.  The following simple steps show how to write and execute a simple c++ code.

1. Check your g++ installation by typing the following on the terminal:
~$g++
If g++ is installed the following will be the response:
g++: no input files

If not then Ubuntu and Debian users type the following to install:
~$sudo apt-get install g++

2.After checking your compiler open and empty document and name it suppose hello.cpp

3.Type the following using your favorite text editor

#include<iostream>
using namespace std;
int main()
{
cout<<"hello";
return 0;
}

save the file. Make sure you save it in your current working directory.

4. On the terminal type the following:
~$g++ hello.cpp
~$./a.out
and vol la you are done.