栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Vim配置C++ debug环境

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vim配置C++ debug环境

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

环境

我的配置地址: 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' 
  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
  • 打断点

  • 运行


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/920294.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号