1. $ sudo apt-get install ctags cscope

2. Ctrl+] when cursor at some function name, variable or structure.

3. Ctrl+t to go back to the previous position

4. 在使用cscope之前需要先生成一個數據庫
a  生成索引文件:
 cscope 用於生成索引文件的選項:
-R: 在生成索引文件時,搜索子目錄樹中的代碼
-b: 只生成索引文件,不進入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成索引文件時,不搜索/usr/include目錄
-i: 如果保存文件列表的文件名不是cscope.files時,需要加此選項告訴cscope到
    哪兒去找源文件列表。能使用「-」,表示由標準輸入獲得文件列表。
-I dir: 在-I選項指出的目錄中查找頭文件
-u: 掃瞄所有文件,重新生成交叉索引文件
-C: 在搜索時忽略大小寫
-P path: 在以相對路徑表示的文件前加上的path,這樣,你不用轉換到你數據庫
    文件所在的目錄也能使用他了。

如果你的源碼中只含有.c,.h文件,你能直接使用
$ cscope -Rbq

在使用cscope之前需要先生成一個數據庫,你可以使用cscope-indexer(如果多個
目錄你可以使用-R選項),它會在當前目前下生成一個cscope.files的文件,這個文
件包含了cscope需要生成索引的全部文件,因為cscope-indexer不會自動查到.cpp,.S
後綴的文件,因此最後使用find來生成cscope.files文件:
$ find ./ -name 「*.c」 -or -name 「*.h」 -or -name 「*.cpp」 > cscope.files

上面的命令會把當前目錄下所有.c,.h,.cpp文件列出並寫入cscope.files文件中。
接著使用cscope -Rbq來生成索引引。
$ cscope -Rbq

一般使用方法:
$ find ./ -name "*.c" -or -name "*.h" -or -name "*.cpp" -or -name "*.S" | cscope -Rbq

b  接著你就能使用vim來打開一個文件來瀏覽編輯代碼了。
想查看cscope的使用方法最好的辦法是在vim中輸入:
:cs help

這裡還是稍稍說明一下:
s: 查找C語言符號,即查找函數名、宏、枚舉值等出現的地方
g: 查找函數、宏、枚舉等定義的位置,類似ctags所提供的功能
d: 查找本函數調用的函數
c: 查找調用本函數的函數
t: 查找指定的字符串
e: 查找egrep模式,相當於egrep功能,但查找速度快多了
f: 查找並打開文件,類似vim的find功能
i: 查找包含本文件的文

================ vim settings ===================

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has("cscope")
  set csprg=/usr/bin/cscope
  set csto=1
  set cst
  set nocsverb
  " add any database in current directory
  if filereadable("cscope.out")
      cs add cscope.out
  endif
  set csverb
endif

" [S] Find this C symbol
nmap cs :cs find s

" [G] Find this definition
nmap cg :cs find g

" [C] Find functions calling this function
nmap cc :cs find c

" [T] Find this text string
nmap ct :cs find t

" [E] Find this egreo pattern
nmap ce :cs find e

nmap cf :cs find f

" [I] Find files #including this file
nmap ci :cs find i ^=expand("")$

" [D] Find functions called by this function
nmap cd :cs find d

arrow
arrow
    全站熱搜

    forteallan 發表在 痞客邦 留言(0) 人氣()