如何将 cscope 用于具有 .c 、.cpp 和 .h 文件的项目

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

本文关键字:cpp 文件 项目 cscope 用于      更新时间:2023-10-16

我正在做一个需要了解llvm编译器源代码的项目。为了浏览llvm的源代码,我尝试在源代码的根目录中使用带有以下命令的cscope:

cscope -R *

但它不起作用。由于主要有.cpp和.h文件,但也有一些.c文件。所以现在我不知道如何让cscope工作?有人可以帮忙吗?

您可以使用以下命令从 llvm 源代码树的根目录执行所需的任务:

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

它将创建与cscope一起使用的cscope.out文件来浏览代码。希望对您有所帮助!

列出项目中所有C++文件的便捷方法是使用 ack 工具:一个针对源代码搜索优化的类似 grep 的命令(在某些发行版中,例如 Ubuntu,该工具称为 ack-grep )。你可以像这样运行它:

ack -f --cpp > cscope.files

输出是所有.cpp.h.cc .hpp文件的路径

只是因为这仍然是最受欢迎的条目。stdin thingy 可能是在此期间添加的,也可能不是,但它使它有点优雅:

find -regex '.*.(c|h|cpp|cxx|hh|hpp|hxx)$' | cscope -i- -b -q

我的 .bashrc 中有以下内容,这使事情变得更容易。运行cscope_build()以生成数据库,cscope以启动cscope工具。

# 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"

为了覆盖我们的大型代码库,我有一个看起来有点像这样的脚本来构建 cscope 索引。我更改为/的原因是,我拥有源文件的完整文件路径,这使事情更加顺畅。

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

也值得一试http://cscope.sourceforge.net/cscope_vim_tutorial.html