vim +cscope read source code

本文介绍如何使用Vim结合Cscope进行高效的代码导航和搜索。包括Cscope的安装配置、数据库构建方法及Vim中的具体操作命令。适用于大型项目的代码管理和维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

under linux vim +cscope build and command

first you must download install cscope or install scope used any method

i used download cscope source like follow.

download and install cscope

a.download cscope-15.7a.tar.bz2 from website http://cscope.sourceforge.net/ 

b.tar jxf cscope-15.7a.tar.bz2

c.cd cscope-15.7a

d. ./configure

e.make

f.sudo make install

g.whereis cscope(test install result)

used vim + cscope build project  cscope.out ,cscope.in.out and cscope.po.out


a.cscope -Rbq (this command must in you project root directory)
   you can see three files create.
b.open any you source file and used follow command add cscope.out to vim
c.:cs add /project_root_dir/cscope.out /project_root_dir
d.you can configuration you vim ~/.vimrc used follow script
   
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
" csprg is cscope dir
" nu is line number
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has("cscope")
  set csprg=/usr/bin/cscope 
  set nu
  set csto=1
  set cst
  set nocsverb
  " add any database in current directory
  if filereadable("cscope.out")
      cs add cscope.out
  endif
  set csverb
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("<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>

以下是论坛找到的转载以下,没有表明出处请谅解!!!!
如果有大型的项目, cscope -R不但会消耗大量的时间, 而且还会干扰tags,所以一般会现生成一个列表

 LNX=/home/username/project_dir
    cd /  
    find  $LNX                                                                \
 -path "$LNX/arch/*" ! -path "$LNX/arch/i386*" -prune -o               \
 -path "$LNX/include/asm-*" ! -path "$LNX/include/asm-i386*" -prune -o \
 -path "$LNX/tmp*" -prune -o                                           \
 -path "$LNX/Documentation*" -prune -o                                 \
 -path "$LNX/scripts*" -prune -o                                       \
 -path "$LNX/drivers*" -prune -o                                       \
        -name "*.[chxsS]" -print >/home/username/cscope/cscope.files

然后使用这个列表生成一个cscope.out的数据库文件,(上面加了绝对路径,使得数据库文件可以放在任意地方)
 cd  /home/username/cscope
 cscope -bkq -i cscope.files

这样就会生成一个数据库文件了
cscope的一些参数的意义如下

  • -R: 在生成索引文件时,搜索子目录树中的代码
  • -b: 只生成索引文件,不进入cscope的界面
  • -q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
  • -k: 在生成索引文件时,不搜索/usr/include目录
  • -i: 如果保存文件列表的文件名不是cscope.files时,需要加此选项告诉cscope到哪儿去找源文件列表。可以使用”-“,表示由标准输入获得文件列表。
  • -Idir: 在-I选项指出的目录中查找头文件
  • -u: 扫描所有文件,重新生成交叉索引文件
  • -C: 在搜索时忽略大小写
  • -Ppath: 在以相对路径表示的文件前加上的path,这样,你不用切换到你数据库文件所在的目录也可以使用它了。

cscope的一些命令如下

  • s: 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
  • g: 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能
  • d: 查找本函数调用的函数
  • c: 查找调用本函数的函数
  • t: 查找指定的字符串
  • e: 查找egrep模式,相当于egrep功能,但查找速度快多了
  • f: 查找并打开文件,类似vim的find功能
  • i: 查找包含本文件的文件

可以在.vimrc设置一个自动更新的快捷键

 nmap <F5> :!find . -iname '*.c' -o -iname '*.cpp' -o -iname '*.h' -o -iname '*.hpp' > cscope.files<CR> 
                        \ :!cscope -b -i cscope.files -f cscope.out<CR> 
                        \ :cs reset<CR>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值