How to use cscope for a project which has .c , .cpp and .h files?
Asked Answered
P

5

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?

Phalansterian answered 11/8, 2012 at 21:47 Comment(0)
A
13

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!

Arcadia answered 11/8, 2012 at 21:55 Comment(5)
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.Unreliable
Yeah u are right. Actually i wrote it to make things easy to understand. But anyway its not the wise thing to do.Arcadia
cscope -R' or cscope $(find -iregex '.+\.[chp]+')Insobriety
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 by default, which need more backslashes than usual, and the pattern must match the entire line, hence the .* necessary at the beginning.Benavides
Note that you need to add quotes ("...") around the file paths in the tags.lst in case they contain spaces somewhere.Maugre
M
8

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

Maccarone answered 22/8, 2014 at 15:58 Comment(1)
Just for you lazy users like me: 'sudo apt-get install ack' gets me: 'ack - Kanji code converter', not the same utility. Reading the beyondgrep.com/install page for 20 seconds and I find it is 'sudo apt-get install ack-grep'Cyndy
S
5

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"
Sprouse answered 24/9, 2017 at 5:55 Comment(0)
A
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
Aq answered 5/7, 2017 at 20:38 Comment(0)
L
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

Leaving answered 11/8, 2012 at 21:54 Comment(1)
Thank you for your answer! Just doing find -L -name "*.cc" -o -name "*.h" > cscope.files worked for me.Remorse

© 2022 - 2024 — McMap. All rights reserved.