Quick Start to Use Visual Studio Code for C++ Programmers in Linux

[Updated: May 04, 2017]

As a software engineer, a great editor or IDE helps improve productivity. I have used many IDEs and editors to write the code for many years such as Visual Studio, VIM, Eclipse, NetBeans, Notepad++. In Windows environment, Visual Studio is always my favorite tool. However, I was still looking for a good tool to code in Linux environment. Now I finally found one, Visual Studio Code, released by Microsoft. Visual Studio Code is a lightweight and cross-platform version of Visual Studio. It supports several programming languages and many features to help programmers.

Microsoft has posted a lot of fantastic documents of Visual Studio Code. However, most of these documents are written for the general purpose of Visual Studio Code. It took me a lot of time to figure out how to configure Visual Studio Code to work with C++ in Linux. Therefore, the purpose of this article is to help C++ programmers, like me, who want to use Visual Studio Code for C++ programming in Linux. This Quick Start aims to provide step by step guideline for C++ programmers who want to spend as little effort as possible to use Visual Studio Code in Linux environment. The Quick Start includes use Visual Studio Code to build C++ code with CMake and Make, and use Visual Studio Code to debug C++ code in real time.

This Quick Start uses a simple C++ example to demonstrate how to configure Visual Studio Code.

The Quick Start is based on the following environment:

Installation of Visual Studio Code

First, go to https://code.visualstudio.com/Download. Several options are available. Second, choose an appropriate Visual Studio Code package to download. Visual Studio Code provides .deb for Debian and Ubuntu, and .rpm for Red Hat, Fedora, and CentOS. Then, install the downloaded package based on Linux distribution.

  • Debian and Ubuntu

In Debian and Ubuntu, use dpkg command to install Visual Studio Code.

sudo dpkg -i code_1.11.2-1492070517_amd64.deb

This command installs Visual Studio Code into /usr/share/code.

  • Red Hat, Fedora, and CentOS

In Red Hat, Fedora, and CentOS, use rpm command to install Visual Studio Code.

sudo rpm -Uvh code-1.11.2-1492070635.el7.x86_64.rpm

Same as dpkg, Visual Studio Code is installed in /usr/share/code.

  • Standalone

The standalone binary release is also available.


Download the standalone Visual Studio Code, untar the package, and then, start Visual Studio Code:

tar xvzf code-stable-code_1.11.2-1492070517_amd64.tar.gz
cd VSCode-linux-x64

A Simple C++ Example

The sample code includes a QuickStart library which provides a very simple function, add, and a unit test which leverages the power of Boost Unit Test Framework.

Source code can be found at https://github.com/shunsvinyard/blog-post/tree/master/quick-start-to-use-visual-studio-code-for-cpp-programmers-in-linux

Sample Code

The sample code has following hierarchy.



class QuickStart
    int add(int, int);


#include "QuickStart.hpp"

int QuickStart::add(int a, int b)
    return (a + b);


#define BOOST_TEST_MODULE QuickStartTest

#include "QuickStart.hpp"

#include <boost/test/unit_test.hpp>


    QuickStart quickStart;

    BOOST_CHECK_EQUAL(quickStart.add(1, 1), 2);

Build with CMake

CMake is a very popular tool to build C++ code, which is designed to build, test, and package software. It supports directory hierarchy and uses compiler-independent approach. CMake is also much easier to use than Make. There are two CMake extensions available for Visual Studio Code: CMake and CMake Tools.

  • CMake provides the language service support for CMake.
  • CMake Tools gives the ability to build CMake targets.

To have the best CMake experience, install both extensions is recommended.

Assume there are CMakeLists.txt files in QuickStart Project, and the hierarchy is as follows.


Top level CMakeLists.txt

cmake_minimum_required (VERSION 3.5)

set(SOURCES ${PROJECT_SOURCE_DIR}/src/QuickStart.cpp)

add_library(QuickStart STATIC ${SOURCES})



find_package(Boost REQUIRED COMPONENTS unit_test_framework)

set(TEST_SOURCES ${PROJECT_SOURCE_DIR}/test/QuickStartTest.cpp)
set(TEST_LIBS QuickStart)

add_executable(test_main ${TEST_SOURCES})
target_link_libraries(test_main ${TEST_LIBS} ${Boost_LIBRARIES})
add_test(QuickStartTest test_main COMMAND test_main)

First, type Ctrl+Shift+P to enable ‘’Command Palette’’ and type cmake, Command Palette displays a list of available commands that we can execute from here. In this case, it shows all commands related to cmake.


