Skip to content

使用VSCode编写C/C++

GCC编译器

下载地址

https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

使用说明

下载之后解压,将其中的mingw64文件夹移动到C:\Program Files目录下,将C:\Program Files\mingw64\bin路径添加到系统环境变量Path中。

vscode C++插件

在扩展(左上竖着并排的五个图标第五个)中搜索C++,安装C/C++扩展。

配置tasks.json文件

在目录下新建.vscode文件夹,在.vscode文件夹中新建tasks.json文件,添加如下内容:

json
{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "process",
        "command": "g++",
        "args": [
          "-g",
          "-Wall",
          "-std=c++14",
          "-lm",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}.o"
        ],
        "presentation": {
          "echo": false,
          "reveal": "silent",
          "showReuseMessage": false
        },
        "problemMatcher": "$gcc",
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "windows": {
          "args": [
            "-g",
            "-Wall",
            "-std=c++14",
            "-lm",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}.exe"
          ]
        }
      },
      {
        "label": "run",
        "type": "shell",
        "dependsOn": "build",
        "command": "${fileDirname}/${fileBasenameNoExtension}.o",
        "presentation": {
          "focus": true
        },
        "group": {
          "kind": "test",
          "isDefault": true
        },
        "windows": {
          "command": "${fileDirname}/${fileBasenameNoExtension}.exe"
        }
      }
    ]
  }

配置一键运行

点击左下角齿轮图标,选择键盘快捷方式(keyboard shortcuts),搜索tasks,找到Tasks: Run Test Tasks,绑定为喜欢的键(如F10)。

配置调试

在.vscode文件夹中新建launch.json文件,添加如下内容:

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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "windows": {
                "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
                "miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",
            },
            "osx": {
                "externalConsole": true
            },
        }
    ]
}

然后就可以按F5调试了,不需要绑定快捷键,另外如果要使用调试功能,目录名和文件名都不能有中文,否则报错。

最后更新于:

未经许可禁止任何形式的转载