GNU Global 的使用

Last updated on July 16, 2023 am

☆ 1. GNU Global 简介

GNU Global 是一种源代码标记系统,可以帮助程序员在代码库中快速定位函数、变量、宏定义等。GNU Global 可以生成索引文件,用于快速跳转到定义、引用、函数调用等地方,从而提高代码阅读和编辑的效率。

GNU global 具有以下优势:

  1. 多语言支持:在默认情况 GNU Global 支持 C, C++, Yacc, Java, PHP4 和 assembly 语言,在安装拓展的情况下可以支持 50多种语言。
  2. 完整性:GNU global 可以标记更多符号的位置,如定义、引用、结构体、类,宏定义等。
  3. 高效性:GNU global 生成高效的 tag 数据库,节省了磁盘空间,使得查询速度更快,同时也支持增量更新索引文件,这意味着当源代码发生变化时,只需要重新生成变更的部分,而不需要重新生成整个索引文件。
  4. 可集成:可以在 Emacs, Vi /vim, Less viewer, Bash shell, 浏览器环境下正常使用
  5. 持续更新: https://www.gnu.org/software/global/whatsnew.html

更多信息请参考: https://www.gnu.org/software/global/globaldoc_toc.html 1.3 Features

☆ 2. GNU Global 的安装

下载地址: https://ftp.gnu.org/pub/gnu/global/global-6.6.9.tar.gz

https://www.gnu.org/software/global/download.html 中说明了源码编译方法:

1
2
3
% sh reconf.sh 
% ./configure
% make

Fedora/Debian 等系统可以使用仓库直接安装,下面以 Fedora Linux 为例

1
sudo dnf install global-ctags global cscope

如果不安装 global-ctags 在 Fedora 下可能会报 Cannot enable custom plug-in parser for GNU GLOBAL 的错误。

gtags-cscope 由 global 安装包提供不用另外安装了。

1
2
3
4
5
6
7
8
9
10
11
sudo dnf whatprovides gtags-cscope

global-6.6.5-7.fc37.x86_64 : Source code tag system
仓库 :@System
匹配来源:
文件名 :/usr/bin/gtags-cscope

global-6.6.5-7.fc37.x86_64 : Source code tag system
仓库 :fedora
匹配来源:
文件名 :/usr/bin/gtags-cscope

macOS 下可以使用 brew 安装 global,执行 brew install global 即可。
参见 https://www.gnu.org/software/global/download.html

如果需要gtags支持 C/C++/Java 之外的语言,需要安装 pygments

1
pip3 install pygments

☆ 3. GNU global 和 cscope, vim 集成

vim 的 cscope 支持可能需要重新编译 vim,可以通过下面的命令查询。

1
2
vim --version | grep cscope
+cscope +localmap +ruby/dyn +wildignore

GNU global 提供了两个 vim 插件用于和 vim 结合,分别为 gtags.vim 和 gtags-cscope.vim,可以直接拷贝到 vim 的插件目录使用。

1
2
cp /usr/local/share/gtags/gtags.vim $HOME/.vim/plugin
cp /usr/local/share/gtags/gtags-cscope.vim $HOME/.vim/plugin

如果是系统仓库安装路径可能有变化,Fedora 上的 gtags.vim 路径为 /usr/share/gtags/gtags.vim

gtags-cscope.vim 定义了一组快捷键和以前的 cscope_maps.vim 一致

1
2
3
4
5
6
7
8
:nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
:nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
:nmap <C-\>i :cs find i <C-R>=expand("<cfile>")<CR><CR>
:nmap <C-\>a :cs find a <C-R>=expand("<cword>")<CR><CR>

gtags-cscope.vim 的使用注释里有详细的说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
" Usage
" -----
" First of all, you must execute gtags(1) at the root of source directory
" to make tag files. Assuming that your source directory is '/var/src',
" it is neccessary to execute the following commands.
"
" [Load vim]
" $ cd /var/src
" $ gtags
" $ vim
" [Load gtags-cscope]
" :GtagsCscope <ENTER> (in vim command line)
"
" Basic command
" -------------
" Then you can use cs commands except for the 'd'(2) command.
" Profitable commands are assigned to keys like follows:
"
" explanation command
" ----------------------------------------------------------
" Find symbol :cs find 0 or s
" Find definition :cs find 1 or g
" Find functions called by this function (not implemented)
" Find reference :cs find 3 or c
" Find text string :cs find 4 or t
" Find egrep pattern :cs find 6 or e
" Find path :cs find 7 or f
" Find include file :cs find 8 or i
" Find assignments :cs find 9 or a
"
" You can move tag list using:
" Go to the next tag :tn
" Go to the previous tag :tp
" Pop tag stack :pop

除了不能使用 :cs find d 命令,可以使用其他所有的命令,gtags-cscope.vim 使用快捷键替代了输入命令,常用的快捷键的含义如下:

1
2
3
4
5
<C-\>g - 查看光标下符号的定义
<C-\>s - 查看光标下符号
<C-\>c - 查看光标下符号的引用
<C-\>f - 查找光标下的文件
<C-\>i - 查找哪些文件 include 了本文件

<C-\>g 是同时按下 Ctrl 和 \ ,接着再按 g , 其他的同理。

在 .vimrc 中添加下面的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if has("cscope")
" To use the default key/mouse mapping:
let GtagsCscope_Auto_Map = 1

" To deterring interruption:
let GtagsCscope_Keep_Alive = 1

set cscopetag
set csprg=/usr/bin/gtags-cscope
set csto=1
set nocsverb

" Set enviroment values
let $GTAGSLABEL='native-pygments'
let $GTAGSCONF='/etc/gtags.conf'

endif

☆ 4. GUN global 的使用方法

切换到源码目录,执行下面的命令将生成 Global 所需要的索引文件。

1
2
find . -type f -name "*.h" -o -name "*.hpp" -o -name "*.hh" -o -name "*.hxx" -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.cxx" > gtags.files
gtags -f gtags.files

上面的命令执行完毕后,会生成 GPATH、GRTAGS、GTAGS 几个索引文件。

在 vim 中执行 :GtagsCscope <ENTER> 即可加载索引文件,最后就是使用快捷键愉快地浏览源码文件了。

参考链接


GNU Global 的使用
http://usmacd.com/cn/gtags-cscope/
Author
henices
Posted on
March 2, 2023
Licensed under