Click here to Skip to main content
15,067,832 members
Articles / Programming Languages / C++
Technical Blog
Posted 30 Apr 2017

Tagged as


30 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
4 May 2017CPOL6 min read
Here is a quick start to use Visual Studio code for C++ programmers in Linux

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 quite 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 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.

Image 1

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.

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 provide 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 follows:



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)
    $( $< $(OUTPUT_OPTION)
    $( -MM -MP -MT $@ $< -o $(BUILDDIR)/$*.d

    @mkdir $(BUILDDIR)

$(BUILDDIR)/%.o:$(TESTDIR)/%.cpp | $(BUILDDIR)
    $( $< $(OUTPUT_OPTION)
    $( -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: cho 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.


Add a Comment Image 29


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shun Huang
Software Developer (Senior)
United States United States
My name is Shun. I am a software engineer and a Christian. I currently work at a startup company.
My Website:

Comments and Discussions

QuestionEverything was good until I installed CMake & CMake Tools Pin
Member 1391105513-Jul-18 15:05
MemberMember 1391105513-Jul-18 15:05 
QuestionNo CMake options Pin
Member 1382499713-May-18 7:59
MemberMember 1382499713-May-18 7:59 
QuestionGreat,,, Pin
Pritam Zope8-May-17 10:16
MemberPritam Zope8-May-17 10:16 
Generalit will be perfect... Pin
Southmountain2-May-17 17:14
MemberSouthmountain2-May-17 17:14 
GeneralRe: it will be perfect... Pin
Shun Huang2-May-17 18:22
professionalShun Huang2-May-17 18:22 
GeneralRe: it will be perfect... Pin
vre4-May-17 20:56
Membervre4-May-17 20:56 
PraiseExcellent! Pin
koothkeeper2-May-17 13:06
professionalkoothkeeper2-May-17 13:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.