Visual Studio Code C++ development tutorial banner

This tutorial consists of both an article and a video.

In this guide, you will configure VS Code on Windows for C/C++ development using the GCC compiler (gcc/g++) and the GDB debugger from UCRT64.

Once the setup is complete, you will build and debug a simple Hello World program inside VS Code. This article does not teach GCC, GDB, UCRT64, or the C/C++ languages themselves. There are plenty of good resources for those topics.

This tutorial is long, but if you follow it carefully from start to finish, getting started with VS Code and C/C++ will be much easier.

1. Prerequisites

To complete this tutorial successfully, work through the following steps first:

  1. Install the latest UCRT64 toolchain through MSYS2. It provides current native builds of GCC, UCRT64, and many other useful tools and libraries for C/C++. Follow the Installation instructions on the MSYS2 website. If the official installer is too slow to download, you can also use this mirror. Installing MSYS2 may take a while because it refreshes package databases.

    MSYS2 installation page for C++ development MSYS2 installation in progress MSYS2 installation completed successfully

  2. Install the UCRT64 toolchain. After MSYS2 is ready, a UCRT64 terminal opens. Run the following commands in order and accept the defaults so every member of the toolchain group gets installed:

    pacman -Suy
    pacman -S tar make
    pacman -S mingw-w64-ucrt-x86_64-toolchain
    pacman -Suy
    

    Installing UCRT64 toolchain in MSYS2

  3. Add the UCRT64 bin folder to your Windows environment variables:

    • Open Windows Settings from the search bar.
    • Search for Edit environment variables for your account.

    Windows environment variables settings page

    • Under system variables, select Path, then click Edit.

    System variables PATH configuration

    • Click New and add the UCRT64 target folder. If you used the default MSYS2 path, add C:\msys64\ucrt64\bin. Click OK to save the change.

    Adding UCRT64 path to system environment variables

  4. Verify that the UCRT64 tools are installed and available. Open a new terminal and run:

    gcc --version
    g++ --version
    gdb --version
    

    Verifying GCC compiler installation and version

  5. Download and install VS Code from the official download link. If the download is slow, you can also use this mirror. Run the installer, accept the license, keep clicking Next, select every option on the Select Additional Tasks screen, and then click Install.

    Visual Studio Code download page VS Code installer license agreement VS Code installer additional tasks selection

    Tip: the installer adds VS Code to your PATH, so later you can type code . in a terminal to open the current folder in VS Code. After installation, restart your terminal so the environment variable changes take effect.

  6. Install the required extensions. To match the screenshots in this article, install the Chinese (Simplified) Language Pack for Visual Studio Code and C/C++.

    Installing Chinese language pack and C++ extensions

2. Create Hello World

Create an empty folder anywhere on disk to hold your C/C++ code. Open that folder in VS Code, for example by right-clicking it and choosing Open with Code. This folder becomes your workspace. Accept the workspace trust prompt, because this is a folder you created yourself.

VS Code workspace trust dialog VS Code workspace successfully trusted

As you follow this tutorial, you will see a .vscode folder appear in the workspace together with three files:

launch.json (debugger configuration)

tasks.json (build configuration)

c_cpp_properties.json (C/C++ configuration)

2.1 Add a source file

In the Explorer title bar, click New File and name it either helloworld.c or helloworld.cpp.

Creating new C++ source file in VS Code

2.2 Add the Hello World source code

Copy this into helloworld.c:

#include <stdio.h>

int main()
{
    printf("Hello world!\n");

    return 0;
}

Or copy this into helloworld.cpp:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    return 0;
}

Now save the file.

Hello World C++ code in VS Code editor

You can also enable Auto Save, which is covered later in More, item 2.

The activity bar on the far left lets you switch between different views such as Search, Run and Debug, and Extensions. We will use Run and Debug later. For more UI details, see the official VS Code user interface docs.

Note: when saving or opening C/C++ files, you may see notifications from the C/C++ extension about preview releases. You can safely ignore them by clearing the notifications.

2.3 Customize debugging with launch.json

Click the Run and Debug setup button in the editor toolbar. From the predefined debug configurations, choose C/C++: gcc.exe build and debug active file for helloworld.c, or C/C++: g++.exe build and debug active file for helloworld.cpp.

VS Code C++ debug configuration dropdown menu showing gcc.exe build options

VS Code creates launch.json and tasks.json.

Add the following line to launch.json so the internal debug console does not pop open and execution stays in the integrated terminal instead:

    "internalConsoleOptions": "neverOpen",

VS Code launch.json configuration file with internalConsoleOptions setting

If you are building and debugging a C source file, replace line 10 of tasks.json with one of the following options, leaving only one enabled:

    "${file}", // Compile only the currently opened source file
    // "${fileDirname}\\*.c", // Compile all C source files in the current folder

If you are building and debugging a C++ source file, use:

    "${file}", // Compile only the currently opened source file
    // "${fileDirname}\\*.cpp", // Compile all C++ source files in the current folder

VS Code tasks.json configuration showing file compilation settings

Now save both files.

2.4 Run Hello World

Return to helloworld.c or helloworld.cpp and press Ctrl+F5.

If compilation succeeds, the program output appears in the integrated terminal.

VS Code integrated terminal showing successful C++ program compilation and output

2.5 Debug Hello World

Click in the left margin of the editor to set a breakpoint, then press F5 to start debugging.

2.6 Explore the debugger

