1. 程式人生 > >Cscope how to support java and c++

Cscope how to support java and c++

Cscope

首先在目錄下建立cscope索引檔案

find -name '*.c' > cscope.file

cscope -Rbkq

這個命令會生成三個檔案:cscope.out, cscope.in.out, cscope.po.out。
其中cscope.out是基本的符號索引,後兩個檔案是使用"-q"選項生成的,可以加快cscope的索引速度。
上面所用到的命令引數,含義如下:

-R: 在生成索引檔案時,搜尋子目錄樹中的程式碼
-b: 只生成索引檔案,不進入cscope的介面
-k: 在生成索引檔案時,不搜尋/usr/include目錄
-q: 生成cscope.in.out和cscope.po.out檔案,加快cscope的索引速度接下來,就可以在vim裡讀程式碼了。
不 過在使用過程中,發現無法找到C++的類、函式定義、呼叫關係。仔細閱讀了cscope的手冊後發現,原來cscope在產生索引檔案時,只搜尋型別為 C, lex和yacc的檔案(字尾名為.c, .h, .l, .y),C++的檔案根本沒有生成索引。不過按照手冊上的說明,cscope支援c++和Java語言的檔案。
於是按照cscope手冊上提供的方法,先產生一個檔案列表,然後讓cscope為這個列表中的每個檔案都生成索引。
為了方便使用,編寫了下面的指令碼來更新cscope和ctags的索引檔案:

#!/bin/sh

find . -name "*.h" -o -name "*.c" -o -name "*.cc"  -o -name "*.java"> cscope.files
cscope -bkq -i cscope.files
ctags -R

這個指令碼,首先使用find命令,查詢當前目錄及子目錄中所有後綴名為".h", ".c"和".cc"的檔案,並把查詢結果重定向到檔案cscope.files中。
然後cscope根據cscope.files中的所有檔案,生成符號索引檔案。
最後一條命令使用ctags命令,生成一個tags檔案,在vim中執行":help tags"命令查詢它的用法。它可以和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,這樣,你不用切換到你資料庫檔案所在的目錄也可以使用它了。
3在vim裡讀程式碼
在VIM中使用cscope非常簡單,首先呼叫“cscope add”命令新增一個cscope資料庫,然後就可以呼叫“cscope find”命令進行查找了。VIM支援8種cscope的查詢功能,如下:例如,我們想在程式碼中查詢呼叫work()函式的函式,我們可以輸入:“:cs find c work”,回車後發現沒有找到匹配的功能,可能並沒有函式呼叫work()。我們再輸入“:cs find s work”,查詢這個符號出現的位置,現在vim列出了這個符號出現的所有位置。我們還可以進行字串查詢,它會雙引號或單引號括起來的內容中查詢。還可以輸入一個正則表示式,這類似於egrep程式的功能。
s: 查詢C語言符號,即查詢函式名、巨集、列舉值等出現的地方
g: 查詢函式、巨集、列舉等定義的位置,類似ctags所提供的功能
d: 查詢本函式呼叫的函式
c: 查詢呼叫本函式的函式
t: 查詢指定的字串
e: 查詢egrep模式,相當於egrep功能,但查詢速度快多了
f: 查詢並開啟檔案,類似vim的find功能
i: 查詢包含本檔案的文

cs help

find 的選項

0或則S:查詢本符號

1或則G:查詢本定義

2或則D:查詢本函式呼叫的函式

3或則C:查詢呼叫本函式的函式

4或則T:查詢本字串

6或則E:查詢本EGREP模式

7或則F:查詢本檔案

8或則I:查詢包含本檔案的檔案

熱後就可以在vim中使用cscope了,具體使用方法參考

//----------------------------------------------------------------------

--------------------------------------------------------------------------------
cscope是什麼?
cscope 是一個 C 語言的瀏覽工具,通過這個工具可以很方便地找到某個函式或變數的定義位置、被呼叫的位置等資訊。目前支援 C 和 C++。cscope 自身帶一個基於文字的使用者介面,不過 gvim 提供了cscope介面,因此可以在 gvim 中呼叫 cscope,方便快捷地瀏覽原始碼。

為什麼要使用cscope?
假設我們在讀一份很大的專案的原始碼。我們也許會需要進行如下操作。

•函式 foo() 呼叫了函式 bar(),想看看函式 bar() 的內容。•想知道 foo() 在什麼地方被呼叫。•想知道一個結構型別的定義。雖然在 Linux 下使用 grep 或者在 Windows 下使用檔案查詢功能也能找到想找的檔案,但是效率太低了。有沒有什麼更為方便快捷的方法?

這就需要用到cscope。gvim結合cscope,可以很方便地完成以上的操作,只需簡單地敲幾下鍵盤即可跳轉到想去的地方。

如何使用cscope?
 

下載
如果你使用的是Linux,那麼恭喜你,很可能作業系統已經為你提供了cscope工具。使用下面的命令確認它是否存在:

