標籤:

輕鬆使用cscope-tags-vim瀏覽C/C++源代碼

作為一個普通程序員,有時偶爾需要用SSH遠程連接到Linux中,瀏覽C/C++或其它語言的源代碼。雖然在Shell和vim中查看源代碼,遠遠沒有在Windows中用Visual Studio或Source Insight那麼舒服,但在cscope和tags的幫助下,簡單瀏覽一下源代碼也是可以很輕鬆的。

本文以CentOS 7.X為例,介紹通過cscope、tags、vim簡單瀏覽源代碼的基本方法。

[root@laosong ~]# cat /etc/redhat-release nCentOS Linux release 7.3.1611 (Core) n

yum安裝cscopectagsctags-etags

[root@laosong ~]# yum list ctags* cscope*nLoaded plugins: fastestmirrornLoading mirror speeds from cached hostfilenInstalled Packagesncscope.x86_64 15.8-9.el7 @basenctags.x86_64 5.8-13.el7 @basenctags-etags.x86_64 5.8-13.el7 @basen

Bitcoin開源代碼為例,通過ctags -R對源代碼生成tags索引文件,通過cscope -Rb對源代碼生成cscope.out索引文件。

[root@laosong src]# pwdn/root/OpenSource/bitcoin-master/srcn[root@laosong src]# n[root@laosong src]# ctags -Rn[root@laosong src]# n[root@laosong src]# cscope -Rkbn[root@laosong src]# n[root@laosong src]# ll -h tags cscope.outn-rw-r--r--. 1 root root 4.9M Aug 14 15:16 cscope.outn-rw-r--r--. 1 root root 1.2M Aug 14 15:15 tagsn[root@laosong src]# n[root@laosong src]# file tagsntags: Exuberant Ctags tag file, ASCII text, with very long linesn[root@laosong src]# n[root@laosong src]# file cscope.outncscope.out: cscope reference data version 15n[root@laosong src]# n

執行cscope(或者cscope -d,用於當代碼沒有變化時,-d速度更快),進入cscope互動式界面,該界面以全屏顯示,按Ctrl+d可以退出,按?可查看幫助。默認游標在「Find this C symbol」處閃爍。這時可按UpDown在各個查找輸入框中切換。

Find this C symbol:(游標默認在這裡)nFind this global definition:nFind functions called by this function:nFind functions calling this function:nFind this text string:nChange this text string:nFind this egrep pattern:nFind this file:nFind files #including this file:nFind assignments to this symbol:n

比如:鍵入「main」回車,即可查找源碼中所有main函數所在位置。這時,游標自動切換到查找結果列表中的第一行。如果想讓游標回到搜索輸入框(即,「Find this C symbol」等), 則可按TAB進行切換。

C symbol: mainnn File Function Linen0 bitcoin-cli.cpp main 312 int main(int argc, char * argv[])n1 bitcoin-tx.cpp main 678 int main(int argc, char * argv[])n2 bitcoind.cpp main 183 int main(int argc, char * argv[])n

在查找結果列表中,每一行代表一個查找結果,每一行以數字或字母開頭。當游標在查找結果列表中,按開頭的數字或字母(比如:按1)即可調用默認文本編輯器(vim)自動打開以其開頭的查找結果行(即,「bitcoin-tx.cpp」文件的「678」行)。

......nint main(int argc, char* argv[])n{n SetupEnvironment();n......n

進入vim後,tags將發揮作用,接如上舉例,將游標移動到「SetupEnvironment()」函數中,按Ctrl + ],即可通過tags索引跳轉到該函數的定義之處;用 Ctrl + t 可以返回。

