How to Setup and Run C++ Code on Windows PC

Bryan Amirul Husna
4 min readJul 26, 2021

--

Hi, I am going to give some detailed information on how to setup, compile, and run C++ code on your Windows computer. I hope this will solve your confusion because I was confused too when first time setting up. Step 1–3 show you how to configure MinGW. If you have MinGW installed and configured, you can freely skip to step 4, although maybe you can get some new information by reading step 1–3.

1. Download MinGW Built Setup

First, you need to download MinGW. MinGW is a free C and C++ compiler. What it does is basically converting your C++ code into an executable file(.exe file). You can download MinGW installer here: https://sourceforge.net/projects/mingw-w64/.

2. Install MinGW

Double click on the installer. For the installation configuration:

  • Version: Choose the latest version
  • Architecture: x86_64
  • Threads: win32
  • Exception: seh
  • Build revision: Choose the highest

Then press Next

MinGW Installer Configuration

Then, choose folder where you want to install it. You need to remember it because it is needed later. Then just press Next, and needed packages download process will begin automatically. Wait until it finished, and then press Next and Finish.

  • Note: If you change the installation location, it will not create a new folder automatically. So you need to create a new folder manually to avoid mess.

3. Configure Environment Variables Path

At this point, you can’t use MinGW yet. What configuring Path does is basically integrating the MinGW program and its command into Command Prompt, so that you can use it. To do it, search Environment Variables in the Windows Search Bar. Choose environment variables that set for “system”, not “your account”. Then System Properties tab will appear, choose “Environment Variables …” menu that located near bottom.

Search System Environment Variables

Then, locate Path variable in System Variable (not User Variable). Select the Path variable, and press “Edit…”.

Click “New” and then “Browse…”. A folder chooser will appear, navigate to your MinGW installation folder, expand “mingw64” folder, choose “bin” folder, and press OK. Then press OK to all of the Environment Variable tab to save and apply your change.

Now you are ready to compile and run your C++ code.

Note: Close and start again any Command Prompt (CMD)/Terminal/IDE that is still running. You need to do it so that the change is applied to them.

Adding MinGW Path

4. Write the C++ Code

To write C++ code, you can put your code in Notepad and save it with .cpp extension (add .cpp at the end of file name when saving, e.g. hello.cpp). Here I provide a simple C++ Hello World code for you to test your setup.

#include <iostream>
using namespace std;
int main(){
cout << "Hello World";
return 0;
}

5. Compile and Run the Code

To compile the code, open Command Prompt or CMD (you can easily find it using the Windows Search Bar). In CMD, navigate to folder where you save the .cpp file. You can do it by using the commands below. Just change the path to your folder location, in my case the .cpp code is located in Bryan Code folder in D: drive.

To do the command, write one line, press enter, and then write the next line and enter again.

D:
cd D:/Bryan Code/

Pro tips: Quickly open CMD and navigate to your folder by typing cmd in your folder

Then, to compile your C++ code, run this command on CMD.

g++ hello.cpp -o hello

Explanation:

  • g++ : it is command from MinGW to compile C++ code
  • hello.cpp : your C++ source code, change it with your file name
  • -o : option to rename the generated .exe file
  • hello: output file name, the code will be compiled into like hello.exe

After the compilation process, you will notice a new .exe file has been created. If error occured, like “g++ is not recognized …”, your Path configuration may be not correct. Read again step number 3.

If no error occured, you can start the program. To start the program, run this command in CMD, and Hello World message will appear on your screen.

.\hello

Note: Yes, it is backslash (\), not ordinary slash (/). Your program might not run if you use slash (/).

Compile and Run C++ Code

What’s Next?

Congratulation, you have compiled and run your first C++ code on your Windows computer.

Using Notepad and CMD might be enough for creating small program. However, programmers are very often forget the keywords of C++ code and need something to remind them, like autocompletion. Programmers also very often create some mistakes and need something to show them what’s wrong.

That’s why we need Integrated Development Tool or Code Editor. You can use VS Code, a free and quite powerful code editor to write C++ code in a better environment than Notepad and CMD. Check out my post about how to write and run code C++ code in VS Code.

--

--

Bryan Amirul Husna
0 Followers

Informatics/Computer Science Student at Bandung Institute of Technology