Language: EN

como-instalar-y-configurar-visual-studio-code-cpp

How to use Visual Studio Code and GCC with C++

Visual Studio Code (VS Code) is a free, lightweight, and highly customizable code editor that we can use to develop in C++.

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 various 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 execution line by line to identify errors.

Installing Visual Studio Code

To get started, we need to have Visual Studio Code installed on the computer. If you don’t have it yet, it’s very simple:

  1. 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).
  2. 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++.

vscode-cpp-extension

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, we 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 programming in C++.

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 will open (a bit rudimentary). From here, we can install other tools, such as MinGW for x64.

install-mingw

For example, we 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 the bin directory of MinGW (for example, C:\mingw-w64\bin).

vscode-cpp-variables-entorno

Usually, you should already have GCC and G++ installed on your distribution. You can check it 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 you do not have it installed, you can install it by running the following from the terminal. For example, on a distribution like Ubuntu, we would do the following,

sudo apt-get update
sudo apt-get install build-essential gdb