8

I am working on a project which requires the understanding of llvm compiler source-code. To browse source code of llvm, I tried to use cscope with following command in the root directory of the source:

cscope -R *

But it doesn't work. As there are mainly .cpp and .h files but some .c files are also there. So now I don't have a clue how to make cscope work? Can someone please help?

5 Answers5

12

You can use following commands to do the required task from the root directory of llvm source tree:

touch tags.lst
find | grep "\.c$" >> tags.lst
find | grep "\.cpp$" >> tags.lst
find | grep "\.h$" >> tags.lst
cscope -i tags.lst

It would create cscope.out file which is used with cscope to browse the code. Hope it helps!

leonidus
  • 246
  • 1
  • 7
  • 11
    Doing 3 lots of `find | grep` is a bit wasteful. You could perfectly well use `egrep` or `grep -E` with a single `find`: `find . -type f -print | grep -E '\.(c(pp)?|h)$' > cscope.files`. – Jonathan Leffler Aug 11 '12 at 21:59
  • 3
    Yeah u are right. Actually i wrote it to make things easy to understand. But anyway its not the wise thing to do. – leonidus Aug 11 '12 at 22:01
  • 6
    `cscope -R'` or `cscope $(find -iregex '.+\.[chp]+')` – Haix64 Nov 04 '13 at 13:29
  • 1
    I almost feel silly adding this, because there's really nothing qualitatively wrong with any of the other solutions, and I'm not normally one of those "but a pipe costs a process!" people, but `find` finds files matching a pattern just fine: `find . -regex '.*\.\(c\|cpp\|h\)$'`. You just have to know that GNU `find` uses [Emacs-style REs](https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Backslash.html) by default, which need more backslashes than usual, and the pattern must match the _entire_ line, hence the `.*` necessary at the beginning. – TheDudeAbides Feb 20 '19 at 00:47
  • Note that you need to add quotes (`"..."`) around the file paths in the `tags.lst` in case they contain spaces somewhere. – luator Jul 09 '19 at 08:22
7

A convenient way to list all C++ files in a project is to use the ack tool: a grep-like command optimized for source code searching (In some distributions, for instance Ubuntu, the tool is called ack-grep). You can run it like this:

ack -f --cpp > cscope.files

The output are paths to all .cpp, .h, .cc .hpp files

Jan Wrobel
  • 6,969
  • 3
  • 37
  • 53
  • 3
    Just for you lazy users like me: 'sudo apt-get install ack' gets me: 'ack - Kanji code converter', not the same utility. Reading the http://beyondgrep.com/install/ page for 20 seconds and I find it is 'sudo apt-get install ack-grep' – qneill Feb 10 '15 at 16:44
4

Just because this is still the most popular entry. The stdin thingy may have been added in the meantime or not, but it makes it kind of elegant:

find -regex '.*\.\(c\|h\|cpp\|cxx\|hh\|hpp\|hxx\)$' | cscope -i- -b -q
MaxC
  • 540
  • 5
  • 14
4

I have following in my .bashrc which make things easier. Run cscope_build() to generate data base and cscope to start cscope tool.

# Use vim to edit files
export CSCOPE_EDITOR=`which vim`

# Generate cscope database
function cscope_build() {
  # Generate a list of all source files starting from the current directory
  # The -o means logical or
  find . -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.hh" -o -name "*.hpp" > cscope.files
  # -q build fast but larger database
  # -R search symbols recursively
  # -b build the database only, don't fire cscope
  # -i file that contains list of file paths to be processed
  # This will generate a few cscope.* files
  cscope -q -R -b -i cscope.files
  # Temporary files, remove them
  # rm -f cscope.files cscope.in.out cscope.po.out
  echo "The cscope database is generated"
}
# -d don't build database, use kscope_generate explicitly
alias cscope="cscope -d"
Validus Oculus
  • 2,756
  • 1
  • 25
  • 34
2

To cover our large code base I have a script that looks a bit like this to build cscope indexes. The reason I change to / is so that I have full file paths to the source files which makes things work a little smoother.

cd /
find -L /home/adrianc/code -name "*.c" -o -name "*.cc" -o -name "*.h" > /home/adrianc/code/cscope.files
cd /home/adrianc/code
/usr/local/bin/cscope -b -icscope.files -q -u

Also it may be worth checking out http://cscope.sourceforge.net/cscope_vim_tutorial.html

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77