Saturday, December 22, 2012

Compile and execute C++ program on Linux



The Borland Turbo C++ compiler which works on Windows operating System, does not have all the capabilities of C++ programming. The core C++ compiler is present on Linux operating System. The gcc is bundled with the compiler of C++ too. It is basically GNU C/C++ Compiler Collection. You may use g++ compiler to compiler your C++ program file on Linux.

Let's see, how to do it.

1.      Open any text editor such as gedit, vi, vim, or emacs and types your program in it. Save the file with extension .cpp. It is required to identify that the file contains C++ code.
2.      You need to write this code in pure C++ format. Look at the program no.1, which is written for Turbo C++ compiler and program no. 2 is written for gcc/g++ compiler.

#include<iostream.h>
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
void main()
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 1: Written for Turbo C++ Compiler.


#include<iostream>      //Line-1
using namespace std;    //Line-2
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
int main()              //Line-3
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 2: Written for Linux g++ compiler.

3.      Look at the changes made in the second program, which is written for g++ compiler. In line number-1 #include<iostream> is written instead of #include<iostream.h>. All C++ related header files must be included in this fashion only. If C programming header files such as stdio.h are required then these are included as usual fashion such as #include<stdio.h>.
4.    The Line-2 is an extra line in program-2. It is pure C++ statement. In C++, namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The keyword 'using' is used to introduce a name from a namespace into the current declarative region. All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
5.      The robust compiler like g++ says that your main() function must return only 'int', it must not be void. It is compulsory to return the int from main().
6.      In order to compile the program use command g++ on the terminal:

g++ add.cpp             [Command-1]

7.      It will compile the file, check for the errors, prints on screen if any and generates an a.out output file. The a.out is default output file name of g++ compiler. You may change it by providing -o option to the command such as,

g++ add.cpp -o addition [Command-2]

8.      Now, this will generate output executable file by name 'addition'.
Now execute the file as,

./a.out                 [Command-1 output]
./addition              [Command-2 output]

9.      Your program will get executed and generate output.

Enter two numbers: 56 41
Addition is: 97

No comments:

Post a Comment