22

I am a Emacs newbie. I have to trying to search on how to use Emacs for use with large C++ projects particularly to index code and auto-complete function names and behave Eclipse-like. I had been using Vim for some time where I used ctags to index code in my project and Vim used to try auto-completing my code using a drop down menu of options. I am trying to achieve the same with Emacs now. But, during my search, results pointed to CEDET and auto-complete and other 3rd party plugins.

I tried to use ctags with ctags -e -R . and etags, but with no success.

Am I missing a default way of Emacs to achieve the same behavior? Which is the best and easiest way to achieve what I want?

lordlabakdas
  • 1,163
  • 5
  • 18
  • 33
  • You may want to check out GNU global plus xgtags.el and clang based auto-complete. Semantic can make use of both as well. – Arne Mar 07 '13 at 22:30
  • Check out clang for c++ autocompletion: http://root42.blogspot.hu/2012/07/nice-c-autocomplete-configuration-for.html – Tom Mar 08 '13 at 07:11
  • I'm currently developing [`clang-tags`](https://github.com/ffevotte/clang-tags), an indexing tool based on clang. It's still in the early stages of the development, but you might find it useable. If you do try it, I would be very glad to get some feedback on github. – François Févotte Mar 08 '13 at 07:29

5 Answers5

17

I use CEDET with autocomplete successfully. Basically, autocomplete is the drop-down box provider, and it takes its sources from various things, most interestingly from CEDET (but also from etags and Gnu Global, which I recommend too).

A good starting point for CEDET is http://alexott.net/en/writings/emacs-devenv/EmacsCedet.html

Alex Ott's emacs config is there: https://github.com/alexott/emacs-configs -- it's an useful resource.

Note that you'll need to grab CEDET from bzr, and install/configure autocomplete correctly. I strongly recommend el-get to install autocomplete (and some other stuff too). You'll need to set up generic projects for EDE to have autocompletion working for random C/C++ files not part of a structured EDE project.

You'll have to spend some time to configure emacs, but it pays off. The tool is amazingly productive once set up correctly.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • +1 for "The tool is amazingly productive once set up correctly" – Pratik Singhal Jul 28 '14 at 16:34
  • I may have to note that I don't use CEDET anymore, due to the fact that the C++ parser is not very accurate, and slow as hell. I use now the libclang based irony-mode for completion, and gtags for navigation. – Alexandre C. Jul 28 '14 at 16:50
12

Indexing

You might want to use GNU/global instead of ctags: it supports C++ and is in my opinion more efficient with large projects (especially since you can update the index instead of rebuilding it from scratch). And it still is a lot simpler to use that CEDET/Semantic (which is also a fantastic tool if you spend the time to set it up).

Example use:

$ cd sources
$ gtags -v         # create the index
$ cd subdirectory
$ [hack hack hack]
$ global -u        # update the index (can be called from anywhere in the project)

In Emacs, activate gtags-mode in the source code buffers to get access to the gtags commands:

  • gtags-find-tag (M-.) : find the definition of the specified tag in your source files (gtags lets you choose between all possible definitions if there are several, or directly jumps if there is only one possibility)
  • gtags-pop-stack (M-*) : return to the previous location
  • gtags-find-rtag : find all uses of the specified tag in the source files

Below is my configuration for gtags, which automatically activates gtags-mode if an index is found:

;; gtags-mode
(eval-after-load "gtags"
  '(progn
     (define-key gtags-mode-map (kbd "M-,") 'gtags-find-rtag)))
(defun ff/turn-on-gtags ()
  "Turn `gtags-mode' on if a global tags file has been generated.

This function asynchronously runs 'global -u' to update global
tags. When the command successfully returns, `gtags-mode' is
turned on."
  (interactive)
  (let ((process (start-process "global -u"
                                "*global output*"
                                "global" "-u"))
        (buffer  (current-buffer)))
    (set-process-sentinel
     process
     `(lambda (process event)
        (when (and (eq (process-status process) 'exit)
                   (eq (process-exit-status process) 0))
          (with-current-buffer ,buffer
            (message "Activating gtags-mode")
            (gtags-mode 1)))))))

(add-hook 'c-mode-common-hook 'ff/turn-on-gtags)

Automatic completion

I don't know of any better tool than auto-complete. Even if it is not included within Emacs, it is very easily installable using the packaging system (for example in the marmalade or melpa repositories).

François Févotte
  • 19,520
  • 4
  • 51
  • 74
6

It depends what you are looking for in an IDE. I have been using Emacs for a fairly large C++ project. Of course you need to configure emacs to work as you want it to work in a greater extent they any other IDE.

But yes CEDET is a start, even though it is not perfect.

However there is a very good auto complete mode for Emacs http://cx4a.org/software/auto-complete/ it is not intelisense but it should integrate with CEDET in some way to give you a farily good auto complete.

Another important feature that I often use is the function ff-find-other-file to easy jump from header and implementation files.

Then of course you need to roll your own bulid. CEDET has some support for projects, but I have not tested it. However Emacs integrate well with command-line build tools such as make. Errors are printed in a buffer and you can jump to the correct line easily within Emacs.

GDB is also integrates well with Emacs M-x gdb, then just remember the gdb-many-windows command.

AxelOmega
  • 974
  • 10
  • 18
2

I recommend to watch Atila Neves lightning talk at CppCon 2015 titled Emacs as a C++ IDE.

For for details, see my answer to this related question.

Community
  • 1
  • 1
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
1

See https://emacs.stackexchange.com/questions/26518/sequence-of-packages-to-be-installed-to-make-emacs-an-ide-for-c-c

I use GNU Global and two popular Emacs plugins:

  • company for code completion

  • emacs-helm-gtags for code navigation

Community
  • 1
  • 1
chen bin
  • 568
  • 4
  • 16