Linux Mint

How to Install g++ on Linux Mint 21

Computers generally interpret the instructions in machine language that consist of zeros and ones and if you want to learn programming then you must be aware that to translate the instruction into machine language compilers are used. Likewise, g++ is a compiler that can compile both C and C++ language and is most used by the Linux users. So, if you want to install g++ on Linux Mint 21 and want to learn how to compile a C++ code using it then give this guide a thorough read.

Installing g++ on Linux Mint 21

To install g++ on Linux Mint, follow the steps listed below:

Step 1: Update the package list of the default package manager as it is recommended before installing any application on Linux:

$ sudo apt update

Step 2: Next, install g++ with the help of below command:

$ sudo apt install g++ -y

Step 3: Now to see if this editor is installed correctly check its version:

$ g++ --version

Compiling a C++ code using g++ on Linux Mint 21

There are few easy steps that one needs to follow to learn how to write and compile C++ code using g++ in Linux Mint 21:

Step 1: Create .cpp file with your desired name:

$ nano mycode.cpp

Step 2: Next write the C++ code in the created file, below is the simple code that I have written in the .cpp file:

//www.linuxhint.com
#include<iostream>

using namespace std;

// Here execution of code begins
int main()
{
   // displays string on screen
   cout<<"Hello LinuxHint!\n";

   return 0;
}

Note: Ensure that you are in the same directory where the created file is present.

Step 3: Now just the save and close the file by pressing ctrl+x and compile the file using:

$ g++ mycode.cpp -o mycode

Step 4: Next, run the compiled C++ file using:

$ ./mycode

So, this is how one can write and compile C++ code using the g++ compiler in Linux Mint 21.

To remove the g++ compiler from Linux Mint 21 use:

$ sudo apt remove g++ -y

Conclusion

The primary purpose of a compiler is to convert the programming language into machine language so that machines can understand the instructions given to them. There are a variety of compilers available that support different programming languages, the g++ is mainly used to compile C and C++ language. To install g++ on Linux Mint 21 one can use its default package manager; the detailed installation process is mentioned in this guide.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.