ubuntu16.04配置VSCode编译执行cpp

来源:互联网 发布:自学java入门书籍推荐 编辑:程序博客网 时间:2024/06/15 07:26
  1. 下载vscode
  2. 检查系统是否有安装g++

    g++ -v
  3. 新建一个文件夹(vscode不支持直接执行单个文件)
  4. 新建一个cpp文件

    #include<iostream>
    using namespace std;
    int main(){
    int a=10;
    int b=9;
    cout<<a+b<<endl;
    }
  5. 安装插件c/c++,F5调试
  6. 会弹出一个launch.json文件,然后修改为如下内容

    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceRoot}/${fileBasenameNoExtension}.out",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceRoot}",
    "environment": [],
    "externalConsole": true,
    "MIMode": "gdb",
    "preLaunchTask": "g++",
    "setupCommands": [
    {
    "description": "Enable pretty-printing for gdb",
    "text": "-enable-pretty-printing",
    "ignoreFailures": true
    }
    ]
    }
    ]
    }
  7. ctrl+p,然后输入>task,选择任务:配置任务执行程序,然后会弹出一个task.json文件,修改为如下内容

    {
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-g", "${file}", "-o", "${fileBasenameNoExtension}.out"],
    "problemMatcher": {
    "owner": "cpp",
    "fileLocation": ["relative", "${workspaceRoot}"],
    "pattern": {
    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
    "file": 1,
    "line": 2,
    "column": 3,
    "severity": 4,
    "message": 5
    }
    },
    "showOutput": "always"
    }
  8. 再次F5调试程序