"Cscope" Installation and Activation on Mac / 在Mac上安裝及使用Cscope
Preface / 前言:
大二下時,修了一門Web Searching and Mining的課,由於寫專案時,trace code需要花上很多時間,發現安裝一下cscope套件,可以增加開發速度以及方便性。但在安裝及執行過程卻是處處碰壁,我想藉由這次寫專案及操作cscope的經驗,向大家介紹一下cscope以及安裝、執行的教學。
<#!> 在安裝前,建議使用mac的朋友先去下載homebrew,它可以幫助我們完成很多任務。
Installation Steps / 安裝步驟:
<Step1> open Terminal / 將終端機開啟
<Step2> Run in Terminal app / 將指令輸入並執行
ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
<Step3> Run / 以brew指令安裝cscope
brew install cscope
After Installation / 安裝完成後:
(以我本次著手的課程專案為例)
<Step1> 建立資料庫cscope.files,用find把你要trace的檔案都找出來。
這部分我在建立時,我會進入到我要trace的files的根目錄,舉例來說是當前專案的根目錄,接著輸入以下的指令,cscope會幫忙你find在這個路徑下的所有.cpp與.hpp檔,整理起來放到你所給的路徑的cscope.files檔案內。執行完後可以less cscope.files看一下是否已整理好。
find /Users/harenlin/Desktop/NCCU1072/wsmproject2/indri-5.14
-name '*.cpp' -o -name '*.hpp' > /Users/harenlin/Desktop/NCCU1072/wsmproject2/indri-5.14/cscope.files
<Step2> 不要進入圖形化介面:-b。加快查詢速度:-q -k。
[此部分說明較為簡略,若有需要可以查詢cscope的command詳細解釋]
cscope -b -q -k
<Step3> 宣告環境變數,方可使用cscope來trace code
CSCOPE_DB=/Users/harenlin/Desktop/NCCU1072/wsmproject2/indri-5.14/cscope.outexport CSCOPE_DB
Cscope + Vim:
如果在操作cscope時,也能結合進去vim,那會極為方便。在cscope的官方網站,他們有提供cscope_maps.vim的參考,可以從裡面擷取一些資訊加入自己的.vimrc中。以下是我的截取及修改後的cscope/vim設定:
set cscopetagset csto=0if filereadable("cscope.out") cs add cscope.outelseif $CSCOPE_DB != "" cs add $CSCOPE_DBendifset cscopeverbosenmap zs :cs find s <C-R>=expand("<cword>")<CR><CR>nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR>nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR>nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR>nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR>nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR>nmap zi :cs find i <C-R>=expand("<cfile>")<CR>$<CR>nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR>nmap qs :vert scs find s <C-R>=expand("<cword>")<CR><CR>nmap qg :vert scs find g <C-R>=expand("<cword>")<CR><CR>nmap qc :vert scs find c <C-R>=expand("<cword>")<CR><CR>nmap qt :vert scs find t <C-R>=expand("<cword>")<CR><CR>nmap qe :vert scs find e <C-R>=expand("<cword>")<CR><CR>nmap qf :vert scs find f <C-R>=expand("<cfile>")<CR><CR>nmap qi :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>nmap qd :vert scs find d <C-R>=expand("<cword>")<CR><CR>
Example / 操作範例:
假設我現在要再專案中的檔案找到71行的JelineMercerTermScoreFunction有在哪裡出現,按下zs後(此指令為上述融合vim的設定),會跳出選項提供使用者挑選。
接著按下1以及enter,表示我要進去它這個class的function定義處的檔案。
若想要跳回原本正在寫的檔案,按下 contral + T 就能回到原始檔案了。
<p.s.> 若你使用的是跳出分割的視窗來trace code,則切換上下左右視窗的指令如下:
control+W with 上下左右鍵
Auto create and update cscope database / 自動建立cscope資料庫:
我的.vimrc設定如下:
map <C-D> :!cscope -R -b -q -k<CR>:cs kill -1<CR>:cs add ./cscope.out<CR>
如此一來能在進入根目錄時,打開vim按下 control + D,即能自動建立資料庫了。但有個缺點是如果你要trace在這個目錄下的子目錄中的檔案,則必續在這個目錄中以絕對路徑打開此檔案。e.g. vim /subfolder/example.cpp,這樣才能順利trace檔案,否則你打cscope指令時會產生一個no cscope connection的窘境。如何解決此類缺點,安裝ack套件。
Other Reference / 其他參考資料:
Related Toolkits: Codequery
Extended Reading / 延伸閱讀:
This article will be updated at any time! Thanks for your reading. If you like the content, please click the “clap” button. You can also press the follow button to track new articles. Feel free to connect with me via LinkedIn or email.