How to install and use cscope

Cscope is a classic source code explorer that can be used to explore a directory containing C source and header files. It can also be applied on C++ files, but do not expect much intelligence on C++ features. It indexes the source files, just like ctags, but provides more useful ways to query the code. It provides a simple commandline interface to query the codebase. You might want to configure Vim to use cscope for a more user-friendly interface.

  • Installing cscope is easy:
$ sudo apt install cscope
  • To recursively scan and index all C source and header files in a directory and bring up the interface to explore it:
$ cscope -R

This will produce a cscope.out file which is where the symbol cross-references are stored. In the interface, you can look up symbols, definition of a symbol, all instances of a function call and even do find-replace. To exit the interface, press Ctrl-D.

  • To only produce the cross-reference file:
$ cscope -R -b
  • To request cscope to write cross-reference file to a different filename:
$ cscope -b -f .some_cscope_file
  • To request cscope to index only files you want, list them in a namefile that is named cscope.files and invoke:
$ cscope -i -b
  • To use a different namefile:
$ cscope -i some_namefile -b
  • To be able to index C++ or CUDA files, we need to be able to list them out in a namefile. This can be done using the find command and we can ask cscope to use that namefile:
$ find -iregex '.*\.\(h\|hpp\|c\|cpp\|cu\)$' > .files_for_cscope
$ cscope -i .files_for_cscope -b

If you use these commands frequently, then wrap them up in a shell function for ease of use.

Tried with: Cscope 15.8b and Ubuntu 16.04

One thought on “How to install and use cscope

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.