0

轻松使用cscope-tags-vim浏览C/C++源代码

hplll9876 2017-08-14 发布于工具 zhuanlan.zhihu.com

作为一个普通程序员,有时偶尔需要用SSH远程连接到Linux中,浏览C/C++或其它语言的源代码。虽然在Shell和vim中查看源代码,远远没有在Windows中用Visual Studio或Source Insight那么舒服,但在cscope和tags的帮助下,简单浏览一下源代码也是可以很轻松的。

轻松使用cscope-tags-vim浏览C/C++源代码

2017-08-14 发布,来源:zhuanlan.zhihu.com

作为一个普通程序员,有时偶尔需要用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 
CentOS Linux release 7.3.1611 (Core) 

yum安装cscopectagsctags-etags

[root@laosong ~]# yum list ctags* cscope*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
cscope.x86_64 15.8-9.el7 @base
ctags.x86_64 5.8-13.el7 @base
ctags-etags.x86_64 5.8-13.el7 @base

Bitcoin开源代码为例,通过ctags -R对源代码生成tags索引文件,通过cscope -Rb对源代码生成cscope.out索引文件。

[root@laosong src]# pwd
/root/OpenSource/bitcoin-master/src
[root@laosong src]# 
[root@laosong src]# ctags -R
[root@laosong src]# 
[root@laosong src]# cscope -Rb
[root@laosong src]# 
[root@laosong src]# ll -h tags cscope.out
-rw-r--r--. 1 root root 4.9M Aug 14 15:16 cscope.out
-rw-r--r--. 1 root root 1.2M Aug 14 15:15 tags
[root@laosong src]# 
[root@laosong src]# file tags
tags: Exuberant Ctags tag file, ASCII text, with very long lines
[root@laosong src]# 
[root@laosong src]# file cscope.out
cscope.out: cscope reference data version 15
[root@laosong src]# 

执行cscope,进入cscope交互式界面,该界面以全屏显示,按Ctrl+d可以退出,按?可查看帮助。默认光标在“Find this C symbol”处闪烁。这时可按UpDown在各个查找输入框中切换。

Find this C symbol:(光标默认在这里)
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
Find assignments to this symbol:

比如:键入“main”回车,即可查找源码中所有main函数所在位置。这时,光标自动切换到查找结果列表中的第一行。如果想让光标回到搜索输入框(即,“Find this C symbol”等), 则可按TAB进行切换。

C symbol: main

 File Function Line
0 bitcoin-cli.cpp main 312 int main(int argc, char * argv[])
1 bitcoin-tx.cpp main 678 int main(int argc, char * argv[])
2 bitcoind.cpp main 183 int main(int argc, char * argv[])

在查找结果列表中,每一行代表一个查找结果,每一行以数字或字母开头。当光标在查找结果列表中,按开头的数字或字母(比如:按1)即可调用默认文本编辑器(vim)自动打开以其开头的查找结果行(即,“bitcoin-tx.cpp”文件的“678”行)。

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

进入vim后,tags将发挥作用,接如上举例,将光标移动到“SetupEnvironment()”函数中,按Ctrl+],即可通过tags索引跳转到该函数的定义之处。

......
void SetupEnvironment()
{
 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
 // may be invalid, in which case the "C" locale is used as fallback.
......

进入vim后,输入ESC + :q 即可退出vim,回到cscope。在cscope中,在查找结果列表中继续浏览其它位置,或者按TAB以及Up/Down查找其它函数或变量。

还有一点要说明的是,如果搜索结果太多,比如在“Find functions called by this function”中搜索“main”,则会找到27行,一屏无法显示,cscope会分多屏显示,按SPACE(空格)可以翻页浏览。

Functions called by this function: main

 File Function Line
0 bitcoin-cli.cpp SetupEnvironment 314 SetupEnvironment();
1 bitcoin-cli.cpp SetupNetworking 315 if (!SetupNetworking()) {
2 bitcoin-cli.cpp fprintf 316 fprintf(stderr, "Error: Initializing networking failed\n");
3 bitcoin-cli.cpp AppInitRPC 321 if(!AppInitRPC(argc, argv))
4 bitcoin-cli.cpp catch 324 catch (const std::exception& e) {
5 bitcoin-cli.cpp PrintExceptionContinue 325 PrintExceptionContinue(&e, "AppInitRPC()");
6 bitcoin-cli.cpp catch 327 } catch (...) {
7 bitcoin-cli.cpp PrintExceptionContinue 328 PrintExceptionContinue(NULL, "AppInitRPC()");
8 bitcoin-cli.cpp CommandLineRPC 334 ret = CommandLineRPC(argc, argv);
9 bitcoin-cli.cpp catch 336 catch (const std::exception& e) {
a bitcoin-cli.cpp PrintExceptionContinue 337 PrintExceptionContinue(&e, "CommandLineRPC()");
b bitcoin-cli.cpp catch 338 } catch (...) {
c bitcoin-cli.cpp PrintExceptionContinue 339 PrintExceptionContinue(NULL, "CommandLineRPC()");
d bitcoin-tx.cpp SetupEnvironment 680 SetupEnvironment();
e bitcoin-tx.cpp AppInitRawTx 683 if(!AppInitRawTx(argc, argv))
f bitcoin-tx.cpp catch 686 catch (const std::exception& e) {
g bitcoin-tx.cpp PrintExceptionContinue 687 PrintExceptionContinue(&e, "AppInitRawTx()");

* Lines 1-18 of 27, 10 more - press the space bar to display more *
Find this C symbol:
Find this global definition:
Find functions called by this function: 
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
Find assignments to this symbol:

最后,按Ctrl+d退出cscope,返回Linux Shell。

Appendix:cscope help

Press the RETURN key repeatedly to move to the desired input field, type the
pattern to search for, and then press the RETURN key. For the first 4 and
last 2 input fields, the pattern can be a regcomp(3) regular expression.
If the search is successful, you can use these single-character commands:

0-9a-zA-Z Edit the file containing the displayed line.
space bar Display next set of matching lines.
+ Display next set of matching lines.
^V Display next set of matching lines.
- Display previous set of matching lines.
^E Edit all lines.
> Write the list of lines being displayed to a file.
>> Append the list of lines being displayed to a file.
< Read lines from a file.
^ Filter all lines through a shell command.
| Pipe all lines to a shell command.

At any time you can use these single-character commands:

TAB Swap positions between input and output areas.
RETURN Move to the next input field.
^N Move to the next input field.
^P Move to the previous input field.
^Y / ^A Search with the last pattern typed.
^B Recall previous input field and search pattern.
^F Recall next input field and search pattern.
^C Toggle ignore/use letter case when searching (USE).
^R Rebuild the cross-reference.
! Start an interactive shell (type ^D to return to cscope).
^L Redraw the screen.
? Display this list of commands.
^D Exit cscope.
Note: If the first character of the pattern you want to search for matches
a command, type a \ character first.
Note: Some ctrl keys may be occupied by your terminal configuration.
Reference:
0. 老宋的独家号
1. welcome home : vim online
2. Cscope Home Page
3. Exuberant Ctags
注:本文为老宋原创文章,转载前请至【知乎专栏-老宋的独家号】点赞或评论建议。

1.5k 浏览 收藏 报告 阅读模式
载入中...