Visual Studio Code (VS Code) is a free, lightweight and highly customizable code editor that we can use for C++ development.
VSCode does not include compilers by itself (it is only a text editor). To work with C++, we will need a compiler like GCC (g++) and the GDB debugger.
- GCC (GNU Compiler Collection) is a set of compilers developed by the GNU project, which includes compilers for several programming languages, including C++.
- G++ is specifically the GCC compiler for C++
- GDB is the GNU debugger. It allows us to stop execution at specific points, examine variables, and follow the execution line by line to identify errors.
Installing Visual Studio Code
To get started, we need to have Visual Studio Code installed on our computer. If you don’t have it yet, it’s very simple:
- Download Visual Studio Code: Go to the official Visual Studio Code page at https://code.visualstudio.com/ and download the version corresponding to your operating system (Windows, macOS, or Linux).
- Install VS Code: Follow the installation instructions for your system. Once completed, open VS Code.
Additionally, we will need to install the VSCode extension for C++.
Installing the C++ Compiler
Visual Studio Code does not include a compiler, so we need to install one. On Windows systems, the most common option is MinGW, while on macOS and Linux, you can use the GCC compiler.
MinGW (Minimalist GNU for Windows) is a set of tools that provides a Unix-like experience on Windows. It includes GCC and other essential utilities for C++ programming.
To download MinGW, go to the official MinGW website MSYS2. Follow the installation instructions. During the process, make sure to select the C++ compiler (g++
).
A console window will open (a bit crude). From here we can install other tools, such as MinGW for x64.
For example, we can run,
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Now we need to set the PATH variable so that Windows can find the MinGW tools.
- Open the Control Panel and go to System > Advanced system settings.
- Click on Environment Variables, select the
Path
variable, and add the path to thebin
directory of MinGW (for example,C:\mingw-w64\bin
).
Normally, you should already have GCC and G++ installed on your distribution. You can check this with
gcc -v
If it is correctly installed, you will see something like this.
gcc version 9.4.0 (Ubuntu 9.4.0-lubuntul~20.04.2)
If it is not installed, you can install it by running the following command from the terminal. For example, in a distribution like Ubuntu, you would do the following,
sudo apt-get update
sudo apt-get install build-essential gdb