cscope和ctags添加头文件的索引,查看系统库函数

4 篇文章 0 订阅

写C语言代码的时候,有时候希望能查看系统库函数,比如memcpy是怎么实现的。 
解决方法:

sudo vi /etc/bash.bashrc   
  • 1

写入:

alias mkcscopefile='find `pwd` -name "*.[ch]" -o -name "*.cpp" > cscope.files'  
112 alias mktag='ctags -R;cscope -bRq'    
  • 1
  • 2

保存退出 
运行:source /etc/bash.bashrc 
接下来,利用 mkcscopefile 和mktag就可以生成索引文件了 
我们进入c语言的头文件目录生成索引,在项目中加入索引就可以了在我们的代码中跳转到系统库函数了。

cd /usr/include
mkcscopefile && mktag
//生成索引文件
  • 1
  • 2
  • 3

用vim打开我们自己的项目,如果需要查看库函数时,在命令模式输入cs add /usr/include 就可以了。 
加入库函数索引前: 
这里写图片描述

加入库函数 
这里写图片描述

定位到printf, 按下ctrl+], 返回可以按ctrl+t 
成功跳转到库函数 
这里写图片描述

这里写图片描述

.vimrc中cscope的配置如下(复制到你的.vimrc中就可以使用,但记得要安装了cscope和ctags):

"  < cscope 工具配置 >
" -----------------------------------------------------------------------------
" 用Cscope自己的话说 - "你可以把它当做是超过频的ctags"
" 在/usr/include下运行cscope -rbq, 设置cscope_db环境变量
if has("cscope")
    set csprg=/usr/bin/cscope "使用which cscope命令查看cscope的安装路径
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
        " else add database pointed to by environment
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
    set csverb
endif
map <F12> :call Do_CsTag()<CR>
nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<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>:copen<CR>
nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
function Do_CsTag()
    let dir = getcwd()
    if filereadable("tags")
        if(g:iswindows==1)
            let tagsdeleted=delete(dir."\\"."tags")
        else
            let tagsdeleted=delete("./"."tags")
        endif
        if(tagsdeleted!=0)
            echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
            return
        endif
    endif
    if has("cscope")
        silent! execute "cs kill -1"
    endif
    if filereadable("cscope.files")
        if(g:iswindows==1)
            let csfilesdeleted=delete(dir."\\"."cscope.files")
        else
            let csfilesdeleted=delete("./"."cscope.files")
        endif
        if(csfilesdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
            return
        endif
    endif
    if filereadable("cscope.out")
        if(g:iswindows==1)
            let csoutdeleted=delete(dir."\\"."cscope.out")
        else
            let csoutdeleted=delete("./"."cscope.out")
        endif
        if(csoutdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
            return
        endif
    endif
    if(executable('ctags'))
        "silent!
        "execute
        ""!ctags
        -R --c-types=+p --fields=+S *"
        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
    endif
    if(executable('cscope') && has("cscope") )
        if(g:iswindows!=1)
            silent! execute "!find `pwd` -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"
        else
            silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
        endif
        silent! execute "!ctags -R"
        silent! execute "!cscope -bqR"
        execute "normal :"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
            set csverb
        endif
    endif
endfunction
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值