VS Code can work well as a lightweight C/C++ learning environment on Windows. The key is to install a compiler, add the right extension, adjust a few settings, and give each project a .vscode folder that tells VS Code how to build and debug. The paths below assume MinGW-w64 is installed in C:\mingw64.

Install VS Code and MinGW-w64

Download VS Code from code.visualstudio.com and install it normally.

For the compiler, get MinGW-w64 from its SourceForge download page. The package used in this setup is x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z. Extract it so that g++.exe and gdb.exe end up in C:\mingw64\bin.

Add the extensions

Install these two extensions in VS Code:

  • Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code
  • C/C++

Adjust settings

Open Settings and paste each key below into the search box. Set the checkboxes and values as shown:

  • editor.formatOnType — enabled
  • editor.mouseWheelZoom — enabled
  • C_Cpp.autocompleteAddParentheses — enabled
  • C_Cpp.default.cppStandard — c++17
  • C_Cpp.default.cStandard — c11
  • C_Cpp.default.compilerPath — C:\\mingw64\\bin\\g++.exe
  • C_Cpp.default.includePath — C:\\mingw64\\lib
  • C_Cpp.default.intelliSenseMode — gcc-x64

Add build and debug files

Create a .vscode folder inside the workspace folder you have open in VS Code. Put the following tasks.json in it:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

For running in the integrated console, use this launch.json:

{
    // 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": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

For an external console, change externalConsole in the configuration above to true.

If you also want the external console to stay open after the program exits, use this variant:

Warning: This method prevents normal debugger use. It is better to set breakpoints instead.

{
    // 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": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\System32\\cmd.exe",
            "args": [
                "/k",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "pause",
                "&",
                "exit"
            ],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}