Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96283
  • 博文数量: 17
  • 博客积分: 37
  • 博客等级: 民兵
  • 技术积分: 217
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-09 18:24
文章分类
文章存档

2016年(4)

2015年(3)

2014年(9)

2011年(1)

我的朋友

分类: C/C++

2016-01-30 22:36:12

对于使用windows进行代码开发的程序员来说,source insight由于其丰富的插件,无疑是最受欢迎的源码查看软件。但是在代码量大时,特别是在阅读kernel这么巨大的源码时,第一次工程同步是一件非常消耗时间的事情,快则半个小时,慢则导到电脑无法操作,本人就有过工程同步时,电脑蓝屏的经历。
但是自从熟悉了用上cscope + vim之后,完全是感受到了vim阅读源码的快感,这种流畅程度是source insight无法比拟的,本文将描述如何搭建vim + cscope环境,以及如何使用vim查看kernel源码,如何查看函数、变量的定义和引用。
1)安装vim和cscope
在ubuntu下直接使用命令,前提是必须保证ubuntu能够访问网络。
$ sudo apt install vim
$ sudo apt install cscope
2) 配置vim配置文件.vimrc(存放在~/.vimrc)
使用vim 打开 ~/.vimrc,在里面添加以下内容

点击(此处)折叠或打开

  1. if has("cscope")
  2.                 set csprg=/usr/bin/cscope "使用which cscope命令查看cscope的安装路径
  3.                 set csto=0
  4.                 set cst
  5.                 set nocsverb
  6.                 " add any database in current directory
  7.                 if filereadable("cscope.out")
  8.                     cs add cscope.out
  9.                 " else add database pointed to by environment
  10.                 elseif $CSCOPE_DB != ""
  11.                     cs add $CSCOPE_DB
  12.                 endif
  13.                 set csverb
  14. endif
  15.         nmap <C-@>s :cs find s <C-R>=expand("")<CR><CR>
  16.         nmap <C-@>g :cs find g <C-R>=expand("")<CR><CR>
  17.         nmap <C-@>c :cs find c <C-R>=expand("")<CR><CR>
  18.         nmap <C-@>t :cs find t <C-R>=expand("")<CR><CR>
  19.         nmap <C-@>e :cs find e <C-R>=expand("")<CR><CR>
  20.         nmap <C-@>f :cs find f <C-R>=expand("")<CR><CR>
  21.         nmap <C-@>i :cs find i ^<C-R>=expand("")<CR>$<CR>
  22.         nmap <C-@>d :cs find d <C-R>=expand("")<CR><CR>

这段内容在cscope的帮助文档(1)里面可以找到,可以从那里面拷贝过来。第一段if ..... endif 的作用是让vim,自动检测cscope.out文件,然后自动加载这个数据库,不需要人为的加载。第二段以nmap开头的是快捷键映射,没有这些快捷键就体现不了cscope的优势了,因为每次手动敲关键字是很耗时间的,详细使用方法后面我们用到的时候慢慢解释。
3)生成cscope.out数据库
在内核根目录下,使用命令cscope -Rbq 很快就可以在当前目录生成三个文件(cscope.in.out, cscope.out, cscope.po.out),到此,我们的准备工作已经结束了。
4)查看源码
在根目录下,输入vim,在命令行下输入cs find g start_kernel或者打开任意一个文件,把光标移动到一个函数或者变量上,之后我们第二步中设置的快捷键就可以派上用场了。光标在函数或者变量上,同时按下快捷键ctrl+@,之后快速按下字母g键,就会列出这个函数或者变量的定义,然后选择列表前面的数字,就可以跳转到函数或者变量定义之处啦。
这也就是前面说到的快捷键了。同样在变量或者函数处按下快捷键ctrl+@再按下s键,就可以列出使用变量或者函数的地方。
如果要返回,只需要使用快捷键ctrl+o,就可以返回到上次光标所在的地方。
对于其他选项,请参考帮助文档,不知道怎么用?在vim 命令行下,输入:help cs 就可以了。

点击(此处)折叠或打开

  1. USAGE :cs find {querytype} {name}
  2.                                                                                 
  3.             {querytype} corresponds to the actual cscope line
  4.             interface numbers as well as default nvi commands:
  5.                                                                                 
  6.                 0 or s: Find this C symbol
  7.                 1 or g: Find this definition
  8.                 2 or d: Find functions called by this function
  9.                 3 or c: Find functions calling this function
  10.                 4 or t: Find this text string
  11.                 6 or e: Find this egrep pattern
  12.                 7 or f: Find this file
  13.                 8 or i: Find files #including this file
  14.                                                                                 
  15.         For all types, except 4 and 6, leading white space for {name} is
  16.         removed. For 4 and 6 there is exactly one space between {querytype}
  17.         and {name}. Further white space is included in {name}.
请尽情享受vim +cscope给你带来的快乐吧!


 
 
阅读(1250) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~