问题描述
我主要使用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为什么要选择一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!