How to Get Started with C++ on Windows

RedXIII | May 28, 2021, 11:18 a.m.

If you’re interested in learning C++, you’re not alone. Originally developed in Bell Laboratories in 1979 by Bjarne Stoustrup, C++ has become one of the most popular computer programming languages in the world. 

The reason for C++’s continued popularity over the years is multifold. For starters, it’s a flexible language that incorporates multiple programming paradigms. Built on top of C’s procedural approach, C++ utilizes object-oriented programming and gives developers the options of using bottom-up design..

C++ also offers excellent performance. It’s perfect for complex problems that require an efficient use of hardware. For tasks where performance is critical, C++ is an obvious choice.

Lastly, there’s industry demand for C++ programmers. A language so popular is bound to need developers for years to come. Choosing a Text Editor

Before you can write any C++ code, you’ll need something to write it with. Fortunately, you’ll find no shortage of choices in this department. 


Some common choices include Notepad++, Atom Text Editor, Visual Studio Code, or Sublime Text. There are others as well. What you choose is a personal preference. If you’re not sure which to go with, try a few and see which stands out as your favorite.


After the Text Editor is installed on your computer, you’ll be able to write all sorts of code, not just C++. The difference between the different editors is mostly in design.


Once you’ve decided on a Text Editor, you’ll want to familiarize yourself with its basic features.. Afterall, this will be your main tool for writing C++ code.

Download and Install MinGW

Now we can write C++ code, but the computer has no way to read it. We need a program that can translate what we write into something the computer can read. This tool is known as a compiler.


There are many compilers to choose from. For the sake of ease, we’re going to recommend MinGW. It’s an effective and easy to install C++ compiler. Download it here.


Run the installation file and follow the instructions, choosing the default settings along the way. Pay attention to where MinGW installs on your machine, you’ll need to know this in the next part. 


After the installation completes, you’re ready to set up Windows for C++ development.

Setting Up the Environment

Once MinGW is installed, we’ll need to set up the environment variables for Windows so that we can run the compiler from the command line.


This section can be a little tricky if you’ve never done it before. Take your time and make sure you complete each step.


Setting up the Environment Variables:

  1. Search for “environment variables” in the Windows toolbar and select “edit the system environment variables.”

  2. In the System Properties panel that appears, click the Environment Variables button.

  3. Select the Path variable and click Edit.

  4. Add a new line with the address to the location of the g++ executable. This path will depend on where MinGW installed. For example, my install is at: C:\MinGW\bin

Close the System Properties and panels and restart any instances of the command prompt that may be running. You need to do this for the changes to take effect.

Testing the Installation

Open a new command prompt. You can do this on Windows by searching for “cmd.” When the command prompt opens, test that MinGW was installed correctly by typing the following:


g++ --version


You should see something like this if everything is working properly.

g++ (MinGW.org GCC Build-2) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.


Writing Your first C++ Program

With MinGW installed, it’s time to write our first line of C++ code. The tradition is to write a “Hello World” app. Here it is in C++:

hello.cpp

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

Save this file on your computer with the name hello.cpp. Where you save this file matters, so keep that in mind. We’ll need to navigate to it using the command line. 


If you’re not familiar with using the Windows command prompt, you’ll need to take a moment to learn the basics. Using some simple commands, we can navigate the Windows file system. 


Windows Command Line Basics


cd Used to change directory
Example: cd Desktop


dir Prints the contents of the current directory


cd .. Moves up one directory.


Running the .cpp File


Navigate to the C++ program file using the Windows Command Prompt. Do this by changing directories until you’ve found the folder containing the file.


Once the file is located, you can compile it with g++. Here’s an example.


C:\Users\jpett\Desktop\Code Tutorials\C++\HelloWorld>g++ hello.cpp

If the program doesn’t contain any errors, g++ will create an executable file named a.exe. You can run this file in the command prompt by calling the filename.


a.exe

You should see a message appear after running the executable file.


Hello World!


Summary

Getting started on Windows can be a hurdle when you’re just getting started. Learning C++ is an exciting and ambitious opportunity. If you’ve followed our step-by-step guide, you should have everything you need to code with C++.


Now that you have a Text Editor and MinGW set up, you’re ready to take the next step and delve into the wonderful world of C++ programming.

Troubleshooting

Did you run into trouble somewhere? It’s okay. It happens to everyone. Here’s some help if you get stuck.

  1. I have MinGW installed, but I get an error when I run g++ from the command prompt.
    You probably need to set up the environment variable again. Make sure you have the correct path. It needs to include the bin directory inside the MinGW install directory. For example: C:\MinGW\bin
  2. I have MinGW installed, but I’m getting an error when I try to compile with g++.
    You might have made an error copying the C++ program and now it won’t compile. Try copying the text exactly, or copy and paste it into your Text Editor.

Related Posts

If you’re ready to learn more about computer programming, we’re here to accommodate you. Check out the links below if you’re ready to take your skills further.


About Us

Learning at the speed of light.

We created Start Prism to help students learn programming. You can find exercises and recent tutorials below.

Topics Quizzes Tutorials

0 comments

Leave a comment