Vim+cscope+ctags+tags Reading source code

Source: Internet
Author: User
Tags function definition egrep

Http://zhwen.org/?p=articles/cscope-tags

The Linux source code provides make commands for making Cscope index files and tags files. First , the installation of software installation Cscope:

sudo apt-get install Cscope
In the installation ctags:
http://ctags.sourceforge.net/

You can use: Ctags–version to see if the version of the current ctags is Emacs.
If you installed Emacs, you must use the above address of the Ctags,vim does not support Emacs Ctags, so to download the installation of this ctags, downloaded in the source file directory running:
helight@helight-desktop:~/desktop/ctags-5.7$  ./configure
helight@helight-desktop:~/desktop/ctags-5.7$ Make
You can generate Ctags files, save this file to the "/usr/bin/" directory. Two, generate index fileUse two commands in the source code directory to generate Cscope index files and tags files.
helight@helight-desktop:~/downloads/linux-2.6.26$ make Cscope
filelst cscope.files
make    cscope.out
helight@helight-desktop:~/downloads/linux-2.6.26$ Make tags
Make   tags
helight@helight-desktop:~/downloads/linux-2.6.26$ ls tags-l
-rw-r–r–1 helight ftp 69274841 2008-09-27 11:04 tags
helight@helight-desktop:~/downloads/linux-2.6.26$ ls cscope.*-l-rw-r–r–1 helight ftp<
c10/>23814144 2008-09-25 20:08 cscope.in.out
-rw-r–r–1 helight ftp    609665
2008-09-25 20:06 cscope.list -rw-r–r–1 helight ftp 174470617 2008-09-25 20:08 cscope.out-rw-r–r-1
helight ftp 124973256 2008-09-25 20:08 cscope . po.out

third, generate index files in general source filesFor ease of use, the following script was written to update the index files for Cscope and ctags:
#!/bin/sh find
.-name "*.h"-o-name "*.c"-o-name "*.cc" > Cscope.files cscope-bkq-i cscope.files
C Tags-r

This command generates three files: Cscope.out, Cscope.in.out, Cscope.po.out.
Where Cscope.out is the basic symbolic index, the latter two files are generated using the "-q" option to speed up indexing of cscope.
This script, first using the Find command, finds all the files in the current directory and subdirectories named ". H", ". C" and ". C", and redirects the lookup results to the file cscope.files. Cscope then generates the symbol index file based on all the files in the cscope.files. The last command uses the Ctags command, generates a tags file, in vim executes ": Help tags" command query its usage. It can be used with cscope. The command arguments used above have the following meanings:
-r: Searching for code in a subdirectory tree when generating index files
-B: Only index files are generated and do not enter the Cscope interface
-Q: Generate Cscope.in.out and cscope.po.out files to speed up cscope indexing
-K: Do not search the/usr/include directory when generating index files
-I: If you save the file list with a filename that is not cscope.files, you need to add this option to tell Cscope where to find the source file list. You can use "-" to indicate that a list of files is obtained from standard input.
-I dir: Find header file in the directory indicated by-i option
-U: Scans all files, rebuilds the cross index files
-C: Ignore case when searching
-P Path: path in front of a file represented by a relative path, so you can use it without switching to the directory where your database files are located.
four, using Cscope to find in Vim1, loading Cscope.out file
Using Cscope in Vim is very simple, first call the "cscope Add" command to add a cscope database, and then you can call the "cscope find" command to look up. VIM supports 8 kinds of cscope query functions, as follows: For example, we want to find a function in the code to call the work () function, we can enter: ": CS Find C Work", carriage return found no matching function, may not have function call work (). We then enter ": CS find s work", looking for where this symbol appears, now vim lists all the locations where this symbol appears. We can also do a string lookup, which can be found in double quotes or in single quotes. You can also enter a regular expression, which is similar to the functionality of the EGREP program.
Open vim under the source code directory. To use the Cscope lookup, you must load the Cscope.out file. Execute on the VIM command line:
: CS Add Cscope.out
Execute under the VIM command line:
: CS Help
Cscope commands:
add  : Add a new Database             (Usage:add file|dir [Pre-path] [flags])
Find:que Ry for a            (usage:find c|d|e|f|g|i|s|t name)
       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 fil Es #including This file
       s:find this C symbol
       t:find assignments to
help:show this message              (Usage: Help)
Kill:kill a connection              (Usage:kill #)
reset:reinit all Connections         (Usage:reset)
Show:show Connections               (usage:show)

S: Find the C language symbol, that is, find the function name, macro, enumeration value, etc. where it appears
G: Find locations defined by functions, macros, enumerations, and so on, similar to the functionality provided by Ctags
D: Find the function called by this function
C: Find functions that call this function
T: Find the specified string
E: Find Egrep mode, which is equivalent to Egrep function, but the search speed is much faster
F: Find and open files, similar to the VIM found function
I: Find the article containing this document

2, use Cscope to find the definition of the Do_fork function:
Execute under the VIM command line:
: CS F G do_fork
Five, use tags to find symbols in vim:Execute under the VIM command line:
: Tag xxx
You can find the function you are looking for or data structure or function xxx
Other uses of tags on the vim in the implementation of the ": Help Tags" command to query. Six, other orders introduced:CTRL +]: Jumps to the function definition where the function is called
Ctrl+t: Return to the previous lookup place
Seven, special attention:Generated cscope.out and tags files to open vim in the folder, otherwise vim could not find the relevant symbolic information.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.