0
0
CppHow-ToBeginner · 4 min read

How to Install g++ on Windows for C++ Development

To install g++ on Windows, download and install the MinGW-w64 toolchain which includes the g++ compiler. After installation, add the MinGW-w64 bin folder to your system PATH environment variable to use g++ from the command line.
📐

Syntax

The basic command to compile a C++ file using g++ is:

  • g++ [options] source_file.cpp -o output_executable

Explanation:

  • g++: The C++ compiler command.
  • [options]: Optional flags like -Wall for warnings.
  • source_file.cpp: Your C++ source code file.
  • -o output_executable: Sets the name of the compiled program.
bash
g++ [options] source_file.cpp -o output_executable
💻

Example

This example shows how to compile and run a simple C++ program using g++ on Windows after installation.

cpp
#include <iostream>

int main() {
    std::cout << "Hello, g++ on Windows!" << std::endl;
    return 0;
}
Output
Hello, g++ on Windows!
⚠️

Common Pitfalls

Common mistakes when installing or using g++ on Windows include:

  • Not adding the MinGW-w64 bin folder to the system PATH, causing g++ to be unrecognized.
  • Downloading an incomplete or wrong MinGW version.
  • Confusing MinGW with other compilers like Visual Studio's cl.exe.
  • Not opening a new command prompt after setting PATH.
bash
REM Wrong: Trying to run g++ without PATH set
> g++ --version
'g++' is not recognized as an internal or external command

REM Right: After adding to PATH
> g++ --version
g++ (MinGW-W64) 12.2.0
Output
'g++' is not recognized as an internal or external command g++ (MinGW-W64) 12.2.0
📊

Quick Reference

StepDescription
Download MinGW-w64Go to https://mingw-w64.org and download the latest installer.
Run InstallerChoose architecture (x86_64), threads (posix), and exception (seh).
Add to PATHAdd the MinGW-w64 bin folder to your system environment PATH.
Verify InstallationOpen cmd and run g++ --version to check.
Compile CodeUse g++ yourfile.cpp -o yourprogram to compile.

Key Takeaways

Install MinGW-w64 to get the g++ compiler on Windows.
Add the MinGW-w64 bin folder to your system PATH to use g++ from the command line.
Use the command g++ source.cpp -o output to compile C++ programs.
Verify installation by running g++ --version in a new command prompt.
Avoid common mistakes like missing PATH setup or wrong MinGW versions.