cscope or ctags why choose one over the other?

73,713

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Share:
73,713
Robert S. Barnes
Author by

Robert S. Barnes

I'm a self taught programmer whose taken a break to go back to school and study Software Engineering. Currently, my main area's of interest are network programming and application level protocols, object oriented design and methodologies like Agile and TDD. SOreadytohelp

Updated on November 30, 2020

Comments

  • Robert S. Barnes
    Robert S. Barnes over 3 years

    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.

  • Robert S. Barnes
    Robert S. Barnes almost 15 years
    Is there any way to make ctags more accurate? I did make tags in the kernel root dir and have been playing with jumping around and most of the time end up in the wrong place. I read that ctags has problems with the c preprocessor, but considering that ctags is used in lxr there obviously must be something that can be done.
  • Ben Moss
    Ben Moss almost 15 years
    If there is deep macro voodoo, then ctags will likely fail :-( I use it mostly for C++ stuff, which relies less on that side of things (though that has its own problems...)
  • Samvel Aleqsanyan
    Samvel Aleqsanyan almost 15 years
    I fully agree with that... the linux kernel is full of weird macros and ctags fail... that's why i switched towards cscope
  • Robert S. Barnes
    Robert S. Barnes over 14 years
    Isn't etags just for emacs? I use g/vim exclusively.
  • rmk
    rmk over 14 years
    Yes, you are right. From the man page: The etags program is used to create a tag table file, in a format understood by emacs(1); the ctags program is used to create a similar table in a format understood by vi(1).
  • Hasturkun
    Hasturkun almost 14 years
    set cscopetag (cst) to make the :tag and CTRL-] commands search through cscope first, then tags
  • Ben Moss
    Ben Moss almost 14 years
    @Hasturkun - cool, will try that and update the post...
  • Claudio Pierard
    Claudio Pierard almost 14 years
    there are two flavors of etags and ctags I believe. One is the emacs one, the other is the exuberant ctags one. The first was first written for emacs, but can be used for vi too; the second was first written for vi, but can be used for emacs too. I find the second one (exuberant ctags) easier to use, even though I am an emacs user. If you install the exuberant-ctags package, the links to etags/ctags binaries will change, and point to different binaries.
  • Ofer
    Ofer over 13 years
    Also, ctags is pretty slow at recursive searching, using "ctags -L cscope.files" will significantly speed up your tag generation.
  • J. Polfer
    J. Polfer over 13 years
    BTW - I think its pretty lame that cscope doesn't do what your script does natively. It can do it for c/h files... why not give it a list of file extensions to index. Anyway, thx for posting your script.
  • Hubert Kario
    Hubert Kario almost 12 years
    @RobertS.Barnes some kind of solution is using g C-], vim will show you the list of tags matching the name. You'll still need to find the proper definition you're looking for manually.
  • kumar
    kumar over 9 years
    <<you just type cscope -R at the root of your kernel .... Better to type "make cscope" under kernel, otherwise you will end up with all Architectures present in Linux kernel and hence multiple definition to same C symbol.
  • cifer
    cifer about 9 years
    it seems that cscope's tag feature don't support jumping back to function call when you had pressed Ctrl + ]
  • Ben Moss
    Ben Moss about 9 years
    @Cifer after jumping to the function then Ctrl + o does return to the previous location, or did you mean something else?
  • cifer
    cifer over 8 years
    @richq I meant return directly to where the function be called. Ctrl + o will just return to the very last location, which may inconvenient if you jump into the function and move your cursor up and down
  • 71GA
    71GA about 3 years
    is cscope even more precise than exuberant-ctags or not? I heard that later is an improved ctags...