【问题标题】:How to make initial setup for C++ development in VS Code?如何在 VS Code 中进行 C++ 开发的初始设置?
【发布时间】:2026-02-04 22:00:01
【问题描述】:

我尝试通过 YouTube 视频为 VS Code 设置 C++,但它们已经很老了。因此,c_cpp_properties.jsontask.json 中存在配置错误。

【问题讨论】:

标签: c++ c visual-studio-code vscode-settings


【解决方案1】:
  1. 下载安装MinGW-w64
  2. 安装“C/C++ for Visual Studio Code”扩展

这是针对 Window 操作系统的所有设置。对于其他操作系统,您只需更改 c_cpp_properties.json configuration

c_cpp_properties.json文件的一个例子(你必须添加你自己的MinGW-w64的path):

{
    "configurations": 
    [
        {
            "name": "Win64",

            "includePath": [
                "${workspaceFolder}",
                "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0",
                "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\i686-w64-mingw32\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "compilerPath": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin",
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0",
                    "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\i686-w64-mingw32\\include"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 3
}


task.json 文件示例:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "debug",
     "type": "shell",
            "command": "",
            "args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
        },
        {
            "label": "Compile and run",
            "type": "shell",
            "command": "",
            "args": [
                "g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}", "&&" , "./${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true  
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}


您可以使用 Ctrl+Shift+b 构建 C++ 程序。

【讨论】: