SourceInsight是Windows下最方便浏览代码的工具。但是SourceInsight是没有Linux版本的。为了方便在Linux下浏览代码并进行学习,可以利用Vim配合Cscope来打造Linux下的SourceInsight。
使用时,将光标停留在要查找的对象上,按下g,即先按“Ctrl+@”,然后很快再按“g”,将会查找该对象的定义
Cscope是Vim适用的工具和插件,通过Cscope可以方便地获知某个函数的定义以及被哪些函数调用。
Cscope安装
可以在http://cscope.sourceforge.net/下载源码包,然后解压,编译安装。
./configure
make
make install
生成Cscope数据库
使用cscope前,必须为代码生成一个cscope数据库。假设当前代码在/usr/src/linux目录下,则运行下列命令。
cd /usr/src/linux
cscope –Rbq
然后会生成3个文件:cscope.in.out,cscope.out,cscope.po.out。
在.vimrc 中加入
Cscope的功能通过它的子命令“find”来实现。
cs find c|d|e|g|f|i|s|tname
- s:查找C代码符号
- g:查找本定义
- d:查找本函数调用的函数
- c:查找调用本函数的函数
- t:查找本字符串
- e:查找本egrep模式
- f:查找本文件
- i:查找包含本文件的文件
可以在.vimrc中添加下面的快捷键,免得每次都要输入一长串命令。
nmap <</span>C-@>s :cs find s <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>g :cs find g <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>c :cs find c <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>t :cs find t <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>e :cs find e <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>f :cs find f <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR> nmap <</span>C-@>i :cs find i ^<</span>C-R>=expand("<</span>cword>")<</span>CR>$<</span>CR> nmap <</span>C-@>d :cs find d <</span>C-R>=expand("<</span>cword>")<</span>CR><</span>CR>