cscope是什么? cscope 是一个 C 语言的浏览工具,通过这个工具可以很方便地找到某个函数或变量的定义位置、被调用的位置等信息。目前支持 C 和 C++。cscope 自身带一个基于文本的用户界面,不过 gvim 提供了cscope接口,因此可以在 gvim 中调用 cscope,方便快捷地浏览源代码。
按照vim里cscope的参考手册(在vim中执行":help
cscope"命令),把cscope功能加到.vimrc里后(需要你的vim在编译时选择了"--enable-cscope"选项,否则你需要重新
编译vim),配置就算完成了。然后用下面的命令生成代码的符号索引文件:
cscope -Rbkq
这个命令会生成三个文件:cscope.out, cscope.in.out, cscope.po.out。
其中cscope.out是基本的符号索引,后两个文件是使用"-q"选项生成的,可以加快cscope的索引速度。
上面所用到的命令参数,含义如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030872863.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030872863.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
-R: 在生成索引文件时,搜索子目录树中的代码
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030873316.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030873316.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
-b: 只生成索引文件,不进入cscope的界面
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030873863.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030873863.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
-k: 在生成索引文件时,不搜索/usr/include目录
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875019.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875019.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
接下来,就可以在vim里读代码了。
不过在使用过程中,发现无法找到C++的类、函数定义、调用关系。仔细阅读了cscope的手册后发现,原来cscope在产生索引文件时,只搜索类型
为C, lex和yacc的文件(后缀名为.c, .h, .l,
.y),C++的文件根本没有生成索引。不过按照手册上的说明,cscope支持c++和Java语言的文件。
于是按照cscope手册上提供的方法,先产生一个文件列表,然后让cscope为这个列表中的每个文件都生成索引。
为了方便使用,编写了下面的脚本来更新cscope和ctags的索引文件:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875144.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875144.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
#!/bin/sh
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875722.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030875722.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030876191.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030876191.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
find . -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files
(find ./* -name '*.[ch]' > cscope.files)
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030876832.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030876832.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
cscope -bkq -i cscope.files
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open('http://images.wangchao.net.cn/images/upload/images/lsdn/1172030877191.png');}" alt="" src="http://images.wangchao.net.cn/images/upload/images/lsdn/1172030877191.png" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
ctags -R
这个脚本,首先使用find命令,查找当前目录及子目录中所有后缀名为".h", ".c"和".cc"的文件,并把查找结果重定向到文件cscope.files中。
然后cscope根据cscope.files中的所有文件,生成符号索引文件。
最后一条命令使用ctags命令,生成一个tags文件,在vim中执行":help tags"命令查询它的用法。它可以和cscope一起使用。
ctags使用简介
Vi大家都很熟悉,某些情况下它是我们在Linux下唯一可用的文本编辑器。其实我们平时所指的VI其实是VIM(ViImproved,VI增强版)。有人常常拿VIM和emacs比较,VIM尽管不象emacs那样有那么多的功能,但是比emacs更方便、好用。VIM简单易用,只要看看VIM自带的vitutor,大约只要20分钟就能掌握VIM的用法。对于Linux下的C/C++的程序员,使用VIM+Ctags的组合来写程序也许是最佳的选择。^_^ VI的使用方法这里就不罗嗦了,本文假设大家已花了20分钟学习过了vitutor。 ^_^ 本节我们主要讲一下Ctags这个程序。尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM。并且VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码。 熟练的使用ctags仅需记住下面七条命令:(很简单吧,呵呵) 1. $ ctags –R * ($ 为Linux系统Shell提示符) 2. $ vi –t tag (请把tag替换为您欲查找的变量或函数名) 3. :ts (ts 助记字:tags list, “:”开头的命令为VI中命令行模式命令) 4. :tp (tp 助记字:tags preview)---?此命令不常用,可以不用记 5. :tn (tn 助记字:tags next) ---?此命令不常用,可以不用记 6. Ctrl + ] 7. Ctrl + T 下面我们逐条的解释上面的命令: “$ ctags –R *”:“-R”表示递归创建,也就包括源代码根目录(当前目录)下的所有子目录。“*”表示所有文件。这条命令会在当前目录下产生一个“tags”文件,当用户在当前目录中运行vi时,会自动载入此tags文件。Tags文件中包括这些对象的列表: 用#define定义的宏 枚举型变量的值 函数的定义、原型和声明 名字空间(namespace) 类型定义(typedefs) 变量(包括定义和声明) 类(class)、结构(struct)、枚举类型(enum)和联合(union) 类、结构和联合中成员变量或函数 VIM用这个“tags”文件来定位上面这些做了标记的对象。 剩下的命令就是定位这些对象的方法: “$ vi –t tag” :在运行vim的时候加上“-t”参数,例如: [/usr/src]$ vim -t main 这个命令将打开定义“main”(变量或函数或其它)的文件,并把光标定位到这一行。如果这个变量或函数有多处定义,在VI命令行模式“:ts”命令就能列出一个列表供用户选择。 “:tp”为上一个tag标记文件,“:tn”为下一个tag标记文件。当然,若当前tags文件中用户所查找的变量或函数名只有一个,“:tp,:tn”命令不可用。 :( 最方便的方法是把光标移到变量名或函数名上,然后按下“Ctrl+]”,这样就能直接跳到这个变量或函数定义的源文件中,并把光标定位到这一行。用“Ctrl+t”可以退回原来的地方。即使用户使用了N次“Ctrl+]”查找了N个变量,按N次“Ctrl+t”也能回到最初打开的文件,它会按原路返回 。 ^_^ 注意:运行vim的时候,必须在“tags”文件所在的目录下运行。否则,运行vim的时候还要用“:settags=”命令设定“tags”文件的路径,这样vim才能找到“tags”文件。在完成编码时,可以手工删掉tags文件(帚把不到,灰尘不会自己跑掉^_^)。 |