vscode配置cpp调试环境

  1. 安装vscode插件
    1. c/c++
    2. cmake
    3. cmake-tool
  2. 安装程序
    1
    sudo apt install cmake clang gdb
  3. 配置vscode
    • c_cpp_properties.json 菜单栏【查看】【命令面板…】,输入关键词”C/C++ edit”,选择匹配到的命令”C/C++: Edit Configurations (json)”

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      {
      "configurations": [
      {
      "name": "Linux",
      "includePath": [
      "${workspaceFolder}/**"
      ],
      "defines": [
      ],
      "compilerPath": "/usr/bin/clang++",
      "cStandard": "c11",
      "cppStandard": "c++11",
      "intelliSenseMode": "${default}",
      "configurationProvider": "ms-vscode.cmake-tools"
      }
      ],
      "version": 4
      }

    • 添加task.json

      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
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      {
      "version": "2.0.0",
      "options": {
      "cwd": "${workspaceFolder}" //需要进入到我们执行tasks任务的文件夹中
      },
      "tasks": [
      {
      "type": "shell",
      "label": "mkdir",
      "group": {
      "kind": "build",
      "isDefault": true
      },
      "command": "mkdir",
      "args": [
      "-p",
      "build"
      ]
      },
      {
      "type": "shell",
      "label": "cmake",
      "group": {
      "kind": "build",
      "isDefault": true
      },
      "options": {
      "cwd": "${workspaceFolder}/build"
      },
      "command": "cmake",
      "args": [
      ".."
      ]
      },
      {
      "label": "make",
      "group": {
      "kind": "build",
      "isDefault": true
      },
      "options": {
      "cwd": "${workspaceFolder}/build"
      },
      "command": "make",
      "args": [
      "-j4"
      ]
      },
      {
      "label": "build",
      "dependsOrder": "sequence",
      "dependsOn": [
      "mkdir",
      "cmake",
      "make"
      ]
      }
      ]
      }
      这里实际上将手动的make过程程序化,有需要可以对task任务加减,如:添加rm -rf CMakeCache.txt命令

    • 添加launch.json 实际的调试入口,启动task任务

      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
      {
      // 使用 IntelliSense 了解相关属性。
      // 悬停以查看现有属性的描述。
      // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [

      {
      "name": "C++ launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/viewer/gltf-viewer",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
      {
      "description": "为 gdb 启用整齐打印",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
      },
      {
      "description": "将反汇编风格设置为 Intel",
      "text": "-gdb-set disassembly-flavor intel",
      "ignoreFailures": true
      }
      ],
      "preLaunchTask": "build",
      "miDebuggerPath": "/usr/bin/gdb"
      }
      ]
      }

  4. 执行 写好CMakeLists.txt ctrl+shift+D 点击launch任务 done

本文作者:Berg Zha

本文链接:http://junglemanpro.com/2024/04/04/vscode%E9%85%8D%E7%BD%AEcpp%E8%B0%83%E8%AF%95%E7%8E%AF%E5%A2%83/

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

ESC 关闭 | 导航 | Enter 打开
输入关键词开始搜索