TechBuzzPro

How to Install g++ on Windows 11: Complete Guide

· 5 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can install g++ (the GNU C++ compiler) on Windows 11. The most reliable method is installing MinGW-w64 via MSYS2, which provides a native Linux-like development environment and integrates seamlessly with VS Code or the command line.

What You'll Need

  • A Windows 11 PC with administrative privileges.
  • An internet connection for downloading packages.
  • A text editor or IDE (Visual Studio Code is recommended but optional).

Step-by-Step

  1. Download MSYS2: Navigate to the official MSYS2 website and download the installer for Windows x86_64. Run the downloaded executable file.
  2. Install MSYS2: Follow the installation wizard. Use the default installation path (usually C:\msys64) unless you have specific requirements. Ensure the option to add MSYS2 to your system PATH is checked if available, though we will configure this manually for precision.
  3. Launch the Terminal: Open the Start menu, search for "MSYS2 UCRT64", and launch it. This environment uses the modern Universal C Runtime (UCRT), which is preferred over the older MINGW64 environment for newer Windows versions.
  4. Update System Packages: In the terminal window, type the following command and press Enter:
    pacman -Syu
    Accept any prompts by typing 'y' and pressing Enter. If the terminal closes after this update, relaunch the "MSYS2 UCRT64" shell and run the command again until no more updates are pending.
  5. Install GCC and G++: Once the system is fully updated, install the compiler suite by running:
    pacman -S mingw-w64-ucrt-x86_64-gcc
    This package includes both gcc (C compiler) and g++ (C++ compiler). Press 'y' when prompted to confirm the installation.
  6. Verify Installation: After the installation completes, verify that the tools are accessible by typing:
    g++ --version
    You should see version information displayed in the terminal.
  7. Configure Environment Variables (Optional but Recommended): To use g++ from any Command Prompt or PowerShell window without launching MSYS2 first, you need to add the binary path to your Windows System Variables.r>
    1. Press Win + S, search for "Environment Variables", and select "Edit the system environment variables".
    2. Click the "Environment Variables..." button.
    3. Under "System variables", find and select the variable named Path, then click "Edit".
    4. Click "New" and add the following path (adjust if you installed MSYS2 elsewhere):
      C:\msys64\ucrt64\bin
    5. Click "OK" on all open dialogs to save changes. Restart any open terminals or IDEs for the changes to take effect.
  8. Test Your Setup: Create a new file named hello.cpp with some C++ code. Open your terminal, navigate to the folder containing the file, and compile it using:
    g++ hello.cpp -o hello.exe
    Run the executable with ./hello.exe or .\hello.exe to ensure it works correctly.

Common Mistakes

Using the Wrong Shell: Many users install MSYS2 but try to run commands in the standard Windows Command Prompt without configuring the PATH correctly. Always ensure you are either using the MSYS2 shell or have added the correct ucrt64\bin directory to your Windows PATH.

Skipping Updates: Failing to run pacman -Syu completely before installing packages can lead to dependency errors. MSYS2 requires a full system update before adding new software.

Confusing Environments: MSYS2 offers several shells (MINGW64, CLANG64, UCRT64). For modern Windows 11 development, UCRT64 is generally recommended due to better compatibility with recent Windows APIs. Using MINGW64 may work but can sometimes encounter issues with newer system libraries.

Tips

Use Visual Studio Code: If you plan to do significant C++ development, install the "Code Runner" extension in VS Code. It allows you to right-click and run C++ files directly, automatically detecting the g++ compiler.

Keep Paths Short: When compiling large projects, avoid excessively long file paths. Windows has a character limit for paths that can cause compilation failures.

Backup Your Configuration: If you customize your MSYS2 environment, consider backing up the etc/pacman.conf file, as updates might overwrite custom repository settings.

Troubleshooting

Command Not Found: If you receive a "'g++' is not recognized" error, check your System Environment Variables. Ensure C:\msys64\ucrt64\bin is listed in the Path variable. You may need to restart your computer or at least reopen your terminal for the changes to apply.

Compilation Errors: If g++ runs but fails to compile, ensure your source file has the .cpp extension. Also, check for syntax errors in your code. Use the -Wall flag (g++ -Wall hello.cpp) to enable warnings that can help identify potential issues.

Package Manager Issues: If pacman fails to update or install packages, check your internet connection. Sometimes, running pacman -Syuu (note the double U) can resolve complex dependency conflicts, but use this cautiously as it may downgrade some packages.

Frequently Asked Questions

Is MinGW the same as g++?

No. MinGW (Minimalist GNU for Windows) is a collection of tools that includes g++. g++ is specifically the C++ compiler component within the MinGW ecosystem.

Can I use g++ with Visual Studio?

Visual Studio primarily uses its own MSVC compiler. While you can configure VS to use external compilers like g++, it is not natively supported out-of-the-box. Most developers prefer using VS Code with MinGW for a lighter setup.

Do I need Administrator rights?

You need administrator rights to install MSYS2 and modify system environment variables. However, once installed, you can compile and run programs without admin privileges.

Why did my terminal close after updating?

This is normal behavior for MSYS2 during major updates. Simply relaunch the MSYS2 shell (e.g., "MSYS2 UCRT64") and continue with the installation steps.

Is UCRT64 better than MINGW64?

For Windows 11, UCRT64 is recommended because it links against the Universal C Runtime, which is built into modern Windows versions. This reduces the need for distributing extra DLLs with your applications.

How do I uninstall g++?

To remove g++, you can uninstall the entire MSYS2 installation by deleting the C:\msys64 folder and removing the associated entries from your Windows Environment Variables. Alternatively, use pacman -Rns mingw-w64-ucrt-x86_64-gcc within the MSYS2 shell to remove just the compiler.