打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Cscope在emacs中的配置与使用
在windows下通常使用source insight阅读源代码,本人希望将这项工作转移到linux下面。在配置并试用vim一段时间后,感觉不是特别满意。幸运的是,这个挑剔的人并不懒惰,开始尝试使用强大的emacs。在这里,并不介绍emacs的基本操作以及配置,建议学习emacs自带的帮助或者阅读《学习GNU Emacs》,而是与大家分享cscope在emacs中的配置与使用。
作为菜鸟在开源世界旅行的第一站,即本人的第一篇linux学习笔记,决定向大牛们学习,在正文开始之前注明文章版权和参考文献,毕竟水文也可以有版权:) 希望本文对您有所帮助,也欢迎您给我提意见和建议。我的邮箱是:intrepyd@gmail.com
版权说明和参考文献
1.版权说明
转载请注明转自潘云登的专栏 ,请保证全文转载,尤其不可省略这一部分。
2.参考文献
cscope-indexer脚本和xcscope.el文件的注释部分
Cscope
简介对于浏览源代码来说,在 Emacs 里面也有很多工具可用,比如自带的 etags 就不错,不过功能不如 Cscope 强大。它最初是由 Bell 实验室开发,并且多年以来一直是 AT&T Unix 商业发行版的一部分。它已经被用于管理超过两千万行代码的工程。在2000年4月,多亏了 Santa Cruz Operation, Inc. (SCO) , Cscope 的源代码在 BSD license 下开放了源代码Blah~Blah~
使用Cscope,主要用来回答以下问题:
Where is this variable used?
What is the value of this preprocessor symbol?
Where is this function in the source files?
What functions call this function?
What functions are called by this function?
Where does the message "out of space" come from?
Where is this source file in the directory structure?
What files include this header file?
安装和配置
1.首先,在系统中安装Cscope。Linux下,安装软件的方式通常有两种:
方式一:下载Cscope的源代码,按照源码包中的INSTALL说明文件,执行下面的命令进行配置编译安装
./configure
make
make install
Cscope的源码包里面有个xcscope.el文件,为(X)Emacs提供了Cscope 接口。它处于源码包的 contrib/xcscope 目录下。该目录下面还有一个名为cscope-indexer的脚本文件,用于创建文件列表和数据库。
方式二:利用linux发行版的软件管理工具进行安装。我使用的是Jaunty Jackalope(Ubuntu 9.04),只要一条命令,解决所有问题。
sudo apt-get install cscope
安装后,cscope-indexer脚本位于/usr/bin目录下,xcscope.el被置于emacs默认的load-path下,在我的系统中是/usr/share/emacs/site-lisp。
2.为了能够执行cscope-indexer脚本,需要将它放到PATH变量指向的文件夹下,如/usr/bin,并确认该脚本具有执行权限。
3.把xcscope.el文件放到系统用户的load-path下。为系统用户创建文件夹并将其加入load-path的方法是:先创建文件夹,
mkdir -p ~/.emacs.d/site-lisp/
然后,在emacs的配置文件~/.emacs中添加
;;LOAD_PATH
(add-to-list 'load-path' "~/.emacs.d/site-lisp")
如果没有该文件,请手动创建。
4.在.emacs文件中加入下面的语句
(require 'xcscope)
或者,你希望只在打开c/c++文件的时候才加载xcscope,可以加入
(add-hook 'c-mode-common-hook '(lambda() (require 'xcscope)))
5.xcscope默认的快捷键都是绑定到C-c s的前缀上面,如果你经常使用xcscope.el,可以自己进行按键绑定,减少击键次数。不要担心别人笑你懒,xcscope.el的编写者就鼓励我们这样做:-)。具体方法是,在.emacs文件中加入
(define-key global-map [(control f3)]  'cscope-set-initial-directory)
(define-key global-map [(control f4)]  'cscope-unset-initial-directory)
(define-key global-map [(control f5)]  'cscope-find-this-symbol)
(define-key global-map [(control f6)]  'cscope-find-global-definition)
(define-key global-map [(control f7)]  'cscope-find-global-definition-no-prompting)
(define-key global-map [(control f8)]  'cscope-pop-mark)
(define-key global-map [(control f9)]  'cscope-next-symbol)
(define-key global-map [(control f10)] 'cscope-next-file)
(define-key global-map [(control f11)] 'cscope-prev-symbol)
(define-key global-map [(control f12)] 'cscope-prev-file)
(define-key global-map [(meta f9)]     'cscope-display-buffer)
(define-key global-map [(meta f10)]    'cscope-display-buffer-toggle)
6.重启emacs,使配置生效。
基本使用
这里以内核源码为例,介绍Cscope的基本用法。
1.首先,在源码根目录下,如~/kernerl/linux-2.6.29.3,利用cscope-indexer脚本生成文件列表和数据库,方法是执行
cscope-indexer -r
-r参数表示递归检索子目录,文件列表和数据库的默认文件名分别为cscope.files和cscope.out,可以使用-i,-f参数进行修改,请参考man了解脚本参数用法。
2.激动人心的时刻到了。用emacs打开init/main.c,C-s搜索sched_init函数,将光标停在函数名上,按C-c s d或者先前设置的Ctrl+F6,回车进行查找。结果居然用了35.32秒,汗!原来,Cscope默认在每次进行查找时更新cscope.out。当工程十分庞大时,建议关闭该选项以提高查找速度。方法是在~/.emacs文件中加入
(setq cscope-do-not-update-database t)
重复上述操作,结果仍然用了9.89秒,再汗!莫非是我的古董本太慢?
3.百度一下,你就知道:) Cscope可以通过创建反向索引加速查找,方法是调用Cscope时,使用-q参数。真的假的,一试便知。修改cscope-indexer脚本,将
cscope -b -i $LIST_FILE -f $DATABASE_FILE
替换为
cscope -q -b -i $LIST_FILE -f $DATABASE_FILE
进入内核根目录,删除先前的文件列表和数据库,重新调用cscope-indexer。这回多生成了两个文件,cscope.in.out和cscope.po.out。重试刚才的查找,结果只用了0.08秒,大功告成。
4.贴张结果,庆祝一下:)
附:默认的按键绑定
;; * Keybindings:
;;
;; All keybindings use the "C-c s" prefix, but are usable only while
;; editing a source file, or in the cscope results buffer:
;;
;;      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.
;;
;; These pertain to navigation through the search results:
;;
;;      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.
;;
;; These pertain to setting and unsetting the variable,
;; `cscope-initial-directory', (location searched for the cscope database
;;  directory):
;;
;;      C-c s a         Set initial directory.
;;      C-c s A         Unset initial directory.
;;
;; These pertain to cscope database maintenance:
;;
;;      C-c s L         Create list of files to index.
;;      C-c s I         Create list and index.
;;      C-c s E         Edit list of files to index.
;;      C-c s W         Locate this buffer's cscope directory
;;                      ("W" --> "where").
;;      C-c s S         Locate this buffer's cscope directory.
;;                      (alternate binding: "S" --> "show").
;;      C-c s T         Locate this buffer's cscope directory.
;;                      (alternate binding: "T" --> "tell").
;;      C-c s D         Dired this buffer's directory.
http://cocobear.info/blog/2007/10/12/use-vim-cscope-read-code/
vim与cscope安装就不说了,一般的发行版都会有的。不过如果你是源码编译的vim,请使用–enable-cscope选项。
-R: 在生成索引文件时,搜索子目录树中的代码
-b: 只生成索引文件,不进入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成索引文件时,不搜索/usr/include目录
-i: 如果保存文件列表的文件名不是cscope.files时,需要加此选项告诉cscope到哪儿去找源文件列表。可以使用“-”,表示由标准输入获得文件列表。
-I dir: 在-I选项指出的目录中查找头文件
-u: 扫描所有文件,重新生成交叉索引文件
-C: 在搜索时忽略大小写
-P path: 在以相对路径表示的文件前加上的path,这样,你不用切换到你数据库文件所在的目录也可以使用它了。
在使用cscope之前需要先生成一个数据库,你可以使用cscope-indexer(如果多个目录你可以使用-R选项),它会在当前目前下生成一个cscope.files的文件,这个文件包含了cscope需要生成索引的全部文件,因为cscope-indexer不会自动查到cpp,java后缀的文件,因此最后使用find来生成cscope.files文件:
[cocobear@cocobear src]$ find ./ -name “*.c” -or -name “*.h” -or -name “*.cpp” > cscope.files
上面的命令会把当前目录下所有.c,.h,.cpp文件列出并写入cscope.files文件中。接着使用cscope -bq来生成索引引。接着你就可以使用vim来打开一个文件来浏览代码了。使用cs(cscope写)命令来实现函数的调用,定义查找:
;;      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.
s: 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
g: 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能
d: 查找本函数调用的函数
c: 查找调用本函数的函数
t: 查找指定的字符串
e: 查找egrep模式,相当于egrep功能,但查找速度快多了
f: 查找并打开文件,类似vim的find功能
i: 查找包含本文件的文
例如:cs find c do_cscope 可以用来查找项目中调用了do_cscope函数的函数,在vim会以一个列表的形式列出所有相关的内容,你可以输入数字来选择。
当然如果你的源码中只含有.c,.h文件,你可以直接使用
cscope -Rbq
来生成索引文件。
如果你有兴趣的话可以在vim里输入:cs help来查看更多cscope的信息。
BTW:总觉得在kscope里面看代码不爽的很,还是喜欢vim。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
程序员的emacs配置大全(cedet+ecb+cscope+gdb-ui)-jzj
vim cscope tags使用总结
vim ctags taglist cscope cppcomplete ...
Linux下vim ctags taglist cppcomplete cscope gl...
打造linux下的source insight
vim 配置&插件分享 ? 抠腚爱揉曼
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服