Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs as an IDE for large C++ projects

Tags:

c++

emacs

ide

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?

like image 612
lordlabakdas Avatar asked Mar 07 '13 21:03

lordlabakdas


2 Answers

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.

like image 164
Alexandre C. Avatar answered Oct 14 '22 07:10

Alexandre C.


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).

like image 11
François Févotte Avatar answered Oct 14 '22 05:10

François Févotte