Cscope
Table of Contents
1. Cscope
Cscope is a developer's tool for browsing source code.
对于浏览源代码来说,在 Emacs 里面也有很多工具可用,比如自带的 etags 就不错,不过功能不如 Cscope 强大。 Cscope 并不像 etags 那样属于 Emacs , 它是一个独立的程序。它是一个具有完全 Unix 血统的程序,用于帮助开发人员浏览源代码。它最初(那还是 PDP-11 的时代)是由 Bell 实验室开发,并且多年以来一直是 AT&T Unix 商业发行版的一部分。它已经被用于管理超过两千万行代码的工程。在 2000 年 4 月,多亏了 Santa Cruz Operation, Inc. (SCO), Cscope 的源代码在 BSD license 下开放了源代码。
尽管 Cscope 最初是为 C 语言而设计的,但它也能很好地分析 C++ 和 Java 源码。
2. Cscope 基本用法
默认地,Cscope 只分析当前目录下的 C 源码(.c 和.h),lex 源码(.l)和 yacc 源码(.y)。
如果想要分析 C++ 源码或子目录中的源码,请手动把文件清单放入 cscope.files 中,Cscope 会分析这个文件清单中的源码。
第一步:把想要分析的文件的清单放到文件 cscope.files 中。在源代码目录中运行下面命令:
$ find . -name "*.[ch]" >cscope.files
如果待分析源码中还有 cpp 程序,可以用下面命令:
$ find . -name "*.[ch]" -o -name "*.cpp" >cscope.files
第二步:生成索引文件。运行下面命令即可:
$ cscope -b -q
-b
Build the cross-reference only
-q
创建反向索引(可加速查找,会生成额外的文件 cscope.in.out 和 cscope.po.out)
执行完上面命令,将生成 cscope.output,cscope.in.out 和 cscope.po.out
注:不手动生成索引文件,cscope 也会自动生成索引文件 cscope.output,但不会自动生成能加快查找速度的反向索引文件。
第三步:启动 Cscope。默认地,Cscope 会自动更新索引文件。启动时指定 -d
选项可以禁止其自动更新索引文件:
$ cscope -d
说明:要退出 Cscope,请按 Ctrl+d。
2.1. cscope-indexer
cscope-indexer 是一个 Shell 脚本,用它可以方便地更新索引文件。 cscope-indexer
能自动处理 C++ 文件。
如:对当前目录中所有文件(包括子目录)生成索引
$ /usr/bin/cscope-indexer -v -r Creating list of files to index ... Creating list of files to index ... done Indexing files ... Indexing files ... done
运行成功后,在当前目录下会生成 cscope.files 和 cscope.out。
2.2. 更快地查找函数定义或引用处
通常要查找一个函数的定义,首先启动 cscope,再按 1 次 Enter 键,使光标位于"Find this global definition"处,然后输入函数名如 fun1,再按 Enter 键。其实,还有更简单的办法查找函数 fun1 的定义:
$ cscope -1 fun1
其中,数字 1 是指下面功能的索引号(第 1 个功能的索引号从数字 0 开始):
0 Find this C symbol: 1 Find this global definition: 2 Find functions called by this function: 3 Find functions calling this function: 4 Find this text string: 5 Change this text string: 6 Find this egrep pattern: 7 Find this file: 8 Find files #including this file: 9 Find all function definitions: 10 Find all symbol assignments:
又如,要查找 fun1 的引用处("Find functions calling this function"功能的索引号为 3),可以使用下面命令:
$ cscope -3 fun1
要在显示查找结果后直接退出,可以使用 -L
选项,如:
$ cscope -L -3 fun1
这样,不会进入到交互界面,显示查找结果后直接退出。
2.3. Emacs 中使用 Cscope
要在 Emacs 中使用 Cscope,需要先安装 cscope 的 Emacs 接口文件。
在 debian 系统下,可用这样安装: apt-get install cscope-el
,安装完成后,在 Emacs 启动文件中加上 (require 'xcscope)
即可。
在使用之前,Cscope 需要对代码进行索引。在 Emacs 中可以这样做:
C-c s a 设定初始化的目录,一般是你代码的根目录 C-s s I 对目录中的相关文件建立列表并进行索引
这是默认的用于查找的键绑定:
C-c s s Find symbol. C-c s d Find global definition. C-c s g Find global definition (alternate binding). C-c s G Find global definition without prompting. C-c s c Find functions calling a function. (看看指定函数被哪些函数所调用) C-c s C Find called functions (list functions called from a function). (看看指定函数调用了哪些函数) C-c s t Find text string. C-c s e Find egrep pattern. C-c s f Find a file. C-c s i Find files #including a file. (看看指定的文件被哪些文件include).
下面是在搜索到的结果之间切换用的快捷键:
C-c s b Display *cscope* buffer. C-c s B Auto display *cscope* buffer toggle. C-c s n Next symbol. C-c s N Next file. C-c s p Previous symbol. C-c s P Previous file. C-c s u Pop mark.
上面这些快捷键记不住没关系,菜单栏有一栏就是 Cscope,这些命令在菜单里都有。
2.4. GNU GLOBAL
GNU GLOBAL是一个类似 cscope 的工具,也能提供源文件之间的交叉索引。其独到之处在于,当你生成索引文件以后,再修改整个项目里的一个文件,然后增量索引的过程非常快。
参考:
http://www.gnu.org/software/global/globaldoc_toc.html
http://www.gnu.org/software/global/links.html
http://www.newsmth.net/nForum/#!article/VIM/61229