cscope 或 ctags 为什么选择其中之一?

发布于 2024-07-22 21:44:54 字数 1542 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゞ记忆︶ㄣ 2024-07-29 21:44:54

ctags 支持两个功能:允许您从函数调用跳转到其定义,以及全方位完成。 第一个意味着当您结束对方法的调用时,点击 g]CTRL-] 将跳转到定义或实现该方法的位置。 第二个功能意味着当您输入 foo.foo-> 时,如果 foo 是一个结构体,则会显示一个带有字段完成功能的弹出菜单。

cscope 还具有第一个功能 - 使用 set cscopetag - 但不是最后一个。 然而,cscope 还增加了跳转到调用函数的任何位置的能力。

因此,就跳转代码库而言,ctags 只会引导您到达函数实现的位置,而 cscope 也可以显示函数被调用的位置。

为什么你会选择其中之一而不是另一个? 嗯,我两者都用。 ctags 更容易设置,运行速度更快,如果您只关心以一种方式跳转,它会显示更少的行。 您只需运行 :!ctags -R 即可。g] 就可以了。 它还可以实现全方位的完整功能。

Cscope 非常适合更大的、未知的代码库。 设置很痛苦,因为 cscope 需要一个包含要解析的文件名列表的文件。 同样在 vim 中,默认情况下没有设置键绑定 - 您需要手动运行 :cscope blah blah

为了解决第一个问题,我有一个 bash 脚本 cscope_gen.sh,如下所示:

#!/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”,而不必记住所有设置步骤。

我使用此代码片段将 cscope 搜索映射到 ctrl-space x 2,这减轻了 cscope 的另一个缺点:

nmap <C-@><C-@> :cs find s <C-R>=expand("<cword>")<CR><CR>

There's this cscope_maps .vim 插件 设置了一堆类似的绑定。 我永远记不起所有选项的含义,所以倾向于坚持使用 ctrl-space。

总而言之:ctags 更容易设置,并且大多数情况下无需执行太多其他操作即可工作,这对于全方位完成也至关重要。 如果您必须维护一个大型且大多数未知的代码库,cscope 可以提供更多功能,但需要更多的跑腿工作。

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 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.

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.

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 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.

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

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.

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>

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.

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.

我三岁 2024-07-29 21:44:54

建议使用全局 gtag。 可以使用 vim 插件 gen_tags 将 gtags 与 vim 集成。

Suggest use global gtags. Could use vim plugin gen_tags to integrate gtags with vim.

森末i 2024-07-29 21:44:54

嗯...你应该使用 etags 而不是 ctags...

如果你使用 cscope,那么你可以看到调用链,即谁调用了这个函数& 这个函数调用了哪些函数?

我不确定这是否可以使用 etags / ctags 来完成...

这只是一个功能...如何找出包含特定函数定义的文件? 这只能在 cscope 中得到。

我同时使用 cscope 和 etag,它们都有不同的用途,尤其是在使用大型代码库(例如 Linux 内核)时。 事实上,当我开始使用 Linux Kernel / Xen 时,我就开始使用 cscope 和 etags。

LXR 不是很好,因为您必须单击、通过网络等,而您可以在内核代码上构建 cscope 和标签数据库,并且不必通过网络(与 lxr 不同) )。

Hmm... You should probably use etags instead of ctags...

If you use cscope, then you can see call chains, i.e., who calls this function & which functions does this function call?

I am not sure if this can be done using etags / ctags...

That's just one feature... what about finding out the file that contains a particular function definition? This you get only in cscope.

I use both cscope and etags, they are both good for different things, especially when working with a large codebase, such as the Linux Kernel. In fact, I started using cscope and etags when I started working with the Linux Kernel / Xen.

LXR is not great, because you have to click, go over the network etc., whereas you can build the cscope and tags databases on your kernel code and do not have to go over the network (unlike lxr).

雨夜星沙 2024-07-29 21:44:54

几个月前我也遇到过同样的情况......

ctags 缺乏精度是一个痛苦......,我发现 cscope 对于所有与宏相关的东西都更好(并且 Linux 内核中有一堆宏) )..

关于用法,这实际上很简单...你只需在内核的根目录下输入 cscope -R ,然后你就没有什么可担心的了..(我的意思是如果你只是想探索那是完美的.. 。)

然后,按键绑定都是基于Ctrl-\(如果你对Ctrl过敏的话可以重新映射它),你主要使用s和g....,

为内核开发,我不需要所以完成了很多......

无论如何,去cscope,这更方便,更准确。

I was in the same situation some months ago...

The lack of precision of ctags is a pain in a.., and i find cscope much better for all the macros related stuff (and there are a bunch of macros in the linux kernel)..

concerning the usage, that's actually straightforward...you just type cscope -R at the root of your kernel and then you've got nothing to worry about.. (I mean if you just want to explore that's perfect...)

Then, the key bindings are all based on Ctrl-\ (you can remap it if you're allergic to Ctrl), you mainly use s and g....,

Developing for the kernel, I didn't need so much the completion....

Anyway, go for cscope, this is much more convenient, accurate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文