Run c++ on windows using MinGW (2024)

Run c++ on windows using MinGW (1)

2 min read

·

Nov 22, 2023

--

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:

Download MinGW:

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, add C:\MinGW\bin to your PATH.

Save the file with a .cpp extension, such as hello.cpp.

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;
}

Open the Command Prompt or PowerShell on your Windows system.

Navigate to the directory where you saved your C++ file using the cd command. For example:

cd path\to\your\directory

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.

Run c++ on windows using MinGW (2024)

FAQs

Run c++ on windows using MinGW? ›

To open a command prompt window, press Windows+R to open the Run dialog. Enter cmd.exe in the Open textbox, then choose OK to run a command prompt window. In the command prompt window, right-click to paste the path to your app into the command prompt. Press Enter to run your app.

How to run C++ in Windows? ›

To open a command prompt window, press Windows+R to open the Run dialog. Enter cmd.exe in the Open textbox, then choose OK to run a command prompt window. In the command prompt window, right-click to paste the path to your app into the command prompt. Press Enter to run your app.

Can I run C++ program in GCC compiler? ›

GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc ). However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library.

Is MinGW required for Dev C++? ›

Dev-C++ internally uses MinGW (Minimalist GNU for Windows) Compiler. It can be used to code programs in C++ as well as C language. This article discusses how to download Dev-C++ and install it on your Windows computer.

How to install MinGW for C++ in Windows 11? ›

Installing MinGW (or clang)
  1. Go to this website and click either MinGW C/C++ For Windows 11 32/64 bit (for Windows 11) or MinGW C/C++ For Windows 10 32/64 bit (for Windows 10) ...
  2. Run the downloaded file ( mingw.exe ). ...
  3. Click 'Extract'. ...
  4. In Windows search, type 'env' and click on 'Edit the system environment variables':

How to run a C++ program from terminal? ›

To run the C++ program in your terminal, you need to make sure to change the directory to the one where your program resides, you can use the 'cd <File_Path>' command to change the path and to generate an executable file, the command, 'g++ File_Name. cpp -o FileName', is used.

What compiler to use for C++ Windows? ›

Use MSBuild with the Microsoft Visual C++ compiler or a 3rd party toolset like CMake with Clang or mingw to build and debug your code right in the IDE.

How to run a C++ file using MinGW? ›

Here's a step-by-step guide to help you run a simple C++ program on Windows using MinGW:
  1. Step 1: Install MinGW. Download MinGW: ...
  2. Step 2: Write a Simple C++ Program. Create a simple C++ program using a text editor. ...
  3. Step 3: Open Command Prompt. ...
  4. Step 4: Navigate to the Directory. ...
  5. Step 5: Compile and Run the Program.
Nov 22, 2023

Should I use GCC or G ++ for C++? ›

GCC and G++ are both used for compiling C and C++ languages. However, GCC is used for compiling C programs while G++ is used for compiling C++ programs. G++ automatically links files in the libraries of std C++ during object file linking, which does not happen with GCC.

How to use GCC compiler in Windows? ›

How to Setup GCC Compiler for Windows
  1. Step 1: Downloading MinGW-w64. ...
  2. Step 2: Downloading Zip File. ...
  3. Step 3: Extract and Install. ...
  4. Step 4: Click Install. ...
  5. Step 5: Installation Preferences. ...
  6. Step 6: Downloading MinGW Installation Manager. ...
  7. Step 7: Choose GNU Compilers. ...
  8. Step 8: Apply Changes.
Mar 26, 2024

What is the difference between MinGW and GCC compiler? ›

GCC (GNU Compiler Collection) is a free and open source compiler for C and C++ (and other languages like Objective-C, Fortran, D). MinGW-w64 is a free and open source C library for targetting Windows 32-bit and 64-bit platforms. The combination of these results in a free C/C++ compiler for Windows.

Does MinGW work on Windows? ›

vcpkg includes x64, x86, arm64 and arm community triplets for Mingw-w64. They don't depend on Visual Studio and can be used natively on Windows as well as for cross-compiling on other operating systems.

Is MinGW a compiler or IDE? ›

MinGW is a compiler system based on the GNU GCC and Binutils projects that compiles and links code to be run on Win32 (Windows) systems.

Is MinGW necessary for VS code? ›

C and C++ have compiled languages. So we need to install the compilers to run the code. A compiler is a software used to translate your computer-written code into machine code. To run C and C++ we need to install the MinGW compiler.

How to use MinGW to compile C code? ›

  1. Press the Windows key and type environment . Click Edit the system environment variables in the search results.
  2. Click the Environment Variables… button.
  3. Select the "Path" option under "System variables" and click Edit…
  4. Click New.
  5. Type C:\MinGW\bin and click OK.
  6. Click OK and OK again.

Is MinGW compiler safe? ›

1 Answer. There shouldn't be a risk, though installing anything (or even just turning the computer on) does involve some chance that a problem could develop.

How to start a C++ program? ›

Begin your 1st C++ Program
  1. Open any text editor or IDE and create a new file with any name with a .cpp extension. e.g. helloworld.cpp.
  2. Open the file and enter the below code in the C++ Compiler: #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; } ...
  3. Compile and run the code.
May 21, 2024

How to run C in CMD? ›

How to Compile and Run C Program in Command Prompt?
  1. STEP 1: Check for Compiler. Run the command 'gcc -v' to check if you have a compiler installed. ...
  2. STEP 2: Create the Program. Create a c program and store it in your system. ...
  3. STEP 3: Change the Directory. ...
  4. STEP 4: Compile the program. ...
  5. STEP 5: Run the Program.
Jul 23, 2024

How to run C++ code in VS Code terminal? ›

Make sure you have a C++ compiler installed before attempting to run and debug helloworld.cpp in VS Code.
  1. Open helloworld.cpp so that it is the active file.
  2. Press the play button in the top right corner of the editor.
  3. Choose g++ build and debug active file from the list of detected compilers on your system.

How to compile C++ to exe? ›

Compile the CPP file to EXE in VS Code (optional).
  1. Reopen your CPP file in VS Code.
  2. Click the Play button at the top-right and select Run C/C++ File.
  3. From the list of compilers, select C/C++: g++.exe build and debug active file. ...
  4. Once selected, your program will be compiled, and you'll see the output in the terminal.
Jun 14, 2024

Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6494

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.