Demystifying the "make: g++: command not found" Error

Have you tried building interesting open source applications on CentOS or RHEL only to be stopped in your tracks by the head-scratching "make: g++: command not found" error? You are not alone – this is a common issue new Linux users face when attempting to compile C/C++ applications from source.

In this post, I will explain what causes this g++ command not found error and how to resolve it with a simple one-line fix. My goal is to help you setup a robust C++ development environment on CentOS/RHEL servers so you can build C++ apps without hiccups.

Why is g++ Important?

Before jumping to the solution, it helps to understand what g++ represents. GNU g++ is the widely used compiler for C++ code, similar to how gcc handles compiling C programs. Most open source C++ software relies on invoking g++ to build the executable binaries.

C++ is one of the most popular programming languages used today. StackOverflow‘s 2021 survey found 38% of professional developers code in C++. The Tiobe index also ranks it in the top 3. This wide usage means many Linux tools and applications – like the Blender 3D suite or Qt Framework – are written in C++ and need g++ available to compile them.

For example, when installing Blender from source, running the configure script fails with this familiar error about g++ missing if you don‘t have the compiler present.

So in a nutshell, g++ can be considered the standard for compiling C++ applications on Linux. Not having it blocks building such software that depends on it.

Statistics on the Popularity of C++

Some interesting metrics that showcase the popularity of C++ among developers:

  • Stack Overflow‘s Developer Survey 2022 found C++ was ranked the 5th most popular programming language with 37.2% of developers using it.

  • On the Tiobe Index for August 2022, C++ featured at the 3rd place in its index of language popularity over the last two decades.

  • The PYPL Popularity of Programming Language index which analyzes Google searches for language tutorials ranks C++ in 6th place with a 4.7% share.

  • On GitHub, C++ is the 6th most used language for projects on the platform as per the Octoverse report.

The numbers validate how C++ remains entrenched within the open source ecosystem that underpins Linux distributions. This underscores why access to the g++ compiler is a prerequisite for unlocked development on CentOS/RHEL systems.

Attempting to Compile Blender Fails without g++

To demonstrate the reported error in action, let‘s see what happens when compiling a real-world C++ application lacking g++. I‘ll use the example of installing the popular open source Blender 3D modeling software from source.

Start by downloading and extracting the latest Blender source tarball from blender.org. This gives you a folder like blender-3.3.0.

Browse into this directory and kick off the compilation with:

./configure
make 

The configure script scans your system to ensure the environment meets Blender‘s numerous build dependencies before initiating the make driven compilation.

On a vanilla CentOS 7 or RHEL 8 system however, this immediately results in:

Makefile:146: *** Blender requires GCC 4.7.x or later.  Stop.

Indicating the system is missing a suitable g++ version meeting Blender‘s needs.

If Development Tools were present, we‘d instead see actual compilation output. The failure showcases the blockade g++ unavailability poses for building C++ software from their source distributions.

Now that we‘ve understood the actual problem, let‘s move on to resolving it for good.

Fixing Missing g++ Compiler with Development Tools

The quickest and most thorough way to address the missing g++ error is by installing the Development Tools software package group using yum.

Run:

sudo yum groupinstall "Development Tools" 

This downloads and sets up the GCC compiler chain including g++, standard libraries and other essential development packages.

Once complete, verify that g++ is now available by checking the version:

g++ --version

Which should output details confirming GCC got installed:

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44).
Copyright (C) 2015 Free Software Foundation... 

We now have the requisite g++ compiler available to build C++ applications.

To validate, we could attempt recompiling Blender which should now proceed much further before any errors. But for a quicker test, consider a simple "Hello World" C++ program.

Create hello.cpp:

#include <iostream> 

int main() {
  std::cout << "Hello World!};
  return 0;
}

And compile with:

g++ -o hello hello.cpp

This will successfully build the hello executable to confirm g++ functions properly. Running ./hello prints out Hello World – verifying your C++ development environment is setup correctly.

Why Installing Development Tools Works

Using yum‘s Development Tools package group to solve g++ not found carries a couple benefits:

Resolves the root cause: It directly installs gcc, g++ and related binaries which source code builds need to compile C/C++ code.

Complete dependency resolution: Just getting the compilers inplace won‘t suffice. You also need standard headers, linking libraries and build tools for compilation to work across different systems. Groupinstall figures all these dependencies out automatically.

So Development Tools gives you the bare minimum to start compiling applications written in C, C++ and other related languages.

Note that if you only care about C/C++, you could instead install the more focused C/C++ Development Tools subset. But the main group also sets up compilers and tools to work with codebases in languages like Python, Go, Rust etc.

Best Practices for Managing g++ Install

Once your CentOS/RHEL system has g++ functioning via Development Tools, some recommendations for keeping things smooth:

Stay Updated: Check periodically if any g++/gcc updates are available through yum by running:

yum check-update

This allows you to upgrade the compilers applying security fixes or improvements.

Grow Your Skills: With the initial hump of getting set for C++ out of the way, further develop your know-how of utilizing g++ and tooling by reading docs at cppreference.com or following C++ communities.

Soon you‘ll pick up handy techniques for debugging odd build errors, linking commonly used libraries, sanitizing code or making apps performant.

Consider Alternate Setups: Later if your needs diversify, look into tailored development environment options like gcc toolsets, the devtoolset packages or all-in-one setups like Wayland‘s Developer Workstation.

Troubleshooting Further Issues

In some tricky cases, the Development Tools install may not entirely resolve matters. If g++ still shows up as missing, here are additional things to investigate:

Check gcc Version: CentOS 7 has an older gcc by default unlike RHEL 8 which matches upstream. If the app needs newer compiler features, compile may fail. Verify version with gcc --version and consider upgrading gcc separately if essential.

Examine PATH Settings: For odd instances where g++ is present after groupinstall but make can‘t find it, ensure PATH includes the relevant binary locations like /usr/bin and /usr/local/bin. Also load updated shell config with source ~/.bashrc.

Test with Simpler C++ Code: Isolate whether g++ itself functions properly by attempting to compile a simple HelloWorld cpp program as discussed previously. If even that fails, deeper environment issues may be preventing use of the compiler.

Conclusion

I hope this post clearly explains the dreaded "make: g++: command not found" error frequently faced when compiling C++ applications from source without required compilers.

The simplest and most thorough fix is to setup essential development tools like g++ using the Development Tools package group in yum. This single install then reliably enables compiling and building applications written in C++ and other languages.

With a functioning g++ in place, plus best practices to manage updates and growth of skills – you are ready to unlock compiling the wide gamut of open source software on CentOS and RHEL. No more pesky blocking errors, only smooth sailing development ahead!

Let me know if you have any other suggestions to handle the g++ compiler issue or run into any problems even after installing Development Tools.