25.1. Running Compilations under Emacs

Emacs can run compilers for noninteractive languages such as C and Fortran as inferior processes, feeding the error log into an Emacs buffer. It can also parse the error messages and show you the source lines where compilation errors occurred.

M-x compile

Run a compiler asynchronously under Emacs, with error messages to *compilation* buffer.

M-x grep

Run grep asynchronously under Emacs, with matching lines listed in the buffer named *grep*.

M-x grep-find

Run grep via find, with user-specified arguments, and collect output in the buffer named *grep*.

M-x kill-compilation, M-x kill-grep

Kill the running compilation or grep subprocess.

To run make or another compilation command, do M-x compile. This command reads a shell command line using the minibuffer, and then executes the command in an inferior shell, putting output in the buffer named *compilation*. The current buffer's default directory is used as the working directory for the execution of the command; normally, therefore, the compilation happens in this directory.

When the shell command line is read, the minibuffer appears containing a default command line, which is the command you used the last time you did M-x compile. If you type just RET, the same command line is used again. For the first M-x compile, the default is make -k. The default compilation command comes from the variable compile-command; if the appropriate compilation command for a file is something other than make -k, it can be useful for the file to specify a local value for compile-command (Section 32.2.5).

Starting a compilation displays the buffer *compilation* in another window but does not select it. The buffer's mode line tells you whether compilation is finished, with the word run or exit inside the parentheses. You do not have to keep this buffer visible; compilation continues in any case. While a compilation is going on, the string Compiling appears in the mode lines of all windows. When this string disappears, the compilation is finished.

If you want to watch the compilation transcript as it appears, switch to the *compilation* buffer and move point to the end of the buffer. When point is at the end, new compilation output is inserted above point, which remains at the end. If point is not at the end of the buffer, it remains fixed while more compilation output is added at the end of the buffer.

If you set the variable compilation-scroll-output to a non-nil value, then the compilation buffer always scrolls to follow output as it comes in.

To kill the compilation process, do M-x kill-compilation. When the compiler process terminates, the mode line of the *compilation* buffer changes to say signal instead of run. Starting a new compilation also kills any running compilation, as only one can exist at any time. However, M-x compile asks for confirmation before actually killing a compilation that is running.