Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you debug C++ code in Vim? How? [closed]

People also ask

Is it possible to debug in Vim?

Vim added a built-in debugger officially in version 8.1, released in May 2018. The feature had been present in some of the version 8.0 releases as well, as early as August 2017. The following vim commands load the plugin and start the debugger.

What do you place down in order to stop at a line of code to debug?

Step into code line by line To stop on each statement when you're debugging, use Debug > Step Into, or select F11. The debugger steps through code statements, not physical lines. For example, an if clause can be written on one line: C#

Can you debug C in Visual Studio?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)


In contrast with the other answers, there are at least three options that do just what you require: clewn, pyclewn and vimgdb.

All three projects are related. vimgdb is a patch against Vim and requires Vim to be recompiled. clewn is a standalone program that communicates with Vim through the Netbeans socket interface. This requires Vim to be built with the +netbeans option (this is the case in recent Linux distributions so it shouldn't be a problem).

To quote from the clewn's website:

Clewn implements full gdb support in the vim editor: breakpoints, watch variables, gdb command completion, assembly windows, etc.

I think you should definitely give it a go.

The homepage of the pyclewn website shows a comparison between the three projects.

A few months ago I tried pyclewn. It was a bit difficult to set up, but it looks well though out and promising. I just did some tests and you could set bookmarks, etc., the usual stuff you would expect from a graphical debugger. I ended up not using it for contingent reasons but I am keen to give it another try.


Vim added a built-in debugger officially in version 8.1, released in May 2018. The feature had been present in some of the version 8.0 releases as well, as early as August 2017.

The following vim commands load the plugin and start the debugger.

:packadd termdebug
:Termdebug

The latter command takes a program as an optional argument, or alternatively a program can be loaded from the gdb window with the file command.

With the plugin loaded, gdb can be used interactively in the corresponding window. For example, breakpoints can be set, code can be stepped through, and variables can be inspected.

Vim commands can be issued for interacting with gdb. Some relevant commands include :Step, :Over, :Finish, :Continue, :Stop, :Break, :Clear, and :Evaluate.

Additionally, there are clickable buttons at the top of the editor window for interacting with gdb.

The editor window is updated to reflect the state of debugging. Breakpoints are indicated with >> and the current line is highlighted.

The built-in help page includes thorough documentation.

:help terminal-debug

I recently wrote a blog post that walks through an example session.

https://www.dannyadam.com/blog/2019/05/debugging-in-vim/


Update 2020: There is a new plugin vimspector using the Debug Adapter Protocol

  1. Install the plugin https://github.com/puremourning/vimspector#installation

  2. Configure (write .vimspector.json)

  3. Compile with debug symbol g++ cpp.cpp -ggdb -o cpp

  4. Press F4 to start debug

enter image description here

  • Note my .vimspector.json in my home directory (so work in any subdir)
{
"configurations": {
  "Python - Launch": {
    "adapter": "vscode-python",
    "configuration": {
      "name": "Python: Launch current file",
      "type": "python",
      "request": "launch",
      "stopOnEntry": true,
      "stopAtEntry": true,
      "console": "externalTerminal",
      "debugOptions": [],
      "cwd": "${cwd}",
      "program": "${file}"
    }
  },
  "Perl - Launch": {
    "adapter": "vscode-perl-debug",
    "configuration": {
      "name": "Perl: Launch current file",
      "type": "perl",
      "request": "launch",
      "exec": "/usr/bin/env perl",
      "execArgs": [],
      "stopOnEntry": true,
      "stopAtEntry": true,
      "console": "externalTerminal",
      "sessions": "single",
      "debugOptions": [],
      "cwd": "${cwd}",
      "program": "${file}"
    }
  },
  "C - Launch": {
    "adapter": "vscode-cpptools",
    "configuration": {
      "name": "Cpp: Launch current file",
      "type": "cppdbg",
      "request": "launch",
      "externalConsole": true,
      "logging": {
        "engineLogging": true
      },
      "stopOnEntry": true,
      "stopAtEntry": true,
      "debugOptions": [],
      "MIMode": "gdb",
      "cwd": "${cwd}",
      "program": "${fileDirname}/${fileBasenameNoExtension}"
    }
  },
  "Java - Launch": {
    "adapter": "vscode-java",
    "configuration": {
      "name": "Java: Launch current file",
      "request": "launch",
      "mainClass": "com.vimspector.test.TestApplication",
      "sourcePaths": [ "${workspaceRoot}/src/main/java" ],
      "classPaths": [ "${workspaceRoot}/target/classes" ],
      "args": "hello world!",
      "stopOnEntry": true,
      "console": "integratedTerminal"
    }
  }
} }

Vim is a nice editor, but to do debugging I use a debugger (like GDB).

But you don't have to use GDB in text mode; you can use a graphical frontend like KDbg, DDD or Insight.

There are ways of getting GDB into Vim (but then you do get text based debugging).


GDB edit command

Opens an editor on the current line using the command:

$EDITOR +<current-line> <current-file>

The default editor is ex, but vim also understands the +<current-line> format.

When you quit the editor, you get back into gdb.

This allows you to browse the source freely and is specially powerfull if you have ctags integration.

This is a poor-man's built-in one way gdb to vim integration: the main missing thing is setting breakpoints from Vim.

edit and center

edit does not center Vim by default around the source, so I've created a Python script that does it: How to open the current file at the current line in a text editor from GDB?

Breakpoint command to clipboard helper

This vim command copies a breakpoint specifier of type:

b <file-path>:<line-number>

to the clipboard:

command! Xg :let @+ = 'b ' . expand('%:p') . ':' . line('.')

Then you can just paste that into gdb.

This is a poor man's vim to gdb integration to ease setting breakpoints.

GDB Dashboard

https://github.com/cyrus-and/gdb-dashboard

This has nothing to do with Vim, but it is a lightweight solution that achieves a lot and might suit other Vimmers out there.

Others have mentioned GDB TUI, but I found it too broken and not powerful enough to be bearable.

So I moved instead to Python API based solutions such as GDB Dashboard.

I have described used and rationale in more detail at: gdb split view with code

Here is a screenshot of what it gives you:

enter image description here

See also: https://vi.stackexchange.com/questions/2046/how-can-i-integrate-gdb-with-vim

Give up and use a real IDE

With all that said, this is the best solution for most people, including myself. Most people will just gain tons of time if they are able to jump around definitions in a C++ class aware manner without selecting and installing several different plugins themselves, and that includes while step debugging stuff. As of 2020 the least worst one for me was Eclipse: https://www.slant.co/topics/1411/~best-ides-for-c-on-linux