cscope 或 ctags 为什么选择一个? [英] cscope or ctags why choose one over the other?
问题描述
我主要使用 vim/gvim 作为编辑器,并且正在考虑使用 lxr(Linux 交叉参考) 和 cscope 或 ctags 用于探索内核源代码.但是,我从未使用过 cscope 或 ctags 并想听听为什么考虑到我将 vim 用作主要编辑器,人们会选择一种而不是另一种.
I primarily use vim / gvim as an editor and am looking at using a combination of lxr (the Linux Cross Reference) and either cscope or ctags for exploring the kernel source. However, I haven't ever used either cscope or ctags and would like to hear why one might choose one over the other taking into consideration my use of vim as a primary editor.
推荐答案
ctags 启用了两个功能:允许您从函数调用跳转到它们的定义,以及全方位完成.第一个意思是当你调用一个方法时,点击 g]
或 CTRL-]
将跳转到定义或实现该方法的地方.第二个功能是当你输入foo.
或foo->
时,如果foo 是一个结构体,那么会显示一个带有字段补全的弹出菜单.
ctags enables two features: allowing you to jump from function calls to their definitions, and omni completion. The first means that when you are over a call to a method, hitting g]
or CTRL-]
will jump to the place where that method is defined or implemented. The second feature means that when you type foo.
or foo->
, and if foo is a structure, then a pop-up menu with field completion will be shown.
cscope 也有第一个功能 - 使用 set cscopetag
- 但不是最后一个.然而,cscope 还增加了跳转到调用函数的任何位置的能力.
cscope also has the first feature - using set cscopetag
- but not the last. However cscope additionally adds the ability to jump to any of the places where a function is called as well.
因此,就跳转代码库而言,ctags 只会将您带到实现函数的位置,而 cscope 也可以显示函数被调用的位置.
So as far as jumping around a code base is concerned, ctags will only ever lead you towards the place where the function is implemented, whereas cscope can show you where a function is called too.
你为什么要选择一个?嗯,我两个都用.ctags 更容易设置,运行速度更快,如果您只关心以一种方式跳跃,它会显示更少的行.你可以只运行 :!ctags -R .
和 g]
就可以了.它还可以实现全方位的功能.
Why would you choose one over the other? Well, I use both. ctags is easier to set up, faster to run and if you only care about jumping one way it will show you less lines. You can just run :!ctags -R .
and g]
just works. It also enables that omni complete thing.
Cscope 非常适合更大的未知代码库.设置很痛苦,因为 cscope 需要一个包含要解析的文件名称列表的文件.同样在 vim 中,默认情况下没有设置键绑定 - 您需要手动运行 :cscope blah blah
.
Cscope is great for bigger, unknown code bases. The set up is a pain because cscope needs a file containing a list of names of files to parse. Also in vim, by default there are no key bindings set up - you need to run :cscope blah blah
manually.
为了解决第一个问题,我有一个 bash 脚本 cscope_gen.sh
看起来像这样:
To solve the fist problem I've got a bash script cscope_gen.sh
that looks like this:
#!/bin/sh
find . -name '*.py'
-o -name '*.java'
-o -iname '*.[CH]'
-o -name '*.cpp'
-o -name '*.cc'
-o -name '*.hpp'
> cscope.files
# -b: just build
# -q: create inverted index
cscope -b -q
这将搜索我感兴趣的代码,创建 cscope.files 列表并创建数据库.这样我就可以运行 ":!cscope_gen.sh" 而不必记住所有设置步骤.
This searches for code that I'm interested in, creates the cscope.files list and creates the database. That way I can run ":!cscope_gen.sh" instead of having to remember all the set up steps.
我使用这个片段将 cscope 搜索映射到 ctrl-space x 2,这减轻了 cscope 的另一个问题:
I map cscope search to ctrl-space x 2 with this snippet, which mitigates the other downer of cscope:
nmap <C-@><C-@> :cs find s <C-R>=expand("<cword>")<CR><CR>
这个 cscope_maps.vim 插件 设置了一堆类似的绑定.我永远不记得所有选项的含义,所以倾向于使用 ctrl-space.
There's this cscope_maps.vim plugin that sets up a bunch of similar bindings. I can never remember what all the options mean, so tend to stick to ctrl-space.
所以总结一下:ctags 更容易设置并且大多数情况下无需做太多其他工作,这对于全向完成也很重要.如果您必须维护大型且大部分未知的代码库,cscope 会提供更多功能,但需要更多的工作.
So to conclude: ctags is easier to set up and mostly works without doing much else, it's vital for omni-complete too. cscope provides more features if you have to maintain a large and mostly unknown code base, but requires more leg work.
这篇关于cscope 或 ctags 为什么选择一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!