首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vim配置C++ debug环境

Vim配置C++ debug环境

作者头像
Autooooooo
发布2022-05-23 10:02:41
1K0
发布2022-05-23 10:02:41
举报
文章被收录于专栏:CoxhuangCoxhuang

文章目录

环境

我的配置地址: C++ debug配置地址

NeoVim 0.7
codelldb 1.7.0 // 不同版本会有差异, 这个需要注意 (https://github.com/mfussenegger/nvim-dap/wiki/C-C---Rust-(via--codelldb))
nvim-dap // vim 插件 (https://github.com/mfussenegger/nvim-dap)
nvim-dap-virtual-text // vim 插件 (https://github.com/theHamsta/nvim-dap-virtual-text)
rcarriga/nvim-dap-ui // vim 插件 (https://github.com/rcarriga/nvim-dap-ui)

准备

  1. 安装vim插件
Plug 'mfussenegger/nvim-dap' 
Plug 'theHamsta/nvim-dap-virtual-text' 
Plug 'rcarriga/nvim-dap-ui' 

2. 安装codelldb

# 找到自己系统对应的版本(我这里使用1.7.0版)
https://github.com/vadimcn/vscode-lldb/releases
在这里插入图片描述
在这里插入图片描述
# 解压
unzip -d ~/.config/nvim/data/debug/tools/ ~/.config/nvim/data/debug/codelldb-x86_64-darwin.vsix

3. 配置文件

  • vim
local dap = require("dap")
local cmd = os.getenv('HOME') .. '/.config/nvim/data/debug/tools/extension/adapter/codelldb'
dap.adapters.codelldb = function(on_adapter)
  -- This asks the system for a free port
  local tcp = vim.loop.new_tcp()
  tcp:bind('127.0.0.1', 0)
  local port = tcp:getsockname().port
  tcp:shutdown()
  tcp:close()

  -- Start codelldb with the port
  local stdout = vim.loop.new_pipe(false)
  local stderr = vim.loop.new_pipe(false)
  local opts = {
    stdio = {nil, stdout, stderr},
    args = {'--port', tostring(port)},
  }
  local handle
  local pid_or_err
  handle, pid_or_err = vim.loop.spawn(cmd, opts, function(code)
    stdout:close()
    stderr:close()
    handle:close()
    if code ~= 0 then
      print("codelldb exited with code", code)
    end
  end)
  if not handle then
    vim.notify("Error running codelldb: " .. tostring(pid_or_err), vim.log.levels.ERROR)
    stdout:close()
    stderr:close()
    return
  end
  vim.notify('codelldb started. pid=' .. pid_or_err)
  stderr:read_start(function(err, chunk)
    assert(not err, err)
    if chunk then
      vim.schedule(function()
        require("dap.repl").append(chunk)
      end)
    end
  end)
  local adapter = {
    type = 'server',
    host = '127.0.0.1',
    port = port
  }
  -- 💀
  -- Wait for codelldb to get ready and start listening before telling nvim-dap to connect
  -- If you get connect errors, try to increase 500 to a higher value, or check the stderr (Open the REPL)
  vim.defer_fn(function() on_adapter(adapter) end, 500)
end

dap.configurations.cpp = {
  {
    name = "Launch file",
    type = "codelldb",
    request = "launch",
    program = function()
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
    end,
    pid = function()
            local handle = io.popen('pgrep hw$')
            local result = handle:read()
            handle:close()
            return result
    end,
    cwd = '${workspaceFolder}',
    stopOnEntry = true,
    terminal = 'integrated',
  },
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp

使用

  • 编译测试代码
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=DEBUG ..
make
  • 打断点
在这里插入图片描述
在这里插入图片描述
  • 运行
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 环境
  • 准备
  • 使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档