vscode 基于 CMake 配置 C++ 调试环境

本文最后更新于:2024年9月27日 下午

配置 vscode 相关 C++插件

  • vscode 插件商店下载 C++C/C++ Extension PackCMakeCMake HighlightCMake Language SupportCMaketoolsMakefile Tools插件。

配置 vscode相关配置文件

  • launch.json:主要修改program关键字 debug 程序的路径,preLaunchTask关键字:在 debug 之前需要运行的任务。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    {
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "type": "lldb", // 指定调试器类型为LLDB。
    // "launch":表示启动一个新的程序进行调试。
    // "attach":表示附加到一个正在运行的进程进行调试。
    // "custom":表示执行一个自定义的调试请求。
    "request": "launch", // 启动一个新的程序进行调试
    "name": "C++ CMake Debug", //调试启动 name,无所谓随便取
    "program": "${workspaceFolder}/Build/cmake_debug", //需要debug的程序,修改程序路径
    "args": [], // 添加一些参数命令
    "cwd": "${workspaceFolder}", // 这指定程序的工作目录
    "preLaunchTask": "CMake: 构建" // 启动调试之前要运行的任务,关联到 tasks.json
    }
    ]
    }
  • 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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    {
    // type 关键字:
    // shell: 表示该任务是一个 shell 命令,将在终端中执行。
    // process: 表示该任务是一个进程,将在后台运行。
    // cppbuild: 表示该任务是一个 C++ 构建任务,用于构建 C++ 项目。
    "version": "2.0.0",
    "tasks": [
    // 1. cmake 配置
    {
    "type": "cppbuild",
    "label": "CMake: 配置",
    "command": "cmake", // 启动命令为 cmake
    "args": [
    "-DCMAKE_BUILD_TYPE=Debug", // cmake 对应的一些参数
    "-S .",
    "-B Build"
    ],
    "problemMatcher": "$msCompile",
    "group": {
    "kind": "build",
    "isDefault": true
    },
    "options": {
    "cwd": "${workspaceFolder}"
    }
    },
    // 2. cmake 构建
    {
    "type": "cppbuild",
    "label": "CMake: 构建",
    "command": "cmake",
    "args": [
    "--build",
    "Build"
    ],
    "problemMatcher": "$msCompile",
    "group": {
    "kind": "build",
    "isDefault": true
    },
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "dependsOn": [
    "CMake: 配置"
    ]
    },
    // 3. 删除Build目录
    {
    "type": "shell",
    "label": "删除Build目录",
    "command": "rm",
    "args": [
    "-rf",
    "Build"
    ],
    "problemMatcher": "$msCompile",
    "group": {
    "kind": "build",
    "isDefault": true
    },
    "options": {
    "cwd": "${workspaceFolder}"
    }
    },
    // 4. 运行可执行文件
    {
    "type": "shell",
    "label": "运行可执行文件",
    "command": "./Build/cmake_debug",
    "problemMatcher": "$msCompile",
    "group": {
    "kind": "build",
    "isDefault": true
    },
    "options": {
    "cwd": "${workspaceFolder}"
    },
    "dependsOn": [
    "CMake: 构建"
    ]
    }
    ]
    }
  • c_cpp_properties.json:主要是includePath需要写入包含库的头文件路径,能够让vscode 识别到。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    {
    "configurations": [
    {
    "name": "Mac",
    "includePath": [
    "${workspaceFolder}/**",
    "/opt/homebrew/Cellar/opencv/4.10.0_2/include/opencv4/**"
    ],
    "defines": [],
    "macFrameworkPath": [
    "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
    ],
    "compilerPath": "/usr/bin/clang",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "macos-clang-arm64",
    "configurationProvider": "ms-vscode.cmake-tools"
    }
    ],
    "version": 4
    }

运行生成任务

Mac vscode 快捷键:command+shift+B则可看到对于的生成任务,打好断点后,就能够调试程序了。vscode构建


vscode 基于 CMake 配置 C++ 调试环境
https://zzmes.github.io/2024/08/30/vscode-cpp/
作者
YangYangYang
发布于
2024年8月30日
许可协议