Before stepping through the code, notice the UI changes:

  • The integrated terminal appears below the source editor.
  • The line with your breakpoint is highlighted.
  • The Run and Debug panel on the left shows debugging information.
  • A floating debug toolbar appears near the top of the editor and can be dragged around.

VS Code debugger interface showing breakpoint, debug panel and integrated terminal

2.7 Step through the code

At this point you are ready to step through the program.

The exact stepping process is omitted here, but the keyboard shortcuts are shown in the appendix at the end of this article.

When you finish, you will see the final output in the integrated terminal together with some additional diagnostics from GDB.

2.8 Set watch expressions

Sometimes you want to track a variable while the program runs. You can do that with Watch expressions.

In the Watch window, click the plus sign and type the variable name.

VS Code watch window showing how to add variable monitoring during debugging

You can also hover the mouse over a variable at a breakpoint to inspect its current value quickly.

2.9 C/C++ configuration

If you want more control over the C/C++ extension, create a .vscode/c_cpp_properties.json file. This lets you configure the compiler path, include path, C/C++ standard, and more. The compilerPath value should match the compiler path used in tasks.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/ucrt64/bin/gcc.exe",
            "cStandard": "gnu23",
            "cppStandard": "gnu++23",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

3. More

  1. Recommended extensions:

    For each extension’s features, commands, and settings, check its own Overview page. You can also explore other excellent extensions on your own.

  2. To make VS Code easier to use, create a user settings file at %AppData%\Code\User\settings.json and add something like the following. It includes the Auto Save setting mentioned earlier. You can also refer to the author’s VS Code Settings.

     {
         "debug.onTaskErrors": "showErrors",
         "editor.cursorSmoothCaretAnimation": "on",
         "editor.formatOnPaste": true,
         "editor.formatOnSave": true,
         "editor.formatOnType": true,
         "editor.minimap.enabled": false,
         "editor.mouseWheelZoom": true,
         "editor.smoothScrolling": true,
         "editor.stickyScroll.enabled": true,
         "editor.unicodeHighlight.allowedLocales": {
             "zh-hans": true,
             "zh-hant": true
         },
         "editor.wordWrap": "on",
         "explorer.confirmDelete": false,
         "explorer.confirmDragAndDrop": false,
         "files.autoSave": "afterDelay",
         "files.autoGuessEncoding": true,
         "terminal.integrated.allowChords": false,
         "terminal.integrated.enableMultiLinePasteWarning": false,
         "terminal.integrated.smoothScrolling": true,
         "workbench.iconTheme": "material-icon-theme",
         "workbench.list.smoothScrolling": true,
         // Extensions
         "code-runner.runInTerminal": true,
         "code-runner.saveFileBeforeRun": true,
         "github.copilot.enable": {
             "*": true,
             "plaintext": true,
             "markdown": true,
             "scminput": true
         },
         "github.copilot-labs.showBrushesLenses": true,
         "github.copilot-labs.showTestGenerationLenses": true
     }
    

    VS Code user settings.json configuration file with various editor and extension settings VS Code settings interface showing Material Icon Theme and other UI customizations

  3. You can enable Settings Sync to keep your settings, per-platform keybindings, extensions, and more in sync across multiple machines.

  4. If you want to delete generated .exe, .o, and similar files after compiling, create a del.bat file in the workspace with the following content. Then right-click it in the Explorer and choose Run Code (which requires the Code Runner extension):

     del *.exe /q /s
     del a.out /q /s
     del *.o /q /s
     del tempCodeRunnerFile.c /q /s
    
  5. If you want a VS Code shortcut to open directly into your workspace folder, right-click the shortcut, choose Properties, and append the workspace path to Target, for example: "C:\Program Files\Microsoft VS Code\Code.exe" "C:\Code\C++".

    VS Code shortcut properties window showing how to add workspace path to target

  6. Avoid Chinese characters in source-file paths and in your Windows username, or you may run into encoding-related build issues.

  7. If VS Code was already installed before you followed this tutorial, uninstall VS Code and remove all of its configuration folders, including %AppData%\Code and %USERPROFILE%\.vscode, before setting it up again according to this guide.

  8. This tutorial is Windows-based. Linux users can refer to Configure VS Code for C++ on Linux, macOS users can refer to Configure VS Code for C++ with Clang, and Windows users can also look at C++ with WSL in VS Code and Configure VS Code for Microsoft C++.

  9. VS Code also offers VS Code Insiders, which is the build shown in the screenshots of this article. You can install it if you want access to the latest daily updates.

  10. VS Code also has VS Code for the Web, which lets you use VS Code in a browser.

  11. One of the most exciting areas today is AI in VS Code, especially the GitHub Copilot extension mentioned above. It can help you write code faster and more intelligently, learn from generated code, and even help configure your editor.

  12. As one of the most popular code editors in the world, VS Code has many more capabilities worth exploring. The official documentation is the best place to continue.

  13. This article only covers beginner-level VS Code plus C/C++ setup. If you want to go deeper, the official docs are still the best next step.

  14. Because the text of this article keeps being updated, the screenshots and video may not always match the latest wording exactly. Please follow the written instructions first.

  15. This tutorial may still have imperfections. If you find errors or have suggestions, feel free to contact the author through CONTACT on Xi Xu’s Home Page.

4. Appendix

VS Code C++ debugging keyboard shortcuts reference chart in Chinese VS Code C++ general keyboard shortcuts reference chart in Chinese