VSCode配置C++ debug环境

文章目录

# 环境

VSCode
macOS / Ubuntu 
Codelldb

# 开始

  • VSCode 安装插件
C/C++
CodeLLDB
C/C++ Clang Command Adapter
  • 测试代码

main.cc

# include <iostream>

int main(int argc, char* argv[]){
  
    std::cout << "hello vscode debug" << std::endl;
  
    for(int i = 0 ; i < argc ; i++){
        std::cout << argv[i] << std::endl;
    }
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(vscode_debug)

set(CMAKE_BUILD_TYPE DEBUG)

add_executable(vscode_debug main.cc)
  • 编译
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
make 
  • 配置调试文件(launch.json)

    launch.json负责调试代码

    task.json 负责编译代码

  1. 生成launch.json文件

在这里插入图片描述

  1. 修改配置文件

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "lldb",
                "request": "launch",
                "name": "Debug",
                "program": "${workspaceFolder}/build/vscode_debug",
                "args": [], // 参数 
                "cwd": "${workspaceFolder}",
                "preLaunchTask": "Build with Clang" // 与 task 保持一致 
            }
        ]
    }
    
  • type: 配置类型(不知道是否可以修改TODO:)
  • request: 请求配置类型,可以设置为 launch(启动) 或者 attach(附加)
  • name: 配置名称,之后会出现再调试窗口的启动配置上
  • program: 进行调试的程序的位置(此处在当前文件夹下的build/vscode_debug可执行文件)
  • args: 参数 (./vscode_debug xxx yyy)
  • cwd: 当前调试所在的路径
  • preLaunchTask: 与task相关, 两边的值必须保持一致
  1. 调试
    在这里插入图片描述
  • 配置编译文件(task.json)

    1. 生成task.json文件
      - Shift+Command+p
      - Task: Configure Default Build Task
      - 使用模板创建task.json文件
      - Other

    2. 修改配置文件

      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "Build with Clang", //这个任务的名字在launch.json最后一项配置
                  "type": "shell",
                  "command": "cd ${workspaceFolder}/build && cmake -DCMAKE_BUILD_TYPE=Debug .. && make -j4",
               	 	"args": [],
                  "options": {
                      "cwd": "/usr/bin"
                  },
                	"group": {
                      "kind": "build",
                      "isDefault": true
                  }
              }
          ]
      }
      
    • label: 任务的名称 (任务的名字在launch.json最后一项配置)
    • type: 任务的类型,一共有两种(shell/Process),其中shell表示先打开shell,再执行输入命令;process则直接执行命令 (由于编译c++ 需要借助shell上进行执行命令)
    • command: 需要执行的命令 (cmake … && make)
  • 配置文件隐藏变量

    ${workspaceFolder} - the path of the folder opened in VS Code
    ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
    ${file} - the current opened file
    ${fileWorkspaceFolder} - the current opened file's workspace folder
    ${relativeFile} - the current opened file relative to workspaceFolder
    ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
    ${fileBasename} - the current opened file's basename
    ${fileBasenameNoExtension} - the current opened file's basename with no file extension
    ${fileDirname} - the current opened file's dirname
    ${fileExtname} - the current opened file's extension
    ${cwd} - the task runner's current working directory upon the startup of VS Code
    ${lineNumber} - the current selected line number in the active file
    ${selectedText} - the current selected text in the active file
    ${execPath} - the path to the running VS Code executable
    ${defaultBuildTask} - the name of the default build task
    ${pathSeparator} - the character used by the operating system to separate components in file paths
    

Vim配置C++ Debug环境

https://blog.csdn.net/Coxhuang/article/details/124900041

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值