Next: , Previous: , Up: Running   [Contents][Index]


22.1 Running “make”, or Compilers Generally

Emacs can run compilers for non-interactive languages like C and Fortran as inferior processes, feeding the error log into an Emacs buffer. It can also parse the error messages and visit the files in which errors are found, moving point to the line where the error 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 ‘*compilation*’.

M-x kill-compilation

Kill the process made by the M-x compile command.

M-x kill-grep

Kill the running compilation or grep subprocess.

C-x `

Visit the next compiler error message or grep match.

To run make or another compiler, type M-x compile. This command reads a shell command line using the minibuffer, then executes the specified command line in an inferior shell with output going to the buffer named ‘*compilation*’. By default, the current buffer’s default directory is used as the working directory for the execution of the command; therefore, the makefile comes from this directory.

When the shell command line is read, the minibuffer appears containing a default command line (the command you used the last time you typed M-x compile). If you type just RET, the same command line is used again. The first M-x compile provides make -k as the default. The default is taken from the variable compile-command; if the appropriate compilation command for a file is something other than make -k, it can be useful to have the file specify a local value for compile-command (see File Variables).

When you start a compilation, the buffer ‘*compilation*’ is displayed in another window but not selected. Its mode line displays the word ‘run’ or ‘exit’ in the parentheses to tell you whether compilation is finished. You do not have to keep this buffer visible; compilation continues in any case.

To kill the compilation process, type M-x kill-compilation. 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 occur at any time. Starting a new compilation prompts for confirmation before actually killing a compilation that is running.

To parse the compiler error messages, type C-x ` (next-error). The character following C-x is the grave accent, not the single quote. The command displays the buffer ‘*compilation*’ in one window and the buffer in which the next error occurred in another window. Point in that buffer is moved to the line where the error was found. The corresponding error message is scrolled to the top of the window in which ‘*compilation*’ is displayed.

The first time you use C-x ` after the start of a compilation, it parses all the error messages, visits all the files that have error messages, and creates markers pointing at the lines the error messages refer to. It then moves to the first error message location. Subsequent uses of C-x ` advance down the data set up by the first use. When the preparsed error messages are exhausted, the next C-x ` checks for any more error messages that have come in; this is useful if you start editing compiler errors while compilation is still going on. If no additional error messages have come in, C-x ` reports an error.

C-u C-x ` discards the preparsed error message data and parses the ‘*compilation*’ buffer again, then displays the first error. This way, you can process the same set of errors again.

Instead of running a compiler, you can run grep and see 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 to grep: a grep-style regexp (usually in single quotes to quote the shell’s special characters) followed by filenames, which may use wildcard characters. The output from grep goes in the ‘*compilation*’ buffer. You can use C-x ` to find the lines that match as if they were compilation errors.

Note: a shell is used to run the compile command, but the shell is not run in interactive mode. In particular, this means 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 initialization file (.cshrc or .shrc or …) by setting the prompt unconditionally. The shell initialization file should set the prompt only if there already is a prompt. Here’s how to do it in csh:

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

Next: , Previous: , Up: Running   [Contents][Index]