首页 > 解决方案 > 如何在 VS Code 中进行 C++ 开发的初始设置?

问题描述

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

标签: c++cvisual-studio-codevscode-settings

解决方案


  1. 下载并安装MinGW-w64
  2. 安装“ C/C++ for Visual Studio Code ”扩展

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

c_cpp_properties.json文件的示例(您必须添加自己的 MinGW-w64路径):

{
    "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您可以使用++构建 C Shift++ 程序b


推荐阅读