Choose CMake:Configure for the first time. A new list bumps up; the list has all available configurations.


Second, after we choose one configuration (e.g. Debug), CMake Tools automatically setup the configurations for us.


Now, Visual Studio Code is ready to use. Type Ctrl+Shift+P to enter Command Palette and type cmake, and choose CMake:Build.


Visual Studio Code starts building the code.


Choose CMake: Clean.


Visual Studio Code cleans the build folder and built files.


Use CMake: Run tests to run unit tests



In addition to using Command Palette, CMake Tools provides a quick and convenient way to trigger most common actions via Side Bar.


It shows all the available build targets to change.


Other Configurations

All available CMake configurations can be found at setting.json. To enable setting.json, from the menu bar do [File->Preference->Settings]. Visual Studio Code has the ability for users to modify nearly every part of Visual Studio Code. In setting.json, there is a section called CMake Tools configuration which has many options for users to modify.


Modification of these options is straightforward. One option worth mentioning is ‘’cmake.preferredGenertors.’’

Ninja Build

’Ninja is a small build system with a focus on speed.’’ CMake Tools provides several code generators. Ninja is the default option. When a project gets bigger, the longer time to build the project. Ninja build system performs faster on build than GNU Make. Using Ninja to build the code is highly recommended, i.e. the default option of ‘’cmake.preferredGenertors.’’


One important reason to use IDE is its debugging support. Visual Studio Code has great debugging support.

To start debugging, click the debug icon in the Activity Bar.


For the very first time, we need to add a configuration.


Then, choose the debugger, C++ (GDB/LLDB), for Linux environment.


Visual Studio Code generates a configuration file, launch.json, for debugging. This configuration file provides variety options to modify. The most important option is to tell Visual Studio Code which binary to debug. In this example, we debug the unit test binary, test_main, which is located at ${workspaceRoot}/build/test/.


Now, Visual Studio Code is ready to debug.



Build with Make

Although CMake is easier to use than GNU Make, GNU Make is used by many people. This section demonstrates how to configure Visual Studio Code to build the code with GNU Make.

Assume there is a Makefile in QuickStart Project, and the hierarchy is as following.



BUILDDIR = build
INCDIRS = include
SRCDIR = src
TESTDIR = test

OBJS = $(addprefix $(BUILDDIR)/, $(patsubst %.cpp, %.o, $(notdir $(wildcard $(SRCDIR)/*.cpp))))
TESTOBJS = $(addprefix $(BUILDDIR)/, $(patsubst %.cpp, %.o, $(notdir $(wildcard $(TESTDIR)/*.cpp))))

TEST_FNAME = $(BUILDDIR)/test_main

CXXFLAGS += $(INCDIRS:%=-I%) -std=c++1y
LDFLAGS += $(LIBQUICKSTART_FNAME) -lboost_unit_test_framework

.PHONY: all clean


    $(AR) $(ARFLAGS) $@ $^

    $(CXX) -o $(TEST_FNAME) $^ $(LDFLAGS)

$(BUILDDIR)/%.o:$(SRCDIR)/%.cpp | $(BUILDDIR)
    $(COMPILE.cc) -MM -MP -MT $@ $< -o $(BUILDDIR)/$*.d

    @mkdir $(BUILDDIR)

$(BUILDDIR)/%.o:$(TESTDIR)/%.cpp | $(BUILDDIR)
    $(COMPILE.cc) -MM -MP -MT $@ $< -o $(BUILDDIR)/$*.d



Visual Studio Code provides a feature, Tasks, to integrate with external tools, including Make.

First, type Ctrl+Shift+P to enable ‘’Command Palette’’ and type tasks, Command Palette displays a list of available commands that we can execute from here. In this case, it shows all commands related to Tasks.


In the very first time, choose Tasks: Configure Task Runner. A new list bumps up; the list has all available configurations.


Second, to use Make, choose Others, and then Visual Studio Code generates a configuration file, tasks.json. This file is where users can set up Tasks with external tools.


Third, change “command”: “echo” to ‘’command’’: “make.” In the Makefile of Quick Start example, there are three build targets: all, run_test, and clean, so add tasks, all, clean, and run_test, as below.


Now, Visual Studio Code is ready to run these tasks that we just defined. Type Ctrl+Shift+P to enable ‘’Command Palette’’ and type tasks, it shows all the available Tasks options. Choose Tasks: Run Task.


Now, it shows all three tasks that we just added: all, clean, and run_test.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s