Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DAP support c

Zeioth edited this page Aug 14, 2023 · 14 revisions

In order to debug your programs, you have to compile them in debug mode as described next

Compiling C in debug mode

Create a .solution.toml file in your working directory. Then pass the -g argument to the compiler like this

[HelloWorld]
entry_point = "/path/to/my/entry_point_file/main.c"
output = "/path/where/the/program/will/be/written/hello_world"
arguments = "-g"

That will tell the compiler to compile in debug mode.

How to setup DAP to debug with C

Please note that this section has nothing to do with compiler.nvim. I'm documenting this to make your life easier. To debug C with DAP you have to:

Here you have an example of how to configure DAP for C

local dap = require("dap")

-- C
dap.adapters.codelldb = {
  type = 'server',
  port = "${port}",
  executable = {
    command = vim.fn.stdpath('data')..'/mason/bin/codelldb',
    args = {"--port", "${port}"},
    detached = function() if vim.fn.has('win32') == 1 then return false else return true end end,
  }
}
dap.configurations.c = {
  {
    name = 'Launch',
    type = 'lldb',
    request = 'launch',
    program = function() -- Ask the user what executable wants to debug
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/bin/program', 'file')
    end,
    cwd = '${workspaceFolder}',
    stopOnEntry = false,
    args = {},
  },
}

How to check if everything works

Compile your program with Build solution, add a break point to your code, and then run DAP. You will see something like this.

screenshot_2023-08-08_02-45-21_084814406

Important for you to know

  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.
  • Update 11/08/2023: Passing arguments in a .solution.toml file is not required anymore, as by default Compiler.nvim now passes the -g argument to the C compiler (gcc) to enable debug mode. But if you create a .solution.toml file in your project you will still have to set the argument manually, as that will overwrite the default compiler arguments with your own.

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.