50

Emacs cannot compete with another other IDE if it doesn’t have an auto completion function. Auto-complete simply makes me a more efficient programmer. I know of the Auto Complete Mode extension, but it does not work with C++.

autocomplete

How can I achieve smart and efficient autocompletion for C++?


This question was originally asked by Malabarba on the Area 51 proposal: How to get intelligent auto-completion in C++?

programking
  • 7,064
  • 9
  • 41
  • 62
  • 1
    It would be nice to expand a bit on this question. It has a lot of potential value, but right now the question is just the title. – Malabarba Oct 07 '14 at 22:10
  • [Irony-mode](https://github.com/Sarcasm/irony-mode) seems to be the way to go at this time. – Sevki Oct 07 '14 at 22:58

6 Answers6

50

Disclaimer: I'm the author of irony-mode

company-irony screenshot

If you just want smart auto-completion, I can recommend irony-mode with company-irony.

  • irony-mode is easy to install thanks to MELPA
  • the completion is very accurate and the performance quite decent thanks to libclang
  • with company-irony you have an asynchronous completion backend that will not get in your way if the completion takes some time
  • company-irony supports overloaded functions and if you have Yasnippet installed the function arguments are inserted as placeholders to fill-in
  • if your project uses Doxygen, the brief documentation of the completion candidate is displayed in the minibuffer (see screenshot)
  • the installation of irony-mode server on a Unix-like system (Linux, OS X) should be easy. Windows on the other hand requires a bit of tweaking but it is documented in the wiki

Another tool that hasn't been cited yet is emacs-ycmd.

Guillaume Papin
  • 886
  • 6
  • 7
  • 2
    Thank you so much! Wish I found out sooner about `irony-mode` – programking Oct 10 '14 at 09:46
  • @GuillaumePapin Thanks for writing irony! Would you happen to have any timing benchmarks available? I have noticed that calls to `clang_codeCompleteAt` with a parsed TU(but unsaved changes) takes 3-5 seconds. Is `irony-mode` faster than this? – Pradhan Nov 19 '14 at 18:38
  • 1
    Irony uses libclang/clang_codeCompleteAt, so you won't get better times than that. 3-5 seconds may be too much depending on the context (e.g. completions at global scopes take much more time than other scopes). You can take a look at some timings that I did recently: https://github.com/Sarcasm/irony-mode/issues/131#issuecomment-60008803 – Guillaume Papin Nov 20 '14 at 08:24
  • Related, for future readers: https://github.com/Sarcasm/irony-mode/issues/140 – Sean Allred Dec 08 '14 at 20:50
  • How do the roles of irony-mode and emacs-ycmd compare? Are they alternatives to one another? Would it ever make sense to use both? – Praxeolitic Mar 01 '15 at 18:53
  • I guess feature-wise they are similar right now. Except that emacs-ycmd seems to provide an interface to multiple languages. emacs-ycmd seems to provide mostly completion (I think the diagnostics are completion diagnostics only) while irony-mode aims at providing better C++ support for C++, more than just completion. Right now it makes little sense to use both but maybe in the future that will change. – Guillaume Papin Mar 01 '15 at 21:18
  • @GuillaumePapin I would like to try [irony-mode](https://github.com/Sarcasm/irony-mode) with [auto-complete](http://auto-complete.org/) instead of [company-mode](http://company-mode.github.io/). According to the docs, irony-mode can be used with auto-complete. But looking at [ac-irony](https://github.com/Sarcasm/ac-irony) I found the statement _This package is not yet ready for prime-time, this documentation is therefore a work of fiction._ Is this information updated? Does it mean I will not be able use ac-irony? Can you comment on that? – Romildo Apr 24 '15 at 11:38
  • The reasons to use ac-irony instead of company-irony aren't clear to me yet. auto-complete doesn't provide true asynchronous completion, nether it supports nice annotations. If one want to use ac-irony it should be possible with some hacks, which worked at the time I tried ac-irony. You may want to try it to see if it works. – Guillaume Papin Apr 28 '15 at 12:14
  • from my experience, irony installs far more easier (than ycmd) and works "just out of the box". thanks for the great tool. – Hot.PxL May 21 '15 at 01:46
  • silly question maybe, I'm using company-mode, irony-mode and company-irony and everything works as expected upon opening a .c file, but when I e.g. create a new function the autocomplete won't suggest my new function as a completion option. What can I do about that? – Mathieu Borderé Dec 22 '16 at 19:20
  • Looks like a bug, you should report the bug on Github (https://github.com/Sarcasm/irony-mode/issues) with a minimally reproducible example, and share the output of `~/.emacs.d/irony/bin/irony-server --version` and your OS. – Guillaume Papin Dec 22 '16 at 21:45
24

Check my guide. It lists several methods for auto-completion in C/C++.

Those options are:

  • Using Irony. Highly recommended, since it uses Clang and easy to setup compared with other solutions.

  • Using the built-in parser from Emacs. It is the best auto-complete you can get is from within Emacs parser, Semantic, but it would be slow on large project like Linux kernel.

  • Using company-mode package with company-clang. Provides code completion fine for system include paths since it can automatically get the paths from Clang, but has to do some configuration for project local

  • Using company-mode with company-gtags. It uses tag database generated from GNU Global as completion. You can use this to provide code complete locally to your project. It's not exactly intelligent since it throws everything inside the generated database to you.

  • rtags goes farther by providing a server for live analysis. But it's complicated to setup compare with pure Elisp solution.

Tu Do
  • 6,772
  • 20
  • 39
  • I would not agree that setting up rtags is more complicated than a pure emacs lisp solution. Pure emacs lisp solution usually do not consider cmake/build files and are hardly able to find *any* header. Rtags is pretty easy to setup in comparison. It's just still a bit young as a project, with not very good test coverage. – baol Jul 02 '16 at 20:06
  • Nice addition to Tuhdo's guide: http://syamajala.github.io/c-ide.html – Victor Jul 26 '17 at 08:05
9

I've had great success with RTags + Company.

RTags needs a separate daemon rdm running that uses clang for code-completion, code-navigation and refactoring. rdm also functions as a database of symbols, meaning that autocompletion is much faster than any solution just directly calling clang_codeCompleteAt.

Rdm needs info of compilation flags for your projects which can be a hassle, but if you follow the advice from the RTags page and symlink gcc-rtags-wrapper.sh to gcc, g++, cc and c++ rdm automatically gets updated with new compilation flags on recompile.

Mattias Bengtsson
  • 1,271
  • 1
  • 11
  • 18
  • I just switched over from irony-mode to rtags. Irony-mode constantly craps out and stops working after about 15 mins or so. Rtags seems pretty good so far, and has cleaner integration with yasnippet. – nenchev Jan 28 '16 at 17:59
7

Solution 1: company-mode + clang

The only issue is you need tell company-mode where to search your project's c/c++ header files.

If you use cmake, you can let https://github.com/redguardtoo/cpputils-cmake to do the setup for you (HINT: I'm the author of cpputils-cmake).

OR

Solution 2: company-mode + gnu global

Nothing more to say, you need build the tags with gnu global cli tool, as others mentioned.

chen bin
  • 4,781
  • 18
  • 36
1

M-x semantic-mode

www.gnu.org/software/emacs/manual/html_node/semantic/Using-Semantic.html#Using-Semantic

&&

Tags Tables

find . -name "*.[chCH]" -print | etags -

www.gnu.org/software/emacs/manual/html_node/emacs/Tags.html

etags, ctags - generate tag file for Emacs, vi

manpages.debian.org/cgi-bin/man.cgi?query=etags&apropos=0&sektion=0&manpath=Debian+8+jessie&format=html&locale=en

C-M-i (or M-TAB) completion-at-point. If Semantic mode enabled, tries to use Semantic parser data. If Semantic mode disabled or fails, tries to complete using selected tags table (see Tags)

http://www.gnu.org/software/emacs/manual/html_node/emacs/Symbol-Completion.html#index-C_002dM_002di

M-. (find-tag) prompts tag name && jump to definition

http://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Tag.html#index-M_002d_002e

alexcham
  • 11
  • 2
1

I've been using cmake-ide to configure rtags, flycheck, irony, and company-clang.

Now you can configure your build system and your favorite static analysis emacs plugins at the same time!

EDIT: Note that company clang provides autocompletions, which was the feature you were looking for. cmake-ide can also configure flycheck and rtags, which adds several more IDE-like features.

AnimatedRNG
  • 111
  • 3