cscope的快速初始化和使用技巧

##1.cscope的安装
sudo apt-get install cscope

##2.cscope脚本的使用
下面是我自己写的一个cscope脚本,用来一个命令完成cscope的初始化操作。

#!/bin/sh
DIR=`pwd`
update=1
change=0
while getopts "d:uc" opt;
do
    case $opt in
        d)
            DIR=$OPTARG
			;;
		u)
			update=1
			;;
		c)
			change=1
			;;
		?)
			echo "invaild option!"
			exit 1
	esac
done

cd ${DIR}

if [ 1 -eq ${change} ]; then
	echo "change project cscope database!"
	res=$(find ${DIR} -name cscope.out)
	if [ "x"${res} = "x" ]; then
		echo "Not found cscope database, generate cscope database!"
		find ${DIR} -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files
		cscope -bkq -i cscope.files 
		ctags -R *
		export CSCOPE_DB=${DIR}/cscope.out
	else
		echo "Found cscope database:${res}, just change CSCOPE_DB env!"
		export CSCOPE_DB=${res}
	fi
elif [ 1 -eq ${update} ]; then
	echo "udpate project cscope database!"
	find ${DIR} -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files
	cscope -bkq -i cscope.files
	ctags -R *
	export CSCOPE_DB=${DIR}/cscope.out
fi

echo CSCOPE_DB_PATH=${CSCOPE_DB}

此脚本是用来自动生成cscope database文件的,把该脚本放在可执行环境中,以后我们在自己的项目代码目录中,直接使用source /bin/cscope.sh即可完成cscope环境的初始化,注意必须要使用source命令,这样才能够使cscope.sh中配置的CSCOPE_DB环境变量在本终端中始终可用。cscope.sh脚本同时支持多种命令,-d命令后可跟具体的项目代码目录,这样可以在其他目录生成对应项目的cscope数据库,-u是用来更新数据库的,-c是用来切换数据库的,如果我们有两个项目要切换,只需要进入到要切换的项目,目录中执行-c或者加上-d指明对应的目录即可。如果不加任何选项,那么cscope.sh默认就会在本目录下执行-u操作,也就是更新数据库操作,如果之前没有生成过数据库,那么它就会新生成一个数据库。所以正常使用下,我们只需要进入到我们的项目目录,然后执行cscope.sh脚本即可,为了更进一步的简化我们的处理,可以在~/.bashrc中添加环境变量:

export PROJECT_INIT="source /bin/cscope.sh"

这样我们进入到对应的项目,只需要执行$PROJECT_INIT即可自动调用到“source /bin/cscope.sh ”命令了,很简单了吧!

##3.cscope的配置

在生成了cscope数据库后,我们还需要对vim进行一些必要的配置来方便我们对cscope的使用:

" set quickfix
set cscopequickfix=s-,c-,d-,i-,t-,e-  

" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag

" check cscope for definition of a symbol before checking ctags: set to 1
" if you want the reverse search order.
set csto=0

" add any cscope database in current directory
if filereadable("cscope.out")
	cs add cscope.out 
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
	cs add $CSCOPE_DB
endif

" show msg when any other cscope db added
set cscopeverbose  

以上是基本的配置,后面我们可以在增加一些keymap:

"cscope插件热键 
nmap cs :cs find s <C-R>=expand("<cword>")<CR><CR> 
nmap cg :cs find g <C-R>=expand("<cword>")<CR><CR>  
nmap cc :cs find c <C-R>=expand("<cword>")<CR><CR>  
nmap cd :cs find d <C-R>=expand("<cword>")<CR><CR>  
nmap ct :cs find t <C-R>=expand("<cword>")<CR><CR>  
nmap ce :cs find e <C-R>=expand("<cword>")<CR><CR>  
nmap cf :cs find f <C-R>=expand("<cfile>")<CR><CR>  
nmap ci :cs find i ^<C-R>=expand("<cfile>")<CR><CR>  

"F6打开quickfix
nmap <F6> :cw<CR>

"Ctrl+F6关闭quickfix
nmap <C-F6> :ccl<CR>

"F7向后选择quickfix
nmap <F7> :cn<CR>

"F8向前选择quickfix
nmap <F8> :cp<CR>

这样利用cs find s和对应的quikcfix窗口,我们可以很方便的跳转到需要的地方。

本人有分享过完整的vimrc的配置,参见另一篇博文:《个人使用的vimrc的配置》
https://blog.csdn.net/rikeyone/article/details/82586253

对应的github地址:https://github.com/rikeyone/cscope-script.git

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值