你在 Vim 中调试 C++ 代码吗?如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3536600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do you debug C++ code in Vim? How?
提问by Andrew_Lvov
The question is to all you people, who use Vim to develop C++ applications.
问题在于你们所有使用 Vim 开发 C++ 应用程序的人。
There was a period in my life, which can be described as 'I hate Vim!!!'..'Vim is nice!'
在我的生命中有一段时期,可以用“我讨厌 Vim !!!”..“Vim 很好!”来形容。
However, having grown up mostly on Microsoft development IDEs, I've got used to those F5-F11shortcuts when debugging code, watch window, call stack and the main code - all visible without need to type any GDB commands.
然而,我主要是在 Microsoft 开发 IDE 上成长起来的,我已经习惯了这些F5-F11调试代码时的快捷方式、观察窗口、调用堆栈和主代码 - 所有这些都无需键入任何 GDB 命令即可看到。
So, here is the question:
那么,问题来了:
Do you use Vim as well for debugging? Or do you switch to some IDE for this purpose? Which one?
您是否也使用 Vim 进行调试?或者您是否为此目的切换到某些 IDE?哪一个?
For those who use Vim to debug code: are there plugins to set breakpoints in editor, highlight the line we're currently debugging, auto-navigation during step, step into, step out?
对于那些使用 Vim 调试代码的人:是否有插件可以在编辑器中设置断点,突出显示我们当前正在调试的行,步骤中的自动导航,步入,步出?
Please, don't tell me you use GDB as command line, see only one line which is debugged, etc.
请不要告诉我你使用 GDB 作为命令行,只看到一行被调试等。
采纳答案by UncleZeiv
In contrast with the other answers, there are at least three options that do just what you require: clewn, pyclewnand vimgdb.
与其他答案相比,至少有三个选项可以满足您的要求:clewn、pyclewn和vimgdb。
All three projects are related. vimgdbis a patch against Vim and requires Vim to be recompiled. clewnis 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).
这三个项目都是相关的。vimgdb是一个针对 Vim 的补丁,需要重新编译 Vim。clewn是一个独立的程序,它通过 Netbeans 套接字接口与 Vim 通信。这需要使用该+netbeans
选项构建 Vim (在最近的 Linux 发行版中就是这种情况,所以它应该不是问题)。
To quote from the clewn's website:
引用 clewn 的网站:
Clewn implements full gdb support in the vim editor: breakpoints, watch variables, gdb command completion, assembly windows, etc.
Clewn 在 vim 编辑器中实现了完整的 gdb 支持:断点、观察变量、gdb 命令完成、汇编窗口等。
I think you should definitely give it a go.
我认为你绝对应该试一试。
The homepage of the pyclewn website shows a comparison between the three projects.
pyclewn 网站首页展示了三个项目的对比。
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.
几个月前,我尝试了 pyclewn。设置起来有点困难,但看起来不错,但很有前途。我只是做了一些测试,您可以设置书签等,这是您期望从图形调试器中获得的常见功能。由于偶然原因,我最终没有使用它,但我很想再试一次。
回答by Daniel S.
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.
Vim 在 2018 年 5 月发布的 8.1 版本中正式添加了一个内置调试器。 该功能早在 2017 年 8 月的一些 8.0 版本中就已经存在。
The following vim commands load the plugin and start the debugger.
以下 vim 命令加载插件并启动调试器。
: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.
后一个命令将程序作为可选参数,或者可以gdb
使用file
命令从窗口加载程序。
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.
加载插件后,gdb
可以在相应的窗口中交互使用。例如,可以设置断点,可以单步执行代码,可以检查变量。
Vim commands can be issued for interacting with gdb
. Some relevant commands include :Step
, :Over
, :Finish
, :Continue
, :Stop
, :Break
, :Clear
, and :Evaluate
.
可以发出 Vim 命令以与gdb
. 一些相关的命令包括:Step
,:Over
,:Finish
,:Continue
,:Stop
,:Break
,:Clear
,和:Evaluate
。
Additionally, there are clickable buttons at the top of the editor window for interacting with gdb
.
此外,编辑器窗口顶部还有可点击的按钮,用于与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.
我最近写了一篇博客文章,介绍了一个示例会话。
回答by Johan
Vim is a nice editor, but to do debugging I use a debugger (like GDB).
Vim 是一个不错的编辑器,但是为了进行调试,我使用了调试器(如 GDB)。
But you don't have to use GDB in text mode; you can use a graphical frontend like KDbg, DDDor Insight.
但是您不必在文本模式下使用 GDB;您可以使用图形前端,如KDbg、DDD或Insight。
There are ways of getting GDB into Vim (but then you do get text based debugging).
有很多方法可以让 GDB 进入 Vim(但是你确实会得到基于文本的调试)。
回答by nobar
Using a source level debugger is only one of many ways to diagnose faulty program behavior, and I rarely find myself launching one -- despite the fact that it is very easy to do.
使用源代码级调试器只是诊断错误程序行为的众多方法中的一种,我很少发现自己启动了一种方法——尽管它很容易做到。
So for me, there is simply no inherent advantage to using a text editor that happens to also be a debugger. Instead, I use the text editor that I prefer -- independent of what debugger I choose to use. At the moment, I mostly use geditand kdbgfor these purposes, but these choices evolve independently over time.
所以对我来说,使用恰好也是调试器的文本编辑器并没有固有的优势。相反,我使用我喜欢的文本编辑器——与我选择使用的调试器无关。目前,我主要将gedit和kdbg用于这些目的,但这些选择会随着时间的推移而独立发展。
回答by Shawn D.
Having just recently worked on an application for a long time that required a bunch of stuff to be in place on the box it was running (appliance set up), I wrote code in vim, had scripts which automated building, pushing it to a server, which had a script there to notice the sentinel file pushed along with the binaries. This would then restart the appropriate services on the box, and in another ssh window I had a tail -f
running on my log file.
最近刚刚在一个应用程序上工作了很长时间,需要在它运行的机器上放置一堆东西(设备设置),我在 vim 中编写了代码,有自动构建的脚本,将其推送到服务器,其中有一个脚本可以通知随二进制文件一起推送的哨兵文件。这将重新启动盒子上的相应服务,并在另一个 ssh 窗口中tail -f
运行我的日志文件。
Long story short, I didn't use a debugger at all. If I had something die unexpectedly, I'd just bump up logging levels, redo it, and see what was the last thing logged before it died, then analyze that and fix the issue.
长话短说,我根本没有使用调试器。如果我有某些东西意外死亡,我只会提高日志记录级别,重做它,看看它死亡之前记录的最后一件事是什么,然后分析并解决问题。
The nice thing was that when something had problems in a customer environment, I'd just ask for a Debug-level log and could identify the issue without even requiring access to their server.
好消息是,当客户环境中出现问题时,我只需要一个调试级别的日志,就可以识别问题,甚至不需要访问他们的服务器。
... but yes, there were times when it would have been nice to have a debugger.
...但是,有时候有一个调试器会很好。
回答by Tinmarino
Update 2020: There is a new plugin vimspectorusing the Debug Adapter Protocol
2020 年更新:有一个使用调试适配器协议的新插件vimspector
Install the plugin https://github.com/puremourning/vimspector#installation
Configure (write
.vimspector.json
)Compile with debug symbol
g++ cpp.cpp -ggdb -o cpp
Press
F4
to start debug
配置(写入
.vimspector.json
)用调试符号编译
g++ cpp.cpp -ggdb -o cpp
按
F4
开始调试
- Note my
.vimspector.json
in my home directory (so work in any subdir)
- 注意我
.vimspector.json
在我的主目录中(所以在任何子目录中工作)
{
"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"
}
}
} }
回答by Jimmy MG Lim
Just to add to above :
只是添加到上面:
IMO vim tends to be quite a light editor and debugging tends to add to the weight. There are ways to do it i.e. using vim7.4+ with
IMO vim 往往是一个相当轻的编辑器,调试往往会增加重量。有办法做到这一点,即使用 vim7.4+
:terminal
and running one of the following commandline (curses) debuggers. A few are used by default for IDEs that you never knew. i.e. lldb = xcode.
并运行以下命令行(curses)调试器之一。一些默认情况下用于您不知道的 IDE。即 lldb = xcode。
- (c/c++/objective c) gdb (gnu debugger)- https://www.gnu.org/software/gdb/
- (c/c++/objective c/python etc) remake- http://bashdb.sourceforge.net/remake/
- (java) oracle java debugger- https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html
- (c/c++/objective c) lldb- https://lldb.llvm.org/
- (c/c++/objective c) gdb (gnu 调试器)- https://www.gnu.org/software/gdb/
- (c/c++/objective c/python 等) remake- http://bashdb.sourceforge.net/remake/
- (java) oracle java 调试器- https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html
- (c/c++/objective c) lldb- https://lldb.llvm.org/
obviously there are more cli based ones; @all feel free to suggest and add to list. thanks!
显然有更多基于 cli 的;@all 随时提出建议并添加到列表中。谢谢!