Blog Archives

Rtags completes Emacs


This post is a description of how to setup Rtags on Emacs.  The internet is has quite a few resources about Rtags’ setup, but despite that I had to go over several of them to get things up and running for me.

Rtags is ideally meant to work for C++ projects that use CMake build systems, for others there are a few tweaks that may be needed. So here is the setup that works for developers using the traditional GNU Make for building their projects.

So here goes my setup:

To start with I had Emacs already installed, and I use Ubuntu 18.04

Installing Clang

Fire up a terminal and execute the following commands

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb https://apt.llvm.org/xenial/ llvm-toolchain-xenial-3.9 main"
sudo apt-get update
sudo apt-get install clang-3.9 lldb-3.9
sudo apt-get install libclang-3.9-dev

Installing Rtags Server

Once you have clang installed now its time to install Rtags. This can be done with the following commands

git clone --recursive https://github.com/Andersbakken/rtags.git
cd rtags
mkdir build
cd build
cmake ..
make

If the make executes with out any errors then it’s time to move forward. Once this install is complete you can start the rtags server with the rdm command

rdm -j3 &

If this does not work ensure that $PATH has the path to your rtags/build/bin folder which was created in the above steps, added to it. The rdm process is located in that folder.

This will have the rdm process running and listening to build commands and create the cache for the code navigation.

Install The Rtags plugin for Emacs

Emacs being an awesome editor all you have to do is

M-x package-install RET rtags RET
M-x package-install RET flycheck-rtags RET
M-x package-install RET company-rtags RET

Now in your .emacs file add the following(sourced from here)

(defun setup-flycheck-rtags ()
  (interactive)
  (flycheck-select-checker 'rtags)
  (setq-local flycheck-highlighting-mode nil)
  (setq-local flycheck-check-syntax-automatically nil))

;; only run this if rtags is installed
(when (require 'rtags nil :noerror)
  ;; make sure you have company-mode installed
  (require 'company)
  (define-key c-mode-base-map (kbd "M-.")
    (function rtags-find-symbol-at-point))
  (define-key c-mode-base-map (kbd "M-,")
    (function rtags-find-references-at-point))
  ;; install standard rtags keybindings. Do M-. on the symbol below to
  ;; jump to definition and see the keybindings.
  (rtags-enable-standard-keybindings)
  (setq rtags-autostart-diagnostics t)
  (rtags-diagnostics)
  (setq rtags-completions-enabled t)
  (push 'company-rtags company-backends)
  (global-company-mode)
  (define-key c-mode-base-map (kbd "<C-tab>") (function company-complete))
  ;; use rtags flycheck mode -- clang warnings shown inline
  (require 'flycheck-rtags)
  ;; c-mode-common-hook is also called by c++-mode
  (add-hook 'c-mode-common-hook #'setup-flycheck-rtags))

If you use the CMake build systems the story kind of ends here, but as I said if you use the GNU Make then hold on .

A Secret Recipe for GNU Make users

Go back to the rtags folder where we synced the code from github. Remember that in step 1 we created a build folder to build rtags and that the binary rdm was present in rtags_path/build/bin

What we are going to do is use the rtags wrappers for g++/gcc to ensure that when these commands are triggered rdm process will listen and create the cache needed for it to work.

So in a terminal we will do the following

ln -s /path-to-rtags/bin/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/gcc-rtags-wrapper.sh
ln -s /path-to-rtags/bin/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/g++
ln -s /path-to-rtags/bin/gcc-rtags-wrapper.sh /path-to-rtags/build/bin/gcc

The paths to these newly created symoblic links should already be a part of $PATH from step 1.

Once this is done go to your project folder and build the project. If you have the terminal with rdm running you can see it is logging some information. Once this stops. You are all set. Start Emacs and open a source file. The key bindings for each action can be found on the C++ menu as shown below.

emacs_rtags

Here is a short demo of how to navigate the code using rtags

rtags_demo

Hope you found this useful.

Advertisements
%d bloggers like this: