安装

  • 下载并安装MinGW-w64

    推荐使用离线方式安装,因网络原因,在线安装大概率失败。

    • 在线安装包

      进入MinGW-w64页面下载并安装MinGW-w64

      1582037372703

    • 离线压缩包

      若在线安装因为网络原因失败,可下载压缩包。往下稍微翻一下,选最新版本中的x86_64-posix-seh。下载完成后解压到合适的位置,如C:\Program Files\mingw64,然后手动添加环境变量:

      1582043841169

  • 下载插件:C/C++ 插件

    1582035915993

创建配置文件

在项目工程目录下创建.vscode文件,分别创建三个文件:

  • c_cpp_properties.json,将文件中的目录替换成你的MinGW安装目录:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    {
    "configurations": [
    {
    "name": "Win32",
    "includePath": [
    "${workspaceRoot}",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
    "C:/Program Files/mingw64/x86_64-w64-mingw32/include"
    ],
    "defines": [
    "_DEBUG",
    "UNICODE",
    "__GNUC__=6",
    "__cdecl=__attribute__((__cdecl__))"
    ],
    "intelliSenseMode": "msvc-x64",
    "browse": {
    "path": [
    "${workspaceRoot}",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
    "C:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
    "C:/Program Files/mingw64/x86_64-w64-mingw32/include"
    ],
    "limitSymbolsToIncludedHeaders": true,
    "databaseFilename": ""
    },
    "compilerPath": "C:/Program Files (x86)/mingw64/bin/gcc.exe",
    "cStandard": "c11",
    "cppStandard": "c++17"
    }
    ],
    "version": 4
    }
  • launch.json,将miDebuggerPath替换成你的MinGW安装目录:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "C++ Launch (GDB)",
    "type": "cppdbg",
    "request": "launch",
    "targetArchitecture": "x86",
    "program": "${workspaceRoot}\\${fileBasename}.exe",
    "miDebuggerPath":"C:/Program Files/mingw64/bin/gdb.exe",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceRoot}",
    "externalConsole": true,
    "preLaunchTask": "g++"  
    }
    ]
    }
  • tasks.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","-std=c++11","${file}","-o","${workspaceRoot}\\${fileBasename}.exe"],
    "problemMatcher": {
    "owner": "cpp",
    "fileLocation": ["relative", "${workspaceRoot}"],
    "pattern": {
    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
    "file": 1,
    "line": 2,
    "column": 3,
    "severity": 4,
    "message": 5
    }
    }
    }

使用

F5或左侧工具栏第四个Debug按钮中的绿色运行按钮即可运行,若有断点则为debug。

(可选)安装 Code Runner插件

Code Runner 可以免除以上三个配置文件的创建,能很方便地运行代码,但是无法调试。如果无需调试,可以选择安装此插件:

1582035983293

ctrl+,进入设置界面,点击右上角切换到Config文件,添加以下内容:

1582035717006

1
2
3
4
/* Code Runner Config*/
"code-runner.clearPreviousOutput": true, // 运行时清除输出
"code-runner.saveAllFilesBeforeRun": true, // 运行前保存所有文件
"code-runner.runInTerminal": true, // 在terminal中运行,从而可以输入,且不会乱码

完成后,点击右上角三角按钮或右键Run Code即可运行代码。

更多

更多VS Code相关配置见环境搭建 - VS Code

参考资料

Visual Studio Code 如何编写运行 C、C++ 程序? - 知乎

VScode 调试 c++_C/C++_yDarkin-CSDN博客