博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

vim ctags cscope taglist

Posted on 2011-09-23 16:02  bw_0927  阅读(947)  评论(0)    收藏  举报
 
 
 
一.安装ctags
   
  首先检测是否安装了ctags,我的RHEL5是自带了,可以运行如下命令进行检测
   ctags --version
   
  1. [root@hxy ~]# ctags --version
  2. Exuberant Ctags 5.6, Copyright (C) 1996-2004 Darren Hiebert
  3. Compiled: Jul 17 2006, 11:39:49
  4. Addresses: <dhiebert@users.sourceforge.net>, http://ctags.sourceforge.net
  5. Optional compiled features: +wildcards, +regex
 如果没有安装,请到http://ctags.sourceforge.net/ 下载
  简单的三步:
    ./configure 
    make 
    make install 
  即可
 
Q. When I try to open the taglist window, I am seeing the following error
   message. How do I fix this problem?

   Taglist: Failed to generate tags for /my/path/to/file
   ctags: illegal option -- -^@usage: ctags [-BFadtuwvx] [-f tagsfile] file ...

A. The taglist plugin will work only with the exuberant ctags tool. You
   cannot use the GNU ctags or the Unix ctags program with the taglist plugin.
   You will see an error message similar to the one shown above, if you try
   use a non-exuberant ctags program with Vim. To fix this problem, either add
   the exuberant ctags tool location to the PATH environment variable or set
   the 'Tlist_Ctags_Cmd' variable. (版本不对, let Tlist_Ctags_Cmd=”/path/to/exuberant/ctags“,该选项不一定起作用)
 
二.安装taglist
 
 安装taglist,可以使vim分成两栏,左边显示一些宏,全局变量,函数的定义,右边显示源码文件
  这可以用taglist来实现,它是一个插件。可以从
 我下载的是最新版 taglist_45.zip
 
  将其解压在  /usr/share/vim/vim70/ 目录之下. 我用如下命令
  unzip taglist_45.zip -d /usr/share/vim/vim70   
 
  1. [root@gz hxy]# unzip taglist_45.zip -d /usr/share/vim/vim70
  2. Archive: taglist_45.zip
  3. inflating: /usr/share/vim/vim70/plugin/taglist.vim
  4. inflating: /usr/share/vim/vim70/doc/taglist.txt
进入vim 在命令行下执行
  TlistOpen 命令,如果能看到多了一个侧边栏,表示安装成功
 
二.taglist 的配置
 
  为了让taglist窗口更加适合不同人的开发,可以在vim的配置文件 ~/.vimrc 中进行一些配置
 
  1. Tlist_GainFocus_On_ToggleOpen :        #为1则使用TlistToggle打开标签列表窗口后会获焦点至于标签列表窗口;为0则taglist打开后焦点仍保持在代码窗口
  2. Tlist_Auto_Open                       # 为1则Vim启动后自动打开标签列表窗口
  3. Tlist_Close_On_Select :               # 选择标签或文件后是否自动关闭标签列表窗口
  4. Tlist_Exit_OnlyWindow :               #Vim当前仅打开标签列表窗口时,是否自动退出Vim
  5. Tlist_Use_SingleClick :               #是否将默认双击标答打开定义的方式更改为单击后打开标签
  6. Tlist_Auto_Highlight_Tag :            #是否高亮显示当前标签。命令":TlistHighlightTag"也可达到同样效果
  7. Tlist_Highlight_Tag_On_BufEnter :     # 默认情况下,Vim打开/切换至一个新的缓冲区/文件后,标签列表窗口会自动将当前代码窗口对应的标签高亮显示。TlistHighlight_Tag_On_BufEnter置为0可禁止以上行为
  8. Tlist_Process_File_Always :           #为1则即使标签列表窗口未打开,taglist仍然会在后台处理vim所打开文件的标签
  9. Tlist_Auto_Update :                   #打开/禁止taglist在打开新文件或修改文件后自动更新标签。禁止自动更新后,taglist仅在使用:TlistUpdate,:TlistAddFiles,或:TlistAddFilesRecursive命令后更新标签
  10. Tlist_File_Fold_Auto_Close :          #自动关闭标签列表窗口中非激活文件/缓冲区所在文档标签树,仅显示当前缓冲区标签树
  11. Tlist_Sort_Type :                     #标签排序依据,可以为"name"(按标签名排序)或"order"(按标签在文件中出现的顺序,默认值)
  12. Tlist_Use_Horiz_Window :              #标签列表窗口使用水平分割样式
  13. Tlist_Use_Right_Window :              #标签列表窗口显示在右侧(使用垂直分割样式时)
  14. Tlist_WinWidth :                      #设定水平分割时标签列表窗口的宽度
  15. Tlist_WinHeight :                     #设定垂直分割时标签列表窗口的高度
  16. Tlist_Inc_Winwidth :                  #显示标签列表窗口时允许/禁止扩展Vim窗口宽度
  17. Tlist_Compact_Format :                #减少标签列表窗口中的空白行
  18. Tlist_Enable_Fold_Column :            #是否不显示Vim目录列
  19. Tlist_Display_Prototype :             #是否在标签列表窗口用标签原型替代标签名
  20. Tlist_Display_Tag_Scope :             #在标签名后是否显示标签有效范围
  21. Tlist_Show_Menu :                     #在图型界面Vim中,是否以下拉菜单方式显示当前文件中的标签
  22. Tlist_Max_Submenu_Item :              #子菜单项上限值。如子菜单项超出此上限将会被分隔到多个子菜单中。缺省值为25
  23. Tlist_Max_Tag_Length :                #标签菜单中标签长度上限
  
