Debian

How to Install and Use g++ in Debian

Some programming files required compilers for handling the source code. There are different compilers available for the C language and g++ or GNU C++ compiler is one of them. g++ is a GNU C++ Compiler that is used for preprocessing and compilation of source code to generate an executable file. The g++ compiler offers various functions straight from the terminal of your Debian. It can compile .c and .cpp files.

In this tutorial, we will demonstrate the installation and usage of g++ in Debian.

How to Install g++ in Debian

The meta package known as build-essential which contains the g++ compiler, libraries, and other utilities for compiling software is present in the default repository of Debian. Update the system before starting the installation procedure through the following command:

sudo apt update

Run the following command to install the built-essential on Debian:

sudo apt install build-essential

Verify the successful installation of g++ through the following command:

g++ --version

How to Use g++ in Debian

Using g++ for the compilation of .cpp files is simple. Create a newfile.cpp with the following command:

sudo nano newfile.cpp

Add the text in the file, for example, I have added the following code in the text file:

#include<iostream>

int main()

{

std::cout<<"Hello this is Linuxhint";

  return 0;

}

Save the file by pressing Ctrl + X and run the following command to make it an executable file:

g++ <program-name>.cpp -o <executable-filename>

Note: g++ can also compile C language code.

For example, I am compiling the newfile.cpp into an executable file named newfile:

g++ newfile.cpp -o newfile

The compiler will create the binary file named newfile in the same directory, and run the file via the below-given command:

./newfile

In my case, the executable file is newfile so the output is Hello this is Linuxhint.

Bottom Line

The g++ converts the .cpp high-level language file into a low-level language file by making it executable. Installation is simple as it is present in the default repository of Debian. In the above guide, we have successfully installed and used g++ on Debian.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.