43

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++?

  • 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 7 '14 at 22:10
  • Irony-mode seems to be the way to go at this time. – Sevki Oct 7 '14 at 22:58
44

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.

  • 2
    Thank you so much! Wish I found out sooner about irony-mode – Luke Oct 10 '14 at 9: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: github.com/Sarcasm/irony-mode/issues/131#issuecomment-60008803 – Guillaume Papin Nov 20 '14 at 8:24
  • Related, for future readers: github.com/Sarcasm/irony-mode/issues/140 – Sean Allred Dec 8 '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 1 '15 at 18:53
22

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.

  • 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 2 '16 at 20:06
  • Nice addition to Tuhdo's guide: syamajala.github.io/c-ide.html – Victor Jul 26 '17 at 8: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.

  • 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
6

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.

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.

  • Please explain how this answers the question of how to get C++ autocompletion. – Drew Feb 15 '17 at 22:40
  • @Drew company-clang provides completions – AnimatedRNG Feb 16 '17 at 1:13
  • Please put it in your answer. Thx. – Drew Feb 16 '17 at 4:00
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.