Run c++ on windows using MinGW
To run a C++ program on Windows, you’ll need a C++ compiler. One commonly used compiler for Windows is MinGW (Minimalist GNU for Windows), which includes the GCC (GNU Compiler Collection) toolchain. Here’s a step-by-step guide to help you run a simple C++ program on Windows using MinGW:
Step 1: Install MinGW
Download MinGW:
- Visit the MinGW website: MinGW — Minimalist GNU for Windows.
- Look for the “Downloads” section and download the installer appropriate for your system (32-bit or 64-bit).
Run the Installer:
- Execute the installer and follow the installation instructions.
- During installation, you’ll be prompted to select components. Make sure to include the C++ compiler (usually named
mingw32-gcc-g++
).
Add MinGW to System Path (Optional but recommended):
- After installation, add the MinGW
bin
directory to your system's PATH environment variable. This step allows you to run the compiler from any command prompt. - For example, if MinGW is installed in
C:\MinGW
, addC:\MinGW\bin
to your PATH.
Save the file with a .cpp
extension, such as hello.cpp
.
Step 2: Write a Simple C++ Program
Create a simple C++ program using a text editor. For example, open Notepad and enter the following code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Step 3: Open Command Prompt
Open the Command Prompt or PowerShell on your Windows system.
Step 4: Navigate to the Directory
Navigate to the directory where you saved your C++ file using the cd
command. For example:
cd path\to\your\directory
Step 5: Compile and Run the Program
Compile the C++ program using the following command:
g++ hello.cpp -o hello.exe
This command uses the g++
compiler to compile your hello.cpp
file into an executable named hello.exe
.
Run the compiled program:
hello.exe
You should see the output “Hello, World!” printed on the screen.
That’s it! You’ve successfully compiled and run a simple C++ program on Windows using MinGW. Remember to adapt file paths and names as needed based on your specific setup.