Setting up Visual Studio Code (VS Code) with C++ on Ubuntu (Linux)

Amritya Singh
Floppy Disk F
Published in
4 min readMay 9, 2020

When it comes to getting things done in the tech world nothing beats Linux. It’s open source, highly secure, conveniently customizable and it comes with a huge community support. Combine that with the power of Visual Studio Code (VS Code): One of the most modern and popular code-editors; and you get the perfect fusion of adaptability and efficacy.

Most of the beginners who venture out in the amazing (and sometimes frustrating) world of programming usually start with C++. The reason for which is quite simple, C++ is an Object Oriented Programming (OOP) language, learning which helps in building a firm foundation for coding. Also, it’s quite fast and thus gives you an edge if you’re into Competitive Programming.

Now let’s start with setting things up.

Step 1: Installing VS Code

Assuming you already have Ubuntu (or any debian-based OS), the first thing required would be to install VS Code. The easiest way to install it would be through Snap Package management. Just run the following commands in the terminal:

sudo apt install snapd
sudo snap install vscode --classic

Step 2: Installing GCC/G++

First update the packages list:

sudo apt update

Install the build-essential package by running:

sudo apt install build-essential 

The above command installs a bunch of new packages including gcc, g++ and make.

To validate the successful installation, run:

gcc --version 

Step 3: Configuring C++

Now open VS Code and you will see the default window page:

Default view

Now go to the Extensions tab (the bottom-most tab on the left side panel) and install these three extensions — C/C++ by Microsoft, C/C++Compile Run by danielpinto8zz6 & Code Runner by Jun Han.

Extensions tab VS Code

Now go to Files -> Preferences -> Settings. Search “code.runner: Run in terminal” and tick the box. This will enable you to run your code & display its output in the terminal and you’ll be able to provide inputs if necessary.

It’s a common custom to name your Main C++ as main

Now go the Explorer tab (the top most tab on the left side panel). Click on Open Folder and create a new folder (or browse to an existing one) where you’ll store your C++ project.

In my case I created a folder named C++. After creating the folder you’ll be able to see it in the editor section. Now we’ll create a new file by clicking on the New File icon (right next to the folder name).

I have named my file as main.cpp. It’s a common custom to name your Main C++ file as main (cpp being the extension) because during the compilation the main file is sought first. You can name it whatever you like though.

We’ll write some code in the cpp file. We’ll go with conventional code that almost every beginner starts with — A code that displays Hello World.

#include<iostream>

using namespace std;

int main() {

cout<<”Hello World \n”;

}

Now go to Terminal -> Configure Default Build Task. Select shell gcc build active file (although you can choose any of the build active file).

c++

This will open a tasks.json file. Something like this. There’s nothing to be afraid of. We are going to keep this file as it is. No changes at all.

Now go to Terminal -> Run Build Task (Ctrl+Shift+B). After that you can close the tasks.json tab from the top bar (It would still appear on the left section of the code editor though, but that’s not a problem)

It’s time to run our code. On the top right corner you will find the run tab.

Click on the run tab and we’ll be ready to go.

As you can see our code ran successfully and there’s the output on the terminal. Here’s to your first C++ code! Cheers!

Now you will be able to create more C++ following the same procedures. Happy Learning!

--

--