Using GDB under Emacs
(or: how to be even more efficient with Emacs)

Today's plan is to walk-through how to compile programs under emacs, and also how to debug using gdb under emacs.

Setting up

If you decide to use emacs for coding, you can optionally put a configuration/preferences file in your home directory (call it ".emacs", and give it r+w permissions to the world). For the purposes of today's lab, please use a copy of my .emacs file.

As well, the program that we will be compiling & debugging can be downloaded from here.

Emacs Basics

Since not all of you have previous experience with emacs, we will briefly review the basics that you'll need to know. If you are already familiar with emacs, you can skip this section.

Well, I think that's enough to get started.


The standard way to invoke the compiler is to type: "M-x compile". You then specify the compile command you want to use (in our case, type in "g++ -g prob.c -o prob". Thereafter, emacs remembers and reuses your compile command.

As a shortcut (and thanks to the .emacs file we copied at the beginning), you can just type C-c C-v.

Once you compile, the emacs window automatically divides into 2 separate windows. The top window is your source code, and the bottom window is your interface to the compiler. If you want to remove the bottom window, click on the top window and then enter "M-x 1"

With 2 separate windows, you can toggle control between the windows by typing "C-x o".

Ok, at this point to the following:

Once the program compiles successfully, we can advance to the next level: debugging.



We can now continue with our coding..