VSCode C++ Development
Setup Visual Studio Code for C++ development
Using VSCode as your primary development IDE is quite interesting as it offers many features with the support of most programming languages. To develop C/C++ applications, you need to install a separate compiler; you can use MinGW or Microsoft Visual C++ compiler. In this tutorial, I’m giving you a brief tutorial about using VSCode to develop C/C++ applications with MSVC on Windows.
There is an official tutorial about setting the environment, but it may be unclear in some parts (e.g., it forces you to use Development Command Prompt each time you open up VSCode). So I’m adding an alternative for those parts and creating a custom build configuration.
Prerequisites
- Download and install VSCode from the official website.
- Install the Microsoft Visual C++ toolset. You can use Visual Studio Installer and select C++ build tools. To verify the installation, open up Developer Command Prompt from the Start menu and check thecl.execommand.
- Install C/C++ extension for VSCode from here.
Setup instructions
-  Create a directory for your project and put the required files in it. Then open the directory with VSCode. You can either use the command code .in the terminal or right-click inside the folder and chooseopen with Code.
-  Create a simple hello world C++ file called main.cppat the project’s root.#include <iostream> using namespace std; int main() { cout << "It Works!" << endl; cin.get(); }
-  Press F1 or Ctrl+Shift+Pto open Command Palette and chooseC/C++ Edit Configurations UI. It will open up the configuration windows. Choose theWin32for the configuration name and set a proper compiler path. It should automatically set the path forcl.exee.g.C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe. You can set other options likeIntelliSense mode,Include pathand etc.
-  Create an empty settings.jsonfile in.vscodedirectory and add the two parameters forcl.exepath, and build the directory path (if the file already exists, just add these two values). In this configuration, we are creating a build directory calledbuildto store the binaries. Also, we create a custom build command calledbuildCommandto executecl.exefrom the VS Command Prompt.{ "buildDir": "${workspaceRoot}\\build", "buildCommand": "C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2019\\Enterprise\\Common7\\Tools\\VsDevCmd.bat && cl" }We would be able to use these variables in other config files with ${config:<ConfigName>}syntax.
-  Next, we will create a tasks.jsonfile and create a task for our build. Create thetasks.jsoninside.vscodedirectory and fill it like below:{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "Set dependencies for build", "command": "if not exist ${config:buildDir} mkdir ${config:buildDir}" }, { "type": "shell", "label": "C/C++: cl.exe build active file", "command": "${config:buildCommand}", "args": [ "/Zi", "/EHsc", "/Fe:", "${config:buildDir}\\${fileBasenameNoExtension}.exe", "/Fo${config:buildDir}\\", "/Fd${config:buildDir}\\", "${file}" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$msCompile" ], "group": { "kind": "build", "isDefault": true }, "dependsOn": [ "Set dependencies for build" ] } ] }The arguments for cl.exeare pretty standard, but if you want to add more options, you can add them in theargssection.
-  To verify the installation, run the build task with Ctrl+Shift+B. This command will create thebuilddirectory and put the output.
-  To debug your application, create a launch.jsonfile in.vscodea directory like the below:{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "cl.exe - Build and debug active file", "type": "cppvsdbg", "request": "launch", "program": "${config:buildDir}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${env:buildDir}", "environment": [], "externalConsole": false, "preLaunchTask": "C/C++: cl.exe build active file" } ] }The above file is a default configuration, and I already change the programpath. I also add apreLaunchTaskto rebuild the project before launching the debug session.
-  After creating the debug configuration, you can put your breakpoints and launch the debug session with F5. 
Resources
All resource files can be accessed from my GitHub repository.