Emacs Advent Calendar 17: Compilation

Thu 2015-12-17

Emacs includes commands for compiling software and, if unsuccessful, quickly finding the locations in the source where it failed.

To start a compilation, execute M-x compile. It will ask for a compilation shell command to run with make -k as default. To re-execute a compilation command, run M-x recompile. A compilation can be killed with M-x kill-compilation. All output from compilation will go into the *compilation* buffer, which itself is in compilation mode and has some special keybindings.

As there is only one *compilation* buffer, you can only run one compilation process at a time. So to have more than one compilation running in parallel you have to give the buffers unique names. Emacs can do this for you by M-x rename-uniquely.

In a *compilation* buffer, M-n and M-p move through the error messages. Similarly M-g M-n and M-g M-p move through error locations in the source buffers (for convenience, those functions are also bound to M-g n and M-g p; next-error has C-x ` as a third keybinding).

Toggling Next Error follow minor mode with C-c C-f makes Emacs jump to the location of an error in the source buffer when point is on an error message in the *compilation* buffer.

Pressing C-c C-k in a compilation buffer kills a running compilation.

Full documentation on compilation in Emacs can be found in sections 27.1 (Compilation) and 27.1 (Compilation Mode) of the Emacs Reference Manual.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.