Visual Studio Code C++ Quick Start

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

When using Visual Studio Code You must be aware that this is not a C++ IDE, but rather a very clever editor. Because of that – a 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. You can read more about them here, but we can use already written solutions in form of CMake and VS Code plugins.

Extensions

First - open an 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.
  • 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.


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


Enter the name of the application:


And tell CMake that we want an Executable application:


At this moment VS Code window should looks like this:

Building

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

  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 a button to build the target.
  5. is a target to build. Click on it to select the HelloVSCode target.
  6. is a 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:

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. Make sure that the When section for this binding has "inCMakeProject && !inDebugMode" values.

Other options

Basically - this is it. With such configuration you can easily start developing CMake based C++ projects 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. For more details how the CMake plugin works - read documentation. I hope this short tutorial showed you that VS Code is not so scary as it looks like at the beginning. Happy coding!