cscope使用简介

cscope最早是由一位Joe Steffen的开发者,为了方便自己的工作而开发的。后来,在PDP-11上开发一项大工程时,进行改进。最后是在2000年由SCO公司开源。

http://cscope.sourceforge.net/

cscope主要是和vim搭配使用,主要的作用是查找函数、宏、变量或者字符串的引用。类似ctags但是更强大。

安装很简单,Ubuntu可以使用apt-get install安装,centos可以使用yum install安装,也可以下载安装包,敲./configure   make   make install安装。


使用也比较方便,首先,为你的代码生成数据库

cscope -Rbqk

-R:为当前目录下所有子目录创建数据库
-b:生成数据库之后不启动自带的查询界面
-q:生成cscope.in.out和cscope.po.out,加快搜索速度
-k:跳过/usr/include目录

执行完毕之后,当前目录会多出三个文件

cscope.out          //生成的符号索引
cscope.in.out
cscope.po.out

cscope支持8种查询:

s:查找C语言符号,即函数名,宏,变量等
g:查找函数,宏,变量等定义的位置,相当于转到定义
d:查找本函数调用的函数
c:查找调用本函数的函数
t:查找指定的字符串
e:查找egrep功能
f:查找并打开文件
i:查找包含本文件的文件

在vim中使用cscope有两种方法:

1、不用配置vimrc文件,直接使用命令:

cs f(ind) g fun

2、写入vimrc文件,可以定义快捷键,例如:

Ctrl + \ + g

vimrc配置可如下参考

if has(“cscope”)

set csprg=/usr/bin/cscope
set cst
set cscopetag
set csto=0
set nocsverb
if filereadable("cscope.out")
    cs add cscope.out
elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
endif

set csverb

set cscopeverbose

nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>i :cs find i <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>

nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@>i :scs find i <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>

nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@><C-@>i :vert scs find i <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>

endif

注:查找包含本文件的快捷键中,大部分网上版本使用

nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>

但是我的计算机提示“找不到匹配的结果”,修改为上述方式可行



综上,基本的cscope使用已经介绍完了,在使用过程中,需要特别注意的是,ctrl+\是同时按下,然后紧接着按g或者其他键,不能三个键同时按下。

猜你喜欢

转载自blog.csdn.net/kakaluote81/article/details/75021053