0 preview
cscope + tags 可以帮助 linux 开发者简单地看代码,提高效率,这里记录一下我的学习过程。
1 安装cscope
yum install cscope
yum install ctags
安装包 make
2 生成索引文件
在你的代码根目录 /data/mycodepkg 下面输入
[root@100 /data/mycodepkg]# cscope -Rbqk
-R: 在生成索引文件时,搜索子目录树中的代码
-b: 只生成索引文件,不进入cscope的界面
-k: 在生成索引文件时,不搜索/usr/include目录
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
3 配置
3.1把生成的索引添加进环境变量
[root@100 /data/mycodepkg]# vim ~/.bashrc
CSCOPE_DB=/data/scrubber/cscope.out
export CSCOPE_DB
3.2 生效环境变量
[root@100 /data/mycodepkg]# source ~/.bashrc
这样就可以把你生成的cscope文件添加到环境变量中,vim可以识别到cscope数据库。
4 验证使用
4.1
打开代码 对关键字/函数使用 ctrl + ] 发现会跳转/选项,按下数字键进入指定文件。
4.2 操作
退出进入文件(栈)命令: ctrl + t
进入下一个标签(当前进入1 若想进入2) : :tnext
第一个(进入1) : :tprevious
第最后一个(进入-1) : :tlast
5 ctags使用
下面来说明cscope + ctags使用方法和设置
5.1 设置数据库
在代码根目录设置 #cscope -Rbqk; ctags -R;
5.2 设置路径添加到vim中
利用脚本可以自动识别以后生成的每一个cscope数据库;后半部分程序是设置快捷键。
# vim ~/.vimrc
if has("cscope")
set csprg=/usr/local/bin/cscope #你的cscope程序路径
set csto=0
set cst
set nocsverb #不在shell提示添加成功echo
if filereadable("cscope.out")
cs add cscope.out
else
let cscope_file=findfile("cscope.out",".;")
let cscope_pre=matchstr(cscope_file,".*/")
if !empty(cscope_file)&&filereadable(cscope_file)
exe "cs add" cscope_file cscope_pre
endif
endif
endif
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("<cword>")<CR><CR>
nmap <C-\>i :cs find i <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
5.3 使用ctags搜索: 光标移动到某一个key上,然后按 ctrl+ ],可进入相应的定义中。
使用cscope搜索:光标移动到某一个key上,然后按 ctrl + \ ,然后再按 c 可查找出现过的关键字。