cscope -V如果cscope工具已經安裝,上面的命令將顯示cscope的版本號,類似於下面的結果:

cscope: version 16.0a但是如果提示錯誤說找不到cscope命令,說明cscope尚未安裝。你可以到網上去尋找cscope的RPM包。另外,我們可以從cscope官方網站上下載到 cscope 的最新原始碼。在Linux下你可以編譯該原始碼生成cscope的可執行檔案。

如果你是Windows使用者,就沒有這麼好的福氣了,因為在Windows下編譯程式並不是很簡單的事情。好在已經有人為我們編譯好了Windows版,可以從這裡下載到:http://iamphet.nm.ru/cscope/

安裝
安裝很簡單,只要將cscope的可執行檔案放到PATH環境變數包含的目錄中即可。推薦Windows使用者將上面下載到的 cscope.exe 與 gvim 放在同一個目錄下,如 C:\Program Files\Vim\gvim64 下,然後單擊開始選單選擇執行,輸入 cmd,啟動命令列提示符程式,執行

cscope -V如果能看到cscope的版本好則說明安裝成功。

另外,為了方便地使用cscope,我們還需要下載cscope的鍵盤對映設定,這樣就可以在gvim中簡單地通過快捷鍵來使用 cscope,而不必敲複雜的命令了。鍵盤對映可以從這裡下載:http://cscope.sourceforge.net/cscope_maps.vim

將下載到的 cscope_maps.vim 放在gvim的外掛目錄裡,如 C:\Program Files\Vim\vimfiles\plugin 中。Linux使用者可以放在 $HOME/.vim/plugin 中。

建立符號資料庫
我們假設我們要閱讀的程式碼放在 D:\src\myproject 下。然後開啟命令列,進入原始碼所在的目錄,為 cscope 建立搜尋檔案列表。在命令列中執行以下命令:

dir /s /b *.c *.h  > cscope.files如果你的原始碼是C++,則可以將 cpp 等副檔名也加入到上面的命令中。

dir /s /b *.c *.h *cpp *.hpp  > cscope.files如果是Linux使用者,則可以使用 find 命令實現同樣的功能:

find $(pwd) -name "*.[ch]"然後執行以下命令:

cscope -b執行結束後你可以在當前目錄下發現 cscope.out 檔案,這就是 cscope 建立的符號資料庫。

上面這個命令中,-b引數使得cscope不啟動自帶的使用者介面,而僅僅建立符號資料庫。

瀏覽原始碼
使用 gvim 開啟你的原始碼目錄中任意一個C程式檔案。然後在gvim中執行如下命令:

:cscope add D:\src\myproject\cscope.out由於在 gvim 中可以使用命令縮寫,因此上面的命令可以寫成:

:cs a D:\src\myproject\cscope.out這樣就打開了剛剛建立的符號資料庫。通過下面的命令可以檢查資料庫連線的存在。

:cscope show該命令可以縮寫為

:cs s現在將游標移動到原始碼中的某個函式名上,依次按下一下組合鍵:

<C-\>s稍等片刻之後你會在螢幕下放看到如下的字樣*1:

Cscope tag: display   #   line  filename / context / line   1    342  D:\src\myproject\src\global.h <<GLOBAL>>             void display(void );   2    616  D:\src\myproject\src\command.c <<changestring>>             display();   3    138  D:\src\myproject\src\display.c <<display>>             display(void )   4    385  D:\src\myproject\src\main.c <<main.c>>             display();   5    652  D:\src\myproject\src\main.c <<main.c>>             display();   6    663  D:\src\myproject\src\main.c <<main.c>>             display();Enter nr or choice (<CR> to abort):這裡顯示出的就是整個工程中使用到了 display 這個識別符號的位置。此時輸入 4,回車,即可跳轉到 main.c 的 385 行呼叫 display() 函式的地方進行瀏覽。瀏覽結束後按 <C-T> 或者 <C-O> 可以回到跳轉前的位置。

然後將游標移動到原始碼某個函式名上,迅速地依次安下面的組合鍵:

<[email protected]>s其中 <[email protected]> 按 Ctrl-2 即可輸入。同樣,螢幕上出現了一排結果,選擇之後你會發現,跳轉到的檔案將在水平方向的新視窗中開啟。

然後將游標移動到原始碼某個函式名上,迅速地依次安下面的組合鍵:

<[email protected]><[email protected]>s選擇之後你會發現,跳轉到的檔案將在垂直方向的新視窗中開啟。

以上我們簡單介紹了cscope的使用方法,其中我們只用到了一個 s 命令,即跟在 <C-\> 和 <[email protected]> 後面的 s 鍵。同樣,我們可以使用以下的功能鍵實現不同的跳轉功能。

