Vim配置C++ debug环境

Vim配置C++ debug环境,第1张

文章目录
  • 环境
  • 准备
  • 使用

环境

我的配置地址: C++ class="superseo">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)
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' 
  1. 安装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
  1. 配置文件
  • 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
  • 打断点

  • 运行


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1295067.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存