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.

vsc_download.png

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
./bin/code

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.

sample_code_hierarchy.png

include/QuickStart.hpp

class QuickStart
{
public:
    int add(int, int);
};

src/QuickStart.cpp

#include "QuickStart.hpp"

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

test/QuickStartTest.cpp

#define BOOST_TEST_MODULE QuickStartTest

#include "QuickStart.hpp"

#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(QuickStartSuite)

BOOST_AUTO_TEST_CASE(AdditionTest)
{
    QuickStart quickStart;

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

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.

cmake_hierarchy

Top level CMakeLists.txt

cmake_minimum_required (VERSION 3.5)
project(QuickStart)

set(QUICK_START_VERSION_MAJOR 1)
set(QUICK_START_VERSION_MINOR 0)
set(SOURCES ${PROJECT_SOURCE_DIR}/src/QuickStart.cpp)

include_directories("${PROJECT_SOURCE_DIR}/include")
add_library(QuickStart STATIC ${SOURCES})
enable_testing()

add_subdirectory(test)

test/CMakeLists.txt

find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories("${PROJECT_SOURCE_DIR}/include")

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.

cmake_cp

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

cmake_configure.png

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

cmake_configure_debug.png

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

cmake_build_list.png

Visual Studio Code starts building the code.

cmake_build.png

Choose CMake: Clean.

cmake_clean.png

Visual Studio Code cleans the build folder and built files.

cmake_clean_output.png

Use CMake: Run tests to run unit tests

cmake_unit_test

cmake_unit_test_result

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

cmake_side_bar.png

It shows all the available build targets to change.

cmake_side_bar_all.png

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.

cmake_other_configurations.png

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

Debug

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.

cmake_debug_1.png

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

cmake_debug_2.png

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

cmake_debug_3.png

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

cmake_debug_4.png

Now, Visual Studio Code is ready to debug.

cmake_debug_5

cmake_debug_6

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.

make_hierarchy

Makefile

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))))

LIBQUICKSTART_FNAME = $(BUILDDIR)/libQuickStart.a
TEST_FNAME = $(BUILDDIR)/test_main

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

.PHONY: all clean

all: $(LIBQUICKSTART_FNAME) $(TEST_FNAME)

$(LIBQUICKSTART_FNAME): $(OBJS)
    $(AR) $(ARFLAGS) $@ $^

$(TEST_FNAME): $(TESTOBJS) $(LIBQUICKSTART_FNAME)
    $(CXX) -o $(TEST_FNAME) $^ $(LDFLAGS)

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

$(BUILDDIR):
    @mkdir $(BUILDDIR)

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

run_test:
    $(TEST_FNAME)

clean:
    $(RM) $(BUILDDIR)/*.d $(BUILDDIR)/*.o $(LIBQUICKSTART_FNAME) $(TEST_FNAME)

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.

make_tasks_1.png

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

make_tasks_2.png

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.

make_tasks_3.png

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.

make_tasks_4.png

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.

make_tasks_5.png

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

make_tasks_6.png

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