VSCode配置C++ debug环境

本文详细介绍了如何在VSCode中配置C/C++环境,包括安装插件CodeLLDB,设置CMakeLists.txt和launch.json,以及编写和调试C++代码的过程。通过实例展示如何配置编译任务(task.json)和Vim的C++调试环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

# 环境

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

### VS Code 中配置 C++ 调试环境 要在 Visual Studio Code (VS Code) 中成功配置调试 C++ 程序,需要完成以下几个方面的设置: #### 1. 安装必要的工具链 为了编译和运行 C++ 程序,在 Linux 或其他类 Unix 系统中,通常需要安装 `gcc` 和 `g++` 工具链以及 GDB 调试器。可以通过包管理器安装这些工具[^4]。 对于 Windows 用户,可以选择 MinGW-w64 或者 Cygwin 提供的 GCC/G++ 编译器支持。此外,也可以使用 Microsoft 的 Clang/LLVM 工具链作为替代方案。 #### 2. 安装 VS Code 扩展 在 VS Code 中,需安装 **C/C++** 扩展(由微软官方提供),它提供了 IntelliSense 支持、代码导航以及其他功能来增强开发体验。另外还需要安装 **CMake Tools** 如果项目依赖于 CMake 构建系统[^2]。 #### 3. 创建 launch.json 文件 此文件定义了启动程序的方式及其参数。下面是一个典型的例子用于本地调试单个源文件的应用场景: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] } ``` 上述 JSON 片段中的 `"program"` 字段应指向可执行文件的位置;而 `"preLaunchTask"` 则关联到 tasks.json 中的任务名称以便先构建再调试。 #### 4. 设置 task.json 进行自动化构建 tasks.json 可以用来指定如何调用外部命令来进行项目的构建工作流。这里给出一个简单的示例,展示怎样利用 g++ 来编译单一 cpp 文件成为可执行文件: ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", "${relativeFile}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } ``` 这里的 `${relativeFile}` 是指当前打开编辑窗口内的那个 .cpp/.cc 文件路径名相对形式表达式。 #### 5. 使用远程主机上的资源 当目标平台不是本机而是另一台服务器时,则除了以上步骤外还需额外考虑 SSH 访问权限等问题,并确保远端已经具备完整的开发套件包括但不限于 GNU Compiler Collection(GCC)/G++, GNU Debugger(GDB)。 --- ### 注意事项 - 尽管可以直接通过 gcc 加 `-lstdc++` 参数手动链接标准库,但推荐始终优先选用 g++ 编译器因为它内置处理好了这个问题从而简化流程[^1]。 - 在跨平台操作期间,请留意不同操作系统间路径分隔符差异(/ vs \\),必要时候调整为双反斜杠格式\[如:C:\\path\\to\\my\\id_ed25519]\]^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值