Skip to content
g++: command not found

g++: command not found

DodaTech 2 min read

The g++: command not found error means the GNU C++ Compiler is missing. It happens when trying to compile C++ code but the compiler is not installed.

What It Means

The shell cannot find the g++ binary. Unlike gcc (the C compiler), g++ is often part of a separate package even on systems where GCC is installed.

Why It Happens

  • g++ is not installed (common on minimal Docker images and fresh systems)
  • Only gcc was installed, which handles C but not C++
  • The build-essential meta-package was not installed (Debian/Ubuntu)
  • PATH does not include the directory where g++ is located

How to Fix It

Step 1: Install g++ on Debian/Ubuntu

The recommended approach installs the full build toolchain:

sudo apt update
sudo apt install build-essential

This installs g++, gcc, make, and other development tools together. Alternatively, install only g++:

sudo apt install g++

Step 2: Install g++ on Fedora/RHEL

sudo dnf install gcc-c++

Step 3: Install g++ on macOS

Install Xcode Command Line Tools:

xcode-select --install

On macOS, g++ is often mapped to clang++. The command still works the same way.

Step 4: Install g++ on Windows

Use MinGW-w64 or MSYS2:

# With Chocolatey
choco install mingw

# Or with MSYS2
pacman -S mingw-w64-x86_64-gcc

Step 5: Test the installation

Create a simple C++ program:

// hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, g++!" << std::endl;
    return 0;
}
g++ hello.cpp -o hello
./hello

Expected output:

Hello, g++!
Do I need both gcc and g++?
Not necessarily. If you only write C++, installing g++ is enough — it can compile both C and C++ code. However, build-essential installs both and is the standard setup for C/C++ development on Linux.
What if 'apt install g++' fails with 'unable to locate package'?
Run sudo apt update first to refresh your package lists. If the package still isn’t found, check your /etc/apt/sources.list for proper repository configuration. On older Ubuntu versions, enable the universe repository with sudo apt-add-repository universe.
Can I specify the C++ standard version when compiling?
Yes. Use the -std flag: g++ -std=c++17 hello.cpp -o hello. Supported standards include c++11, c++14, c++17, c++20, and c++23. This ensures your code compiles against the desired language version.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro