December 3, 2024

How to Install C++ Compiler and Use it in VS Code in Windows

In this tutorial, we explain how to install a version of GNU Compiler Collection (GCC) in Windows. GCC contains a powerful and easy-to-use C++ compiler. GCC is free and open source. We also explain how to set it up in VS Code (Visual Studio Code) and how to compile C++ programs in VS Code. Besides installing a version of GCC compiler, it is also possible to install and use some other compilers in VS Code, such as for example Microsoft Visual C++ compiler or similar Microsoft Compilers. You should actually try to compile your code using several different (at least two) compilers.

In particular, in this tutorial, we explain how to install a version (port) of GCC for Windows called MinGW. MinGW stands for Minimalist GNU for Windows. It includes a port of GNU Compiler Collection (GCC). Consequently, it is a free and open-source C++ build system.

This tutorial is very important for other tutorials on this channel covering C++, robot operating system, machine learning, and control engineering topics.

The YouTube video tutorial is given below.

The procedure is

  1. Download and install a version of MinGW either from this website: https://nuwen.net/ Download the executable file that is a self-extracting archive. Unpack the downloaded zip archive and copy a complete folder to a new destination. For example, the new folder can be named like this after copying “C:\MinGW
  2. Add the folder: “C:\MinGW\bin” to the Windows path. To do that in Windows, click Start->”Edit the System Environment Variables”. Then, click on “Advanced”, then click on “Environment Variables”. Then click on “Path”, then “edit”, then “new”, and insert the path to the folder. That is, you should insert the complete path “C:\MinGW\bin”. Then OK to close “Edit Environment variable” window, then two twice more OK to close the parent window.
  3. Test the compiler from the command line. Open a Command Prompt and type
    g++ –version
    The output should be:

    g++ (GCC) 13.2.0
    Copyright (C) 2023 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


    This means that the compiler is properly installed.
  4. Download VS Code by visiting https://code.visualstudio.com/ and download the installer. Install the program.
  5. Then, install the C++ extension for VS code. Open VS Code, select Extensions
    (CTRL+SHIFT+X), search for C++, and select Install.
  6. Write a hello_world.cpp program, compile it, and execute it (see the video tutorial).