WSU logo

College of Engineering & CS
Wright State University
Dayton, Ohio 45435-0001

CEG 333: Introduction to Unix

Prabhaker Mateti

Emacs: Compiling

Key Meaning
M-x compile Compile a program.
M-x first-error Jump to the line containing the first error in the current program.
C-x ` Go to the next error.
M-x gdb Run the debugger on the current program.
C-x SPC Set a breakpoint at the current line.

As mentioned previously, Emacs can integrate with compilers and debuggers to create an IDE.

To start compiling, use M-x compile. Emacs asks for a command line, which is the same g++ or make command used in the shell.

The advantage to compiling this way is that Emacs will parse the compiler output. It's displayed in a special buffer named "*compilation*" (This is an example of a buffer without a file). When the cursor is in the buffer, hitting enter jumps to the line of source code the triggered the error described on the current line (think of each line in the *compilation* buffer as a button or hyperlink). The M-x commands first-error, next-error, previous-error, and goto-line can speed this up.

Similarly, M-x gdb asks for a GDB command line, then opens a debugger session in its own window. Emacs integrates with GDB by providing commands to set, toggle, and delete breakpoints, step through code, and print expressions.

Try C-x SPC while GDB is running and the cursor is on a line of source code to set a breakpoint at that line. C-x C-a C-h shows help on what other debugger commands are available.