The Notorious Mick Beaver’s Guide to Using Emacs With C++

 

GNU Emacs is not only a great text editor, but also a great IDE for developing with C++.  As of January 2011, the documentation for putting all of the pieces together was, arguably, scattered.

 

  1. Install GNU Emacs
  1. Mac OS X - http://emacsformacosx.com/
  2. Microsoft Windows
  1. Download ftp://ftp.gnu.org/gnu/emacs/windows/emacs-23.2-bin-i386.zip
  2. Unzip
  3. Put in “C:\Program Files (x86)”
  4. Double-click “C:\Program Files (x86)\emacs-23.2\bin\addpm.exe” to add GNU Emacs to the Start Menu
  1. Linux - You KNOW what to do.
  1. Configure Emacs
  1. Emacs uses a configuration file named ‘.emacs’
  1. Mac OS X - put it in your home directory ‘~’
  2. Microsoft Windows - put it in %APPDATA%, or the folder specified by the %HOME% environment variable.
  1. Prosper.

 

Tools

  1. Semantic
  1. This is the C++ code analyzer and completer
  1. GDB-UI
  1. The new visual debugger built into Emacs that uses GDB as the backend debugger
  1. C-style
  1. Emacs will help you indent your code as you go. There are a number of different built-in styles, but you can define your own if you want
  1. Compile
  1. Build your code from Emacs. This will give you the opportunity to jump directly to warnings and errors in the compilation process.
  1. Speedbar
  1. The Emacs file browser. When coupled with Semantic, it can be a code browser.

 

Keybindings/Commands To Know

  1. C-h t - tutorial
  2. C-h w - Type in a function/command and it will tell you which keys it is bound to
  3. C-h k - Type in a key/key combination and tell you which command is bound to it
  4. C-c , J - Jump to the definition of the symbol under point (“point” means the logical position right before the cursor)
  5. M-x speedbar - Start the Speedbar file/code browser
  6. M-x gdb - Start the GDB debugger
  7. M-x shell - Start a command line shell within Emacs
  8. M-x ielm - Interactive Emacs Lisp Mode to test out your Elisp code
  9. M-x describe-function - Get help on a function/command
  10. M-x describe-variable - Get help on a settings variable

 

Configuration

;; Here is a sample .emacs configuration for working with C++.

;; Note that everything after a semicolon is a comment.

;; Part I. Essentials to editing

;; Specify a directory where to load additional Emacs Lisp files.

(add-to-list 'load-path (expand-file-name "~/.emacs.d"))

;; Use spaces instead of tabs

(setq-default indent-tabs-mode nil)

;; Turns on syntax highlighting.

(global-font-lock-mode t)

;; Sets the syntax highlighting to the maximum amount of colorization.

(setq font-lock-maximum-decoration t)

;; Do not make backup recovery files when editing.

(setq make-backup-files nil)

;; Use Shift + the arrow keys to move between windows in a frame

(when (fboundp 'windmove-default-keybindings)

            (windmove-default-keybindings))

;; Part II. Configure Emacs as a great C++ IDE

;; Include the C/C++ mode Elisp package

(require 'cc-mode)

;; In versions of Emacs greater than 23.2, do the following

(when (or (> emacs-major-version 23)

                (and (= emacs-major-version 23)

                     (>= emacs-minor-version 2)))

 ;; Use the GDB visual debugging mode

 (setq gdb-many-windows t)

 ;; Turn Semantic on

 (semantic-mode 1)

 ;; Try to make completions when not typing

 (global-semantic-idle-completions-mode 1)

 ;; Use the Semantic speedbar additions

 (add-hook 'speedbar-load-hook (lambda () (require 'semantic/sb))))

;; Treat .h files as C++ files (instead of C)

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

;; Use the indentation style of "The C++ Programming Language"

(setq c-default-style "stroustrup")

;; Return adds a newline and indents

(define-key c-mode-base-map (kbd "RET") 'newline-and-indent)

;; Run compile when you press F5

(global-set-key (kbd "<f5>") 'compile)

 

Where To Go For Help

  1. Emacs Wiki - http://www.emacswiki.org
  2. Emacs Manual - http://www.gnu.org/software/emacs/manual/emacs.html
  3. An Introduction to Programming in Emacs Lisp - http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/index.html
  4. Emacs Lisp Reference - http://www.gnu.org/software/emacs/manual/elisp.html
  5. Semantic Manual - http://www.gnu.org/software/emacs/manual/html_node/semantic/
  6. Emacs Quick Reference Card
  1. C:\Program Files (x86)\emacs-23.2\etc\refcards\refcard.pdf
  2. http://pages.cs.wisc.edu/~mick/downloads/emacs/23.2/etc/refcards/refcard.pdf
  1. Practical Common Lisp (helps with understanding Emacs Lisp) - http://www.gigamonkeys.com/book/