How to install rtags for vim in Ubuntu 18.04

How to install rtags for vim in Ubuntu 18.04 / 如何在 Ubuntu 18.04 vim 上安装 rtags 插件

This blog introduces how to install the best cross-reference tool, rtags, that I have ever used in vim, step by step. I hope it helps.

What's rtags

Rtags is a client/server application that indexes C/C++ code and keeps a persistent file-based database of references, declarations, definitions, symbolnames etc. It allows you to find symbols by name (including nested class and namespace scope). Most importantly, it gives you proper follow-symbol and find-references support.

Rtags comes with emacs support but there are projects supporting other IDEs: vim-rtags and sublime-rtags.

In this blog, We would install vim-rtags later.

How to install rtags

First, you need clang, which is a compiler front end for the C, C++, Objective-C and so on. It uses the LLVM compiler infrastructure as its back end and has been part of the LLVM release cycle since LLVM 2.6.

1
2
3
4
// Commands in Ubuntu terminal
// sudo apt install clang-5.0 lld-5.0
// sudo apt install libclang-5.0-dev
sudo apt install llvm-7-dev libclang-7-dev

Secondly, make and install rtags as follows.

1
2
3
4
5
6
// Commands in Ubuntu terminal
git clone --recursive https://github.com/Andersbakken/rtags.git
cd rtags <-- in rtags directory
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .
make
sudo make install

Thirdly, you should install vim-rtags in Vundle.vim. I think it's the easiest way to install rtags plugin in vim. You need to add the following line to .vimrc if you have installed Vundle, and then run :PluginInstall in vim.

1
Plugin 'lyuts/vim-rtags' <-- install in vim

Finally, the last but essential step is that forcing cmake to output compile_commands.json (like DCMAKE_EXPORT_COMPILE_COMMANDS) and link it with rtags according to your project.

1
2
3
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 [***] your_src_path
rc -J your_build_folder_path <-- use compile_commands.json in your build folder
rdm & <-- launch the rtags server