Visual Studio Code Setup for Beginners using C++ and CMake

Sam Romano
Oct 17, 2018 · 6 min read

My new job requires that I learn C++, and it’s been a long time since I’ve done any sort of programming. What I’ve decided to do is take what I learn and write it down to cement it in memory — almost like journaling, but to also help anyone else is in the same situation because why not?

The article I used to figure this out was here on Medium as well, however I ran into issues because of VSCode updates (different versions), but this still is a good article if you’d like to check it here because I still want to give credit where credit is due. Otherwise, I’ll do my best to link my references throughout the document.

Installation

First download VSCode which can be found here. I’m using macOS High Sierra, but if you’re using Windows these instructions for the most part will still work.

My version of VSCode is 1.27.2, if you’re curious about how to find yours once installed, simply click on Code →About Visual Studio Code as shown below.

Figure 1 Visual Instructions to Locate Version of Visual Studio Code.

CMake and Compiler

VSCode is a lightweight editor so you will need to make sure you have some sort of compiler installed. If you have a Mac and XCode is installed you could use Clang. However, the beauty of VSCode is being able to download extensions. It’s kind of like downloading an app on your phone, search for what you need, download, and go!

  1. Open VSCode

2. Press Shift-Command-X (⇧⌘X) to open Extensions or press the square icon on the activity bar. (Should be under the Debug symbol).

Figure 2 Visual of what the Activity Bar is — https://code.visualstudio.com/docs/getstarted/userinterface

3. Search and download CMake.

Figure 3 CMake Extension by twxs. Provides support for build.

The purpose of CMake, in simple terms, is to help manage the build process and provide cross-platform build files so that you can build your project on your specific platform. A good metaphor may be that it’s like wedding planner. He/She may will procure goods and services from a variety of vendors from all different locations, but will be sure to incorporate these goods and services to design a specific wedding. If you can think of a better one, let me know!

4. Download the extension C/C++ IntelliSense.

Figure 4 Screenshot of C/C++ IntelliSense Extension.

“Go beyond syntax highlighting and autocomplete with IntelliSense, which provides smart completions based on variable types, function definitions, and imported modules.” — Some Dude Who Wrote It On the Website

First Program

After everything has been downloaded, feel free to restart Visual Studio Code. This may not be necessary but for good measure let’s do it.

Everyone and their mother has done Hello World, so let’s jazz things up and just multiply two numbers via user input and output the result.

Every Program Needs

  1. *Insert Awesome Program Name*.cpp
  2. tasks.json file
  3. CMakeLists.txt
  4. Build folder

On your desktop, documents, 1990 Taxes, or wherever you can keep track of your program let’s create a folder called Multiply.

Workspace

There isn’t good documentation on what a workspace is, at least as of now, from the VSCode website. I think of it as a way to organize all the files within a project. However, to make a new workspace (because doing File →new workspace is apparently soooo difficult), you create a folder, and select add new workspace under File and select your new folder, Multiply.

Select File → Save Workspace As → Multiply.code-workspace

Figure 5 Create a Workspace

Now here’s your choice, you can either add a folder or just immediately add the files. I like to add folders just incase I choose to have multiple programs and for me the folders help organize it.

You will need to add the files:

※ MultiplyTest.cpp

※ CMakeLists.txt

CMakeLists.txt

Copy & paste or type out the following code into your CMakeLists.txt file and save!

MultiplyTest.cpp

This is my quick version, but feel free to make any alterations. No one will yell at you I promise. You can even change it to Hello World, I won’t be offended.

Command Palette / Task Runner

This is where me and the other article differed a lot. I’m not sure why but certain situations didn’t arise and I couldn’t quite follow his steps. If you run into similar situation as I did, check out his article (linked in the beginning) and well, honestly, Google it. I ain’t a wizard.

  1. Type Shift+Command + P (⇧+ ⌘ + P) or Select View →Command Palette
  2. A search bar will appear with greater than symbol in the bar “>”, type Tasks: Configure Task and hit return/enter.
Figure 6 Command Palette for Tasks Config

3. Select Create tasks.json file from template

Figure 7 Command Palette tasks.json creation

4. Select Others

Figure 8 tasks.json template type, select Other

This will create a tasks.json file that your program needs to be able to configure the build properly.

This is where it got different for me compared to the other article, but when I altered the tasks.json file to look like the code below my programs would finally build. A lot of the nomenclature changed during the version so it was a little bit difficult to figure out. Here’s my source along with the VSC terminology definition below the code snippet.

Figure 9 Nomenclature of tasks.json; Source: https://code.visualstudio.com/docs/editor/tasks

Build

Your sidebar should look like this at this point:

Figure 10 List of Files in User Sidebar / Explorer at this Point in Guide .
  1. Open a terminal and make sure you’re in your Multiply folder.

To change to a folder:

Go up a folder

List All the Folders in a Directory

2. Create a build folder and move to that folder

3. Now we use CMake

Output of CMake

4. Now we use make

Figure 11 Output of Make

Run

Last command:

Figure 12 Final Output of Program

Done!

Summary

At the end of the day I used the terminal and basic bash commands to run my program. I understand there will be future versions and different scenarios for others. I’m hoping this helps in some way for someone out there. Comment if you think there are sections I can improve. Thanks!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade