0
0
CppHow-ToBeginner · 4 min read

How to Install C++ Compiler on Windows: Step-by-Step Guide

To install a C++ compiler on Windows, you can download and install MinGW or the Visual Studio Build Tools. After installation, add the compiler's bin folder to your system PATH environment variable to use it from the command line.
📐

Syntax

After installing a C++ compiler, you use the command line to compile your C++ files. The basic syntax to compile a file named program.cpp is:

  • g++ program.cpp -o program.exe for MinGW (GCC compiler)
  • cl program.cpp for Visual Studio Build Tools (MSVC compiler)

Here, g++ or cl is the compiler command, program.cpp is your source file, and -o program.exe sets the output executable name.

bash
g++ program.cpp -o program.exe
💻

Example

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

cpp
#include <iostream>

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

Common Pitfalls

Common mistakes when installing or using a C++ compiler on Windows include:

  • Not adding the compiler's bin folder to the system PATH, so commands like g++ or cl are not recognized.
  • Downloading incomplete or wrong versions of MinGW or Visual Studio Build Tools.
  • Confusing 32-bit and 64-bit versions causing compatibility issues.
  • Trying to compile without saving the source file with a .cpp extension.

Example of a wrong command and the right command:

Wrong: g++ program
Right: g++ program.cpp -o program.exe
bash
g++ program
// Error: g++: error: program: No such file or directory

// Correct usage:
g++ program.cpp -o program.exe
📊

Quick Reference

StepAction
1Download MinGW from https://sourceforge.net/projects/mingw/ or Visual Studio Build Tools from https://visualstudio.microsoft.com/downloads/
2Run the installer and select C++ compiler components
3Add the compiler's bin folder (e.g., C:\MinGW\bin) to the system PATH environment variable
4Open Command Prompt and verify installation by running 'g++ --version' or 'cl'
5Compile your C++ files using 'g++ file.cpp -o file.exe' or 'cl file.cpp'

Key Takeaways

Install MinGW or Visual Studio Build Tools to get a C++ compiler on Windows.
Add the compiler's bin directory to your system PATH to use it from the command line.
Use 'g++ file.cpp -o file.exe' for MinGW or 'cl file.cpp' for Visual Studio to compile programs.
Always save your source files with a .cpp extension before compiling.
Verify installation by checking the compiler version in the command prompt.