Go to the first, previous, next, last section, table of contents.

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 kill-compilation
M-x kill-grep
Kill the running compilation or grep subprocess.
C-x `
Visit the locus of the next compiler error message or grep match.
Visit the locus of the error message that point is on. This command is used in the compilation buffer.
Visit the locus of the error message that you click on.

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 (see section Local Variables in Files).

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.

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.

The `*compilation*' buffer uses a special major mode, Compilation mode. This mode provides the keys SPC and DEL to scroll by screenfuls, and M-n and M-p to move to the next or previous error message. You can also use M-{ and M-} to move up or down to an error message for a different source file.

You can visit the source for any particular error message by moving point in `*compilation*' to that error message and typing RET (compile-goto-error). Or click Mouse-2 on the error message; you need not switch to the `*compilation*' buffer first.

To parse the compiler error messages sequentially, type C-x ` (next-error). The character following the C-x is the backquote or "grave accent," not the single-quote. This command is available in all buffers, not just in `*compilation*'; it displays the next error message at the top of one window and source location of the error in another window.

The first time C-x ` is used after the start of a compilation, it moves to the first error's location. Subsequent uses of C-x ` advance down to subsequent errors. If you visit a specific error message with RET or Mouse-2, subsequent C-x ` commands advance from there. When C-x ` gets to the end of the buffer and finds no more error messages to visit, it fails and signals an Emacs error.

C-u C-x ` starts scanning from the beginning of the compilation buffer. This way, you can process the same set of errors again.

Just as you can run a compiler, you can also run grep and then visit the lines on which matches were found. To do this, type M-x grep with an argument line that contains the same arguments you would give grep when running it normally: a grep-style regexp (usually in single-quotes to quote the shell's special characters) followed by file names which may use wildcards. The output from grep goes in the `*grep*' buffer, and you can find the matching lines in the source with C-x ` and RET just like compiler errors.

Note: a shell is used to run the compile command, but the shell is told that it should be noninteractive. This means in particular that the shell starts up with no prompt. If you find your usual shell prompt making an unsightly appearance in the `*compilation*' buffer, it means you have made a mistake in your shell's init file by setting the prompt unconditionally. (The init file name may be `.profile', `.cshrc', `.shrc', or various other things, depending on the shell you use.) The shell init file should set the prompt only if there already is a prompt. In csh, here is how to do it:

if ($?prompt) set prompt = ...

And here's how to do it in bash:

if [ "${PS1+set}" = set ]
then prompt=...

There may well be other things that your shell's init file ought to do only for an interactive shell. You can use the same method to conditionalize them.

The features of Compilation mode are also available in a minor mode called Compilation Minor mode. This lets you parse error messages in any buffer, not just a normal compilation output buffer. Type M-x compilation-minor-mode to enable the minor mode. This defines the keys RET and Mouse-2, as in the Compilation major mode. In an Rlogin buffer (see section Remote Host Shell), Compilation minor mode automatically accesses remote source files by FTP (see section File Names).

The MS-DOS "operating system" does not support asynchronous subprocesses; to work around this lack, M-x compile runs the compilation command synchronously on MS-DOS. As a consequence, you must wait until the command finishes before you can do anything else in Emacs. See section MS-DOS Issues.

Go to the first, previous, next, last section, table of contents.