Warning
I no longer use Irony, this page is left available for reference only. For an alternative, I recommend lsp-mode.
Irony mode
Irony mode is an amazing
Emacs package that provides excellent C/C++ code completion. To use
Irony mode in Emacs, you will need to install irony company-irony
company-irony-c-headers
. You can use other completion backends with
Irony, but I have seen the best results from
Company You can configure Irony and
Company for C/C++ development with this snippet of ELisp:
;; irony-mode
(require 'irony)
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
;; company mode
(add-hook 'c++-mode-hook 'company-mode)
(add-hook 'c-mode-hook 'company-mode)
;; Use irony-mode's completion functions
(defun my-irony-mode-hook ()
"Remap completion functions to Irony's versions."
(define-key irony-mode-map [remap completion-at-point]
'irony-completion-at-point-async)
(define-key irony-mode-map [remap complete-symbol]
'irony-completion-at-point-async))
(add-hook 'irony-mode-hook 'my-irony-mode-hook)
(add-hook 'irony-mode-hook
'irony-cdb-autosetup-compile-options)
(eval-after-load 'company
'(add-to-list 'company-backends 'company-irony))
(add-hook 'irony-mode-hook
'company-irony-setup-begin-commands)
(eval-after-load 'flycheck
'(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))
Note: Before this will work, you will need to run M-x irony-install-server RET
in Emacs to install and build the Irony server. This will require CMake and libclang.
Irony mode and Qt
One of the great things about Irony mode is that it can do symbol completion from libraries that you compile against, like the Qt framework for C++. To do this is a little harder, though, and will require some terminal skills.
To get started, you will need to generate what Irony calls a compilation database
(basic documentation here, more in-depth coverage here). Here is an example of doing this with CMake (assuming a correct CMakeLists.txt
):
cmake -D EXPORT_COMPILE_COMMANDS=ON
You're almost done. The compile_commands.json
generated by CMake needs to either be:
- Moved into the same place as the source code you are editing
- Selected in Emacs with
M-x irony-cdb-json-select