C/C++ auto complete in emacs using clang in Debian/Ubuntu

I got so accustomed to using Visual Studio 2010 over the course of my internship, that I seriously was missing it while coding in Linux. I knew there were tools available like libclang+vim etc., but they never seemed complete. So finally I sat down and got auto-complete working with emacs in Ubuntu Linux(Note: This should also work in other distributions, but I’ve only tested it on Debian testing and Ubuntu 12.04).

I also got the browsing mode setup which enables me to jump to definitions, see the references of functions etc., A complete setup for browsing source is here.

Here are the steps:

1. Install emacs, emacs-goodies, global, clang, auto-complete

sudo apt-get install emacs emacs-goodies-el global clang auto-complete-el cscope-el

2. Download this emacs conf file and copy it to ~/.emacs

3. Copy the contents of this file into ~/.emacs.d/

4. generate global tags in the rootsource directory by issuing the command

gtags -v

5. Done!


Auto complete in emacs using clang
* To search for tags, use M-.
* To search for symbols, use M-]
* To search for function references, use M-[
* To go back to where you were, use M-*

If there are any errors/suggestions, please let me know in the comments. Thanks!

update 11/08/2012: added cscope-el

update 1/30/2014: source code browsing in emacs is much simpler now thanks to global. Check it out: https://github.com/leoliu/ggtags


5 thoughts on “C/C++ auto complete in emacs using clang in Debian/Ubuntu

  1. Joseph says:

    Nice Article, Do you know how it works ? Does it do a compile
    before it auto-completes ?

  2. pranith says:

    It does not compile the code. It build the AST and walks the tree for indexing and cross-referencing. You can get more details here: http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf

  3. Joseph says:

    How do we extend this for say boost libraries ?

    • pranith says:

      I did not try this but downloading the boost library and running gtags in the root folder should be enough. Let me know if that works.

  4. tolga says:

    This is really nice but does it also show help for functions arguments? eg printf(… it shows mw print variations but what do I need to do to get help/code assist for arguments?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s