Skip to content

Installation of g++ compiler in LINUX

GNU C++ compiler is a fairly portable optimizing compiler for C++/C. g++ compiler is used to execute both c and c++ programs.

Command for installing G++ Cmpiler

Execute this command in terminal :

$ sudo apt-get install g++

Steps for compiling and executing c++ program

1. Write and save the program in gedit
    
    //Sample c++ program
    #include<iostream>
    using namespace std;
    main()
    {
         cout<<"Sample c++ Program";
         return 0;
    }

    Save the program as sample.cpp (.cpp is the extension for c++ program).

2. Compile the program

   Write the following command in terminal after installing g++ compiler.
   $ g++ sample.cpp -o sample.o

   In this command,
  • g++ indicates the compiler name,
  •  sample.cpp is a file name of the source program,
  •  -o option specifies the file name of the output program.
  •   sample.o is an output file.
   If there is no syntax/semantic error in you program then the compiler 
   will successfully generate an executable file,otherwise fix the problem 
   in your code.

3. Executing the program
   Write the following command in terminal after compiling c++ program.
   $ ./sample.o
 
   After executing this command it gives the output as:
   Sample c++ Program

If you have any problems then share it through comments.
%d bloggers like this: