Jade Dungeon

vscode C / C++

C / C++ 环境

VSCode安装两个插件,如果是之前安装过的,这时候就会出现install on WSL选项, 点击即可:

vscode

配置launch.jsontasks.json

{
  "version": "0.2.0",
  "configurations": [ {
      "name": "C/C++",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/bin/gdb",
      "preLaunchTask": "compile",
      "setupCommands": [ {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
    },
  ]
}
{
  "version": "2.0.0",
  "tasks": [{
      "label": "compile",
      "command": "g++",
      "args": [
        "-g", "${file}",
        "-o", "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [ "relative", "${workspaceRoot}" ],
        "pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "severity": 4,
          "message": 5
        }
      },
      "group": { "kind": "build", "isDefault": true }
    }
  ]
}

配置完成以后:

  • 快捷键Ctrl+Shift+b触发编译当前编辑窗口所在的文件。
  • Debug按钮会触发编译并调试当前编辑窗口所在的文件。

例:

#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
    cout << "hello world" << endl;
    char name[100];
    printf("What is your name?\n");
    scanf("%s", name);
    printf("Hello, %s. nice to meet you!\n", name);
    return 0;
}

插件Code Runner的作用是让你不用配置tasks.jsonluanch.json 文件就可以很方便地编译并运行源代码文件。 安装好插件后右上角会出现一个播放按钮:

vscode