为了方便在Linux下,快速阅读修改代码,可以使用vim+ctags+cscope的方式
一、安装ctags与cscope
Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置、被调用的位置等信息。Cscope对 C /C++支持较好,也可以自己定制来支持Java和Perl、Python等脚本语言。Vim和gvim都提供了cscope接口,通过适当的配置,可以在Unix/Linux下实现变量、函数、文件等之间跳转,就像Windows下的Source Insight一样灵活易用。由于cscope是开源免费的,而且配合vim可以脱离鼠标,实现全键盘操作,方便快捷地浏览源代码。
ctags(Generate tag files for source code)是vim下方便代码阅读的工具。尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM。并且VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码。
#安装ctags
sudo apt install exuberant-ctags -y
#安装cscope
sudo apt install cscope
在内核顶层目录中建立索引
make tags
make cscope
二、cscope用法
Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]
[-p number] [-P path] [-[0-8] pattern] [source files]
-b Build the cross-reference only.
-C Ignore letter case when searching.
-c Use only ASCII characters in the cross-ref file (don't compress).
-d Do not update the cross-reference.
-e Suppress the <Ctrl>-e command prompt between files.
-F symfile Read symbol reference lines from symfile.
-f reffile Use reffile as cross-ref file name instead of cscope.out.
-h This help screen.
-I incdir Look in incdir for any #include files.
-i namefile Browse through files listed in namefile, instead of cscope.files
-k Kernel Mode - don't use /usr/include for #include files.
-L Do a single search with line-oriented output.
-l Line-oriented interface.
-num pattern Go to input field num (counting from 0) and find pattern.
-P path Prepend path to relative file names in pre-built cross-ref file.
-p n Display the last n file path components.
-q Build an inverted index for quick symbol searching.
-R Recurse directories for files.
-s dir Look in dir for additional source files.
-T Use only the first eight characters to match against C symbols.
-U Check file time stamps.
-u Unconditionally build the cross-reference file.
-v Be more verbose in line mode.
-V Print the version number.
#当前目录下递归建立索引
cscope -Rbkq
#在文件中的用法 打开一个文件
:cs help
cscope commands:
add : Add a new database (Usage: add file|dir [pre-path] [flags])
find : Query for a pattern (Usage: find a|c|d|e|f|g|i|s|t name)
a: Find assignments to this symbol
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find this text string
help : Show this message (Usage: help)
kill : Kill a connection (Usage: kill #)
reset: Reinit all connections (Usage: reset)
show : Show connections (Usage: show)
#添加一个database
:cs add cscope.out
#找到register_chrdev这个函数
:cs find d register_chrdev
三、ctags用法
#当前目录下所有文件生成tags
ctags -R *
#跳转到函数
:tag function
#添加tags
:set tags=./tags
#分割窗口
:stag
Ctrl + ] :跳转到函数定义
Ctrl + t :跳转到上一次的位置
Ctrl + w + ] :保留当前窗口 跳转到新的函数定义并打开新的窗口