linux c/c++ vscode 调试设置

首先要安装 gcc g++ gdb
终端输入sudo apt-get install gcc g++ gdb
新建一个hello.c
编辑task.json与launch.json

task.json与launch.json的关系
前者为gcc/g++命令,负责编译,后者为启动可执行程序选项。
先编写前者,后编写后者。

task.json

{
    "version": "2.0.0",
    "tasks": [{
      "label": "build c program",       //这个要与launch.json里添加的prelacunchTask一致
      "type": "shell",
      "command": "gcc",     //意思是使用gcc编译,如果是c++程序,则改为g++
      "args": [                //等价于 gcc hello.c -o hello
        "-g",
        "${file}",                     //.c文件
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",     //可执行文件
      ]
    }]
  }

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",    //此处需要把系统默认改为这个
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "build c program",           //添加这一行
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
            
        }
    ]
}

你可能感兴趣的:(linux c/c++ vscode 调试设置)