DEV Community

Cover image for Build and Debug C++ Application with VSCode and MSVC
Masoud Rahimi
Masoud Rahimi

Posted on

Build and Debug C++ Application with VSCode and MSVC

Using VSCode as your main 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 have a separate compiler installed, you can either 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 force you to use Development Command Prompt each time you open up VSCode). So I'm adding an alternative for those parts and create a custom build configuration.


  1. Download and install VSCode from the official website.
  2. Install 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 the cl.exe command.
  3. Install C/C++ extension for VSCode from here.

Setup instructions

  1. 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 terminal or right-click inside the folder and choose open with Code.

  2. Create a simple hello world C++ file called main.cpp in the root of the project.

    #include <iostream>
    using namespace std;
    int main()
        cout << "It Works!" << endl;
  3. Press F1 or Ctrl+Shift+P to open Command Palette and choose C/C++ Edit Configurations UI. This will open up the configuration windows. Choose the Win32 for configuration name and set a proper compiler path. It should automatically set the path for cl.exe e.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 like IntelliSense mode, Include path and etc.

  4. Create an empty settings.json file in .vscode directory and add the two parameters for cl.exe path and build directory path (if the file already exists just add these two values). In this configuration, we are creating a build directory called build to store the binaries. Also, we create a custom build command called buildCommand to execute cl.exe from 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.

  5. Next, We are going to create a tasks.json file and create a task for our build. Create the tasks.json inside .vscode directory 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": [
                "options": {
                    "cwd": "${workspaceFolder}"
                "problemMatcher": [
                "group": {
                    "kind": "build",
                    "isDefault": true
                "dependsOn": [
                    "Set dependencies for build"

    The arguments for cl.exe are quite standard but if you want to add more options you can add them in the args section.

  6. To verify the installation run the build task with Ctrl+Shift+B. This will create the build directory and put the output.

  7. To debug your application create a launch.json file in .vscode a directory like below:

        "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"

    This is a default configuration and I already change the program path. I also add a preLaunchTask to rebuild the project before launching the debug session.

  8. After creating the debug configuration you can put your breakpoints and launch the debug session with F5.


All resource files can be accessed from my GitHub repository.

Top comments (3)

shockvawe profile image
Yusuf Umut Piynar

Oh man, I am working on a bug from this code for almost 3 hours by now. I am not used to Windows environment and batch scripting. I challenged myself to use MSVC but I didn't know that it would be this hard.
In your tasks.json there's a line:
"command": "if not exist ${config:buildDir} mkdir ${config:buildDir}"
This line gives an error:
At line:1 char:3

  • if not exist C:\Users\aurus\Desktop\C\build mkdir C:\Users\aurus\Desk ...
  • ~ Missing '(' after 'if' in if statement.
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement

I even learned the syntax of batch scripting and did some examples in the Powershell as a complete linux user yet I couldn't figure out why the f this line gives this error.

I would appreciate some enlightment.

shockvawe profile image
Yusuf Umut Piynar

Solved the issue, took my 4 hours. Just switching VSCODE terminal to CMD from PS would probably solve the issue (or maybe not idk) but this definitely does.

Change the problematic line with this:

"command": "cmd",
"args": ["/C", "if not exist ${config:buildDir} mkdir ${config:buildDir}"]

masoudr profile image
Masoud Rahimi

Hi, I'm sorry I haven't check the community for a while.
Yes you are right you need the set CMD as default shell because scripts are based on it.