......nvoid SetupEnvironment()n{n // On most POSIX systems (e.g. Linux, but not BSD) the environments localen // may be invalid, in which case the "C" locale is used as fallback.n......n

進入vim後,輸入ESC + :q 即可退出vim,回到cscope。在cscope中,在查找結果列表中繼續瀏覽其它位置,或者按TAB以及Up/Down查找其它函數或變數。

還有一點要說明的是,如果搜索結果太多,比如在「Find functions called by this function」中搜索「main」,則會找到27行,一屏無法顯示,cscope會分多屏顯示,按SPACE(空格)可以翻頁瀏覽。

Functions called by this function: mainnn File Function Linen0 bitcoin-cli.cpp SetupEnvironment 314 SetupEnvironment();n1 bitcoin-cli.cpp SetupNetworking 315 if (!SetupNetworking()) {n2 bitcoin-cli.cpp fprintf 316 fprintf(stderr, "Error: Initializing networking failedn");n3 bitcoin-cli.cpp AppInitRPC 321 if(!AppInitRPC(argc, argv))n4 bitcoin-cli.cpp catch 324 catch (const std::exception& e) {n5 bitcoin-cli.cpp PrintExceptionContinue 325 PrintExceptionContinue(&e, "AppInitRPC()");n6 bitcoin-cli.cpp catch 327 } catch (...) {n7 bitcoin-cli.cpp PrintExceptionContinue 328 PrintExceptionContinue(NULL, "AppInitRPC()");n8 bitcoin-cli.cpp CommandLineRPC 334 ret = CommandLineRPC(argc, argv);n9 bitcoin-cli.cpp catch 336 catch (const std::exception& e) {na bitcoin-cli.cpp PrintExceptionContinue 337 PrintExceptionContinue(&e, "CommandLineRPC()");nb bitcoin-cli.cpp catch 338 } catch (...) {nc bitcoin-cli.cpp PrintExceptionContinue 339 PrintExceptionContinue(NULL, "CommandLineRPC()");nd bitcoin-tx.cpp SetupEnvironment 680 SetupEnvironment();ne bitcoin-tx.cpp AppInitRawTx 683 if(!AppInitRawTx(argc, argv))nf bitcoin-tx.cpp catch 686 catch (const std::exception& e) {ng bitcoin-tx.cpp PrintExceptionContinue 687 PrintExceptionContinue(&e, "AppInitRawTx()");nn* Lines 1-18 of 27, 10 more - press the space bar to display more *nFind this C symbol:nFind this global definition:nFind functions called by this function: nFind functions calling this function:nFind this text string:nChange this text string:nFind this egrep pattern:nFind this file:nFind files #including this file:nFind assignments to this symbol:n

最後,按Ctrl+d退出cscope,返回Linux Shell。作為總結,在此將本文中用的命令/按鍵羅列如下:

yum list ctags* cscope* yum install ctags* cscope* ctags -R cscope -Rb cscope ? TAB Up/Down SPACE Ctrl+] ESC + :q Ctrl+d

Appendix:cscope help

Press the RETURN key repeatedly to move to the desired input field, type thenpattern to search for, and then press the RETURN key. For the first 4 andnlast 2 input fields, the pattern can be a regcomp(3) regular expression.nIf the search is successful, you can use these single-character commands:nn0-9a-zA-Z Edit the file containing the displayed line.nspace bar Display next set of matching lines.n+ Display next set of matching lines.n^V Display next set of matching lines.n- Display previous set of matching lines.n^E Edit all lines.n> Write the list of lines being displayed to a file.n>> Append the list of lines being displayed to a file.n< Read lines from a file.n^ Filter all lines through a shell command.n| Pipe all lines to a shell command.nnAt any time you can use these single-character commands:nnTAB Swap positions between input and output areas.nRETURN Move to the next input field.n^N Move to the next input field.n^P Move to the previous input field.n^Y / ^A Search with the last pattern typed.n^B Recall previous input field and search pattern.n^F Recall next input field and search pattern.n^C Toggle ignore/use letter case when searching (USE).n^R Rebuild the cross-reference.n! Start an interactive shell (type ^D to return to cscope).n^L Redraw the screen.n? Display this list of commands.n^D Exit cscope.nNote: If the first character of the pattern you want to search for matchesna command, type a character first.nNote: Some ctrl keys may be occupied by your terminal configuration.n

Reference:

0. 老宋的獨家號

1. welcome home : vim online

2. Cscope Home Page

3. Exuberant Ctags

註:本文為老宋原創文章,轉載前請至【知乎專欄-老宋的獨家號】點贊或評論建議。

推薦閱讀:

vim 啟動速度優化的一些經驗
Vim - 配置IDE一般的python環境
Vim 折騰記
打造Python開發工具——vim+zsh+tmux
SpaceVim 模塊化狀態欄

TAG:Linux | Vim | CC |