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.
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:
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:- shows a number of errors and warnings in the project.
- 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.
- is the version of the Kit we use to compile. You can also change it anytime by clicking on it.
- is a button to build the target.
- is a target to build. Click on it to select the HelloVSCode target.
- is a button to debug. When you click it the list of available targets should appear in the Command Palette. Select the HelloVSCode.