You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Arch Linux环境下使用(Neo)vim调试C++的方法求助

Neovim C++ 调试配置方案

前置依赖安装

先在Arch Linux系统安装必要工具:

  • 安装gdb:sudo pacman -S gdb
  • 编译C++代码时必须添加-g参数生成调试符号,示例编译命令:g++ -g main.cpp -o main
  • 若使用CMake构建项目,添加编译参数-DCMAKE_BUILD_TYPE=Debug即可自动生成带调试符号的二进制文件

所需Neovim插件

用你常用的插件管理器安装以下插件即可:

  • 调试核心组件:mfussenegger/nvim-dap
  • 可视化调试UI:rcarriga/nvim-dap-ui
  • 行内变量虚拟文本显示:theHamsta/nvim-dap-virtual-text
  • 打开Mason安装C++调试适配器::MasonInstall cpptools

基础配置示例(Lua)

下面是最简可用配置,可直接加入你的Neovim配置文件:

-- 加载dap相关模块
local dap = require("dap")
local dapui = require("dapui")

-- 配置cpptools适配器
dap.adapters.cppdbg = {
  id = 'cppdbg',
  type = 'executable',
  command = vim.fn.stdpath('data')..'/mason/bin/OpenDebugAD7',
}

-- 配置C++调试启动项
dap.configurations.cpp = {
  {
    name = "Launch C++ executable",
    type = "cppdbg",
    request = "launch",
    program = function()
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
    end,
    cwd = '${workspaceFolder}',
    stopAtEntry = false,
    setupCommands = {
      {
        text = '-enable-pretty-printing',
        description =  'enable gdb pretty printing for C++ STL objects',
        ignoreFailures = false
      },
    },
  },
}

-- 自动开启/关闭dapui
dap.listeners.after.event_initialized["dapui_config"] = function()
  dapui.open()
end
dap.listeners.before.event_terminated["dapui_config"] = function()
  dapui.close()
end
dap.listeners.before.event_exited["dapui_config"] = function()
  dapui.close()
end

-- 开启虚拟文本
require("nvim-dap-virtual-text").setup()
-- 初始化dapui
dapui.setup()

常用调试操作

可根据你的使用习惯绑定快捷键,常用操作对应命令如下:

  • 切换断点:require'dap'.toggle_breakpoint()
  • 启动调试/继续执行:require'dap'.continue()
  • 单步跳过(不进入函数内部):require'dap'.step_over()
  • 单步进入(进入函数内部):require'dap'.step_into()
  • 单步跳出(退出当前函数):require'dap'.step_out()
  • 查看悬停变量值:require'dapui'.eval()
  • 终止调试:require'dap'.terminate()

调试流程

  1. 用带-g参数的命令编译你的C++代码,生成带调试符号的二进制文件
  2. 打开Neovim进入你的代码文件,在需要暂停的位置打断点
  3. 执行启动调试命令,在弹出的输入框中输入你刚才编译的二进制文件路径
  4. 按需使用上述操作快捷键调试即可,变量值、调用栈、断点列表会在dapui的分栏中同步显示

内容的提问来源于stack exchange,提问作者Sobhy Rzk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 16:06:05