•c: 查詢該函式被呼叫的位置•d: 查詢該函式呼叫了哪些函式•e: 查詢指定的正規表示式•f: 查詢指定的檔案•g: 查詢指定識別符號的定義位置•i: 查詢該檔案在哪些地方被包含•s: 查詢指定識別符號的使用位置•t: 查詢指定的文字字串命令列使用說明
除了上述通過快捷鍵對映的方式使用cscope之外,也可以直接在gvim命令列中使用cscope。這樣就可以隨意定義查詢字串,而不必侷限於原始碼中已有的識別符號。命令格式如下:

:cscope find <c|d|e|f|g|i|s|t> <關鍵字>該命令可以縮寫為

:cs f <c|d|e|f|g|i|s|t> <關鍵字>一個比較實用的技巧是使用cscope開啟檔案。使用以下命令即可直接開啟名為display.c的檔案,而不必先切換到display.c所在的目錄。

:cs f f display.ccscope也支援正規表示式。如果記不清某個函式的名稱,可以用下面的方式來找到該函式的定義位置。

:cs f g .*SetConfiguration.*

相關推薦

Cscope how to support java and c++

Cscope 首先在目錄下建立cscope索引檔案 find -name '*.c' > cscope.file cscope -Rbkq 這個命令會生成三個檔案:cscope.out, cscope.in.out, cscope.po.out。 其中cscope.

How to read version (and other) information from Android and iOS apps using Java

How to read version (and other) information from Android and iOS apps using Java https://medium.com/@mart.schneider/how-to-read-version-and-oth

Ask HN: How to support and groom polyopoly to destroy monopoly?

To destroy monopoly the first step is to achieve polyopoly where multiple small companies are being used and supported, they may be doing some unpleasant s

SuiteScript Tutorial - How to use it and why use it?

What you will learn: What SuiteScript is? How to create a Script record in NetSuite? How to write and upload a JavaScript file? How to

How to manually BEGIN and END transactions?

程式愈寫愈複雜,怕資料不一致,所以 connection 的 isolation_level 設到 None = auto commit mode. 雖然,沒有下 commit() 不會寫到 database 裡,但由於為了效能,我偷偷的把 connection 放在記憶體裡重覆使用,connection

How to use *args and **kwargs in Python

這篇文章寫的滿好的耶,結論: 1星= array, 2星=dictionary. 1星範例: def test_var_args(farg, *args): print "formal arg:", farg for arg in args: print "an

How to compare strings in C conditional preprocessor-directives

想使用 #define 寫code 在 preprocessor 裡,會顯示錯誤: Invalid token at start of preprocessor expression 解法: 解法 1,換成流水號: #define USER_JACK 1 #define USER_QUEEN 2

How to safely charge and store lithium drone batteries

This post was done in partnership with Wirecutter. When readers choose to buy Wirecutter's independently chosen editorial picks, Wirecutter and Engadget ma

Privacy By Design: How To Sell Privacy And Make Change

Joe Toscano is an award-winning designer and former consultant for Google who left in 2017 due to ethical concerns. Upgrade your inbox and get our editors'

How to speak up and impact conversations as a junior designer

How to speak up and impact conversations as a junior designerA large part of my week is spent in meeting rooms, design critiques, and spontaneous discussio

How to Be Lazy and Stay Calm

What frustrates me most in my profession of software development is the regular necessity to understand large problem scopes before fixing small bugs, espe

Top 5 Free Data Structure and Algorithm Courses for Java and C Programmers

Data Structure and Algorithm is one of the essential topics for programmers, both to get a job and do well on Job. A good knowledge of data structure and

Command Magicks: How to Manipulate Files and Strings with the Console

Command Magicks: How to Manipulate Files and Strings with the ConsoleProgramming will make you be amazed by the Cosmos. Source: Pixabay.As developers, ther

How to deploy Kubernetes and Containerum on Digital Ocean

How to deploy Kubernetes and Containerum on Digital Oceanby Nikita MazurForewordSeveral days ago we decided that Containerum Online — a hosted PaaS for lau

How to predict likes and shares based on your article’s title using Machine Learning

Some of the most used platforms to spread ideas nowadays are Twitter and Medium (you are here!). On Twitter, articles are normally posted including externa

Nerd Fonts: How to install, configure, and remove programming fonts on a mac

Nerd Fonts: How to install, configure, and remove programming fonts on a macPatched programming fonts, such as Nerd Fonts, are those that have been amended

How to Marry UX and AI to Outdo Your Competition

Ashley Sams is a consultant at PR 20/20. She joined the agency in 2017 with a background in marketing, specifically for higher education and social media.

How to Diagnose Overfitting and Underfitting of LSTM Models

Tweet Share Share Google Plus It can be difficult to determine whether your Long Short-Term Memo

How to Detect Analyze and Compare Faces with Amazon Rekognition

In this tutorial, you will learn how to use the face recognition features in Amazon Rekognition using the AWS Console. Amazon Rekognition is a d

How-to: resolve "java.lang.NoClassDefFoundError: org/htrace/Trace" when hbase Export

Caused by: java.lang.NoClassDefFoundError: org/htrace/Trace at org.apache.hadoop.hbase.client.ResultBoundedCompletionService.submit(ResultBoundedC