我常用的配置是
    let Tlist_Use_Right_Window = 1 
    let Tlist_Auto_Open=1
   let Tlist_Exit_OnlyWindow = 1 
   let Tlist_GainFocus_On_ToggleOpen=1  
三.taglist 的界面操作
 
  taglist的常用命令有(注意大小写)
     TlistOpen             #打开taglist窗口
     TlistClose            #关闭taglist窗口
     TlistToggle           #打开/关闭切换操作,
 
 
 一.安装cscope 
 
   RHEL已经安装这个软件,可以运行 cscope --version 来检测
     
  1. cscope --version
  2. cscope: version 15.5
  如果没有安装,可到http://cscope.sourceforge.net/ ,
 
  并且下载vim的插件代码,这里主要是定义了一些快捷方式
 
 这里插件要拷贝到 /usr/share/vim/vim70/plugin/ 当中
 
  安装成功后,可以在vim中运行命令
    cs help 
 
  如果能查看结果表示安装成功
    
  
  
二.创建cscope 数据库
  
   cscope主要靠检索自己的索引库来查找定义,因此需要检索内核源码定义,必须首先用cscope建立一个针对内核的数据库,名字一般叫 cscope.out
 
   我把所有cscope的数据库,集中放在/home/hxy/cscope 之下,每一个项目为一个单独子目录。我要查看ARM-Linux的 2.6.28的源码,就建立了/home/hxy/cscope/linux-2.6.28 目录。
 
   cscope 首先需要一个文件列表,这个列表名称一般取名为cscope.files.
   然后调用 cscope -b -q -k 创建数据库
      这几个选项含义如下
   
  1. [root@gz cscope]# cscope -h
  2. Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]
  3. [-p number] [-P path] [-[0-8] pattern] [source files]
  4. -b Build the cross-reference only.
  5. -C Ignore letter case when searching.
  6. -c Use only ASCII characters in the cross-ref file (don't compress).
  7. -d Do not update the cross-reference.
  8. -e Suppress the <Ctrl>-e command prompt between files.
  9. -F symfile Read symbol reference lines from symfile.
  10. -f reffile Use reffile as cross-ref file name instead of cscope.out.
  11. -h This help screen.
  12. -I incdir Look in incdir for any #include files.
  13. -i namefile Browse through files listed in namefile, instead of cscope.files
  14. -k Kernel Mode - don't use /usr/include for #include files.
  15. -L Do a single search with line-oriented output.
  16. -l Line-oriented interface.
  17. -num pattern Go to input field num (counting from 0) and find pattern.
  18. -P path Prepend path to relative file names in pre-built cross-ref file.
  19. -p n Display the last n file path components.
  20. -q Build an inverted index for quick symbol searching.
  21. -R Recurse directories for files.
  22. -s dir Look in dir for additional source files.
  23. -T Use only the first eight characters to match against C symbols.
  24. -U Check file time stamps.
  25. -u Unconditionally build the cross-reference file.
  26. -v Be more verbose in line mode.
  27. -V Print the version number.
 
如果手工来建立files文件基本不可能,文件太多了,所以我一般用如下脚本来创建内核索引库
如果用于你的机器,你需要修改KER_PRJ (cscope项目目录) 和 KERNDIR (内核源码目录)
 
这个脚本内容如下
  1. #!/bin/sh
  2. KERNDIR=/home/hxy/linux-2.6.28_smdk6410
  3. CS_PRJ=/home/hxy/cscope
  4. KER_PRJ=$CS_PRJ/linux-2.6.28
  5. #if [ -ne $KER_PRJ ] ; then
  6. #mkdir -p $KER_PRJ
  7. #endif
  8. find $KERNDIR/mm/* $KERNDIR/init/* \
  9. $KERNDIR/ipc/* $KERNDIR/net/* $KERNDIR/include/linux/* \
  10. $KERNDIR/arch/arm/boot/* $KERNDIR/arch/arm/common/* $KERNDIR/arch/arm/include/* \
  11. $KERNDIR/arch/arm/mach-s3c6410/* $KERNDIR/arch/arm/mach-s3c6410/* $KERNDIR/arch/arm/plat-s3c64xx/* \
  12. $KERNDIR/arch/arm/mm/* $KERNDIR/arch/arm/plat-s3c/* \
  13. -name "*.[chxsS]" >$KER_PRJ/cscope.files
  14. cd $KER_PRJ
  15. cscope -b -q -k
  16. cd $CS_PRJ
 
 
 三.在vim中使用 cscope  
 
    首先如果在VIM使用内核源码可以用 cs add 命令来加载源码数据库
     我在~/.vimrc 中增加如下一段
 
  1. if has("cscope") && filereadable("/usr/bin/cscope")
  2. set csprg=/usr/bin/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. cs add /home/hxy/cscope/linux-2.6.28/cscope.out
  14. set csverb
  15. endif
    cscope来提供多个命令来查询各种定义.
    主要用 cs find <option> 关键字 来检索
    其中选项有,比如c是查找调用函数定义,如果不确定,可以直接用g选项
      
  1. : cs find s ---- 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
  2. :cs find g ---- 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能
  3. :cs find d ---- 查找本函数调用的函数
  4. :cs find c ---- 查找调用本函数的函数
  5. :cs find t: ---- 查找指定的字符串
  6. :cs find e ---- 查找egrep模式,相当于egrep功能,但查找速度快多了
  7. :cs find f ---- 查找并打开文件,类似vim的find功能
  8. :cs find i ---- 查找包含本文件的文件
   如:
       cs find -g module_init
 
   cs的插件定义一些快捷操作,更加方便的cs的使用, 这一些快捷键是按
    ctrl-\ 或 ctrl-@ (即2键) 加上选项字符,便是对当前光标所在单词进行检索
    
       
   在我这里的环境,开始快捷键和查找总是失败,必须先在命令行窗口运行一次
   cs reset 才能正常工作
 
   ctrl-\ <选项> 是查找当前单词的定义,并且在当前窗口显示.
     如 (ctrl-\)g 是查找定义
   ctrl-@ <选项> 是查找当前单词的定义,并且打开一个水平窗口显示.
 
  这里找到结果的显示界面,这里我是查找readl定义
     
  
这里显示有两个结果,可以输入相关数字跳到相关文件.
 
   如果用ctrl-\ 查找,结果直接跳到相关文件,并有当前窗口显示.
 
 
 
 
如果用ctrl-@来查找,则显示一个水平窗口显示结果
 
 
 
在查找结果中处理的快捷键:
 
    ctrl-T 返回上次调用的地方
    ctrl-] 显示光标处单词的检索列表
    ctrl-[ 关闭检索列表
    ctrl-F 返回上一次修改处
    ctrl-B 跳入前一次修改处(ctrl-F相反方向)
 
三.常见问题处理
   
   cs 查找不到结果,并建议重置连接,可能需要cs reset重置一下联接
 
   taglist 滚动后发生乱屏,它正常显示需要UTF-8字体
 
   在图形界面下的终端的VIM使用cscope。出现cs find 可以用,但是快捷键不能用的情况,这个主要是终端的类型不对,在RHEL5下的不能配置成Linux终端,必须配置成Xterm 终端快捷键才有响应。
 
  

使用vim + cscope/ctags,就可以实现SourceInsight的功能,以后可以不再用盗版SouceInsight读代码了。
按照vim里cscope的参考手册(在vim中执行”:help cscope”命令),把cscope功能加到.vimrc里后(需要你的vim在编译时选择了”–enable-cscope”选项,否则你需要重新编译vim),配置就算完成了。然后用下面的命令生成代码的符号索引文件:

    cscope -Rbkq


这个命令会生成三个文件:cscope.out, cscope.in.out, cscope.po.out。
其中cscope.out是基本的符号索引,后两个文件是使用”-q”选项生成的,可以加快cscope的索引速度。
上面所用到的命令参数,含义如下:

-R: 在生成索引文件时,搜索子目录树中的代码
-b: 只生成索引文件,不进入cscope的界面
-k: 在生成索引文件时,不搜索
/usr/include目录
-q: 生成cscope
.in.outcscope.po.out文件,加快cscope的索引速度

接下来,就可以在vim里读代码了。
不过在使用过程中,发现无法找到C++的类、函数定义、调用关系。仔细阅读了cscope的手册后发现,原来cscope在产生索引文件时,只搜索类型为C, lex和yacc的文件(后缀名为.c, .h, .l, .y),C++的文件根本没有生成索引。不过按照手册上的说明,cscope支持c++和Java语言的文件。
于是按照cscope手册上提供的方法,先产生一个文件列表,然后让cscope为这个列表中的每个文件都生成索引。
为了方便使用,编写了下面的脚本来更新cscope和ctags的索引文件:

#!/bin/sh

find . -name "*.h" -o -name "*.c"-o -name "*.cc" > cscope.files
cscope -bkq -i cscope.files
ctags -R


这个脚本,首先使用find命令,查找当前目录及子目录中所有后缀名为”.h”, “.c”和”.cc”的文件,并把查找结果重定向到文件cscope.files中。
然后cscope根据cscope.files中的所有文件,生成符号索引文件。
最后一条命令使用ctags命令,生成一个tags文件,在vim中执行”:help tags”命令查询它的用法。它可以和cscope一起使用

 

遇到错误:

cscope error reading cscope connection

解决: 先使用”:cs kill 0″断开和cscope的连接,然后再”:cs add cscope.out”重新连就可以了。