Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++

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 79.1K   30   7
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 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.

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

Sample Code

The sample code has following hierarchy.

1184735/sample_code_hierarchy.png

include/QuickStart.hpp

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

src/QuickStart.cpp

C++
#include "QuickStart.hpp"

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

test/QuickStartTest.cpp

C++
#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.

1184735/cmake_configure.png

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

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

1184735/cmake_build_list.png

Visual Studio Code starts building the code.

1184735/cmake_build.png

Choose CMake: Clean.

1184735/cmake_clean.png

Visual Studio Code cleans the build folder and built files.

1184735/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 provide a quick and convenient way to trigger most common actions via Side Bar.

1184735/cmake_side_bar.png

It shows all the available build targets to change.

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

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

1184735/cmake_debug_1.png

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

1184735/cmake_debug_2.png

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

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

1184735/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 follows:

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.

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

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

1184735/make_tasks_3.png

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:

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

1184735/make_tasks_5.png

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

1184735/make_tasks_6.png

Add a Comment Image 29

License

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


Written By
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: https://formosa1544.com
Email: shun@formosa1544.com

Comments and Discussions

 
QuestionEverything was good until I installed CMake & CMake Tools Pin
Member 1391105513-Jul-18 15:05
Member 1391105513-Jul-18 15:05 
QuestionNo CMake options Pin
Member 1382499713-May-18 7:59
Member 1382499713-May-18 7:59 
QuestionGreat,,, Pin
Pritam Zope8-May-17 10:16
Pritam Zope8-May-17 10:16 
Generalit will be perfect... Pin
Southmountain2-May-17 17:14
Southmountain2-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
vre4-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.