I was writing about the best IDEs for C++ development lately and I mentioned the Visual Studio Code – the free, open source, powerfull editor that can be easily transformed into a slick environment for C++ coding. But how to do such transform? This is my approach.

When using Visual Studio Code You must be aware that this is not an IDE, but rather very clever editor. Because of that – the developer must provide a building pipeline by itself. It can be done by writing special task and lanuch scripts in JSON, which are lately interpreted by VS Code, but we can use already written solutions in form of extensions.

Extensions

First – open extensions window (Ctrl + Shift + X), then find and install the following extensions:

  • C/C++ – this is an official, written by Microsoft, extension for C/C++ support. It basically gives you all of the support in writing C++ code like code formatting, autocompletion and debugging support.
  • CMake Tools - it provides the full configure + build workflow for the CMake based projects. In other words – it gives you ready to use build/debug pipeline.
  • CMake -  it provides the colorization and completion of the CMakeList.txt file.

Creating new project

To create a new project – create a new folder for this project first and open it in VS Code ( File -> Open Folder… ).

Then open Command Palette (Ctrl + Shift + P) and type: CMake: Quick Start.

vsc2

Then, follow the instructions. It might ask you for a Kit you want to use. In this example we use GCC 7.3.0:

vsc5

Enter the name of the application:

vsc3

And tell CMake that we want an executable application:

vsc4

At this moment the VS Code window should look like this:

Building

Look at the bottom of the VS Code window. You should see such a toolbar.

vsc7

  1. Shows a number of errors and warnings in the project.
  2. Is the CMake configuration. There should be Debug, Release, MinSizeRel and RelWithDebInfo options available. To change it simply click on this text in this toolbar and select the desired option from the Command Palette.
  3. Is the version of the Kit we use to compile. You can also change it anytime by clicking on it.
  4. Is the button to build the target.
  5. Is the target to build. Click on it to select the HelloVSCode target.
  6. Is the button to debug. When you click it the list of available targets should appear in the Command Palette. Select the HelloVSCode.

After you select the proper targets the toolbar should look like this:

vsc8

Keyboard Shortcuts

By default – the CMake debug task is binded to the “Ctrl+F5″ shortcut while the “F5″ is used for a default debug task. I prefer using “F5″ for CMake debugging, this is why I rebound the keyboard shortcuts. To do it, click on the gear icon in the bottom left corner of the VS Code window and select “Keyboard Shortcuts” option. Then, find the cmake.debugTarget command and bind the desired key combination.

Other Options

Basicaly – this is it. With such configuration you can easily start developing CMake based C++ project in VS Code! I recommend to check all of the other CMake commands in Commnd Palette. Now you can build and debug the application pretty the same way you could do it in Visual Studio with CMake support. I hope this short tutorial showed you that VS Code is not so scary as it looks like at the beginning. Happy coding!

Further readings: