You're probably reading this article after looking up how to install the
g++ compiler for C++ programs. Yes, g++ is indeed for compiling C++ programs :)
On recent versions of Ubuntu, build-related tools like this one are installed by default and thus you should be able to use g++ or some other compiler to run your C++ programs.
However, if you cannot find it, you can jump into the terminal and install it like this:
sudo apt install g++
Let me explain in detail.
Installing only G++
As discussed, your Ubuntu system might already contain such build tools and compilers preinstalled by default. If you are facing trouble launching
g++, you may try installing that package alone.
This is why I asked to install only that package so your error might be fixed. Installing g++ alone might help you fix any linking errors or dependency errors.
If you did not know already,
g++ is simply a link to the
gcc compiler (which can compile both C and C++ programs), configured to compile C++ programs specifically. GCC does not compile C++ programs by default, hence a bit of configuration is needed. It is just a part of the GNU Compiler Collection.
g++ is available in the official Ubuntu repositories. You can install it via this command:
sudo apt install g++
To check the version of
g++ in the system, add a
-v or a
--version tag to the command.
Installing along with other tools
If you would like to get the other tools as well, there's a package by the name build-essential. Installing this bundle will give you different compilers that you usually work with, in a Linux system.
This build-essential bundle is a part of Debian, which contains software that is basically required to construct .DEB files:
gcc- GNU Compiler Collection (to compile C and C++ programs)
g++- compile C++ programs
make- compile software (that is built manually)
dpkg-dev- Build .deb files, and more.
Your use case might not always be installing a deb package, it might be as simple as running C++ programs. If you do not find any of these packages in your system, go ahead and install the entire bundle.
To install the build-essential bundle, you can follow this command:
sudo apt install build-essential
This command will install whatever packages are missing in your system, hence do not feel like you will miss a tool by mistake.
You can just check out the version with the
-v (detailed) or the
--version (brief) tag.
Installing specific versions of g++
If you are compiling something very specific, and you need a particular version of the
g++ compiler for that, you might need to install the specific version that is compatible with your program.
If you're just a normal user trying out C++ programs, the latest version available in the Ubuntu repositories will do the job for you. This section is for advanced users who know what release versions of the language they are working on.
At the time of writing, with the Ubuntu 22.04 LTS, GCC compilers version 11 is supplied. Let's say, you need version 9 of this compiler.
To get that specific version, add a
-9 to the package name, like this:
sudo apt install g++-9
At the time of writing, versions of
g++ below 9 are not officially supported. You might have to enable the universe repository(TO BE LINKED) in order to get the older 6 or 7 versions.
If you need to work on the ninth
g++ version, you might have to use
g++-9 instead of simply
g++ to make use of that version.
This is a simple tutorial to install
g++ for various purposes. While g++ can run C programs, it is better to use it for C++ programs.
I hope you learnt something while reading this article. While you finish, the following image is kind of what
g++ actually is :)
I hope you enjoyed reading this article. Make sure to drop your doubts in the comments section.