Next: Compilation Mode, Up: Building [Contents][Index]

### 27.1 Running Compilations under Emacs

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

`M-x compile`

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

`M-x recompile`

  Invoke a compiler with the same command as in the last invocation of `M-x compile`.

`M-x kill-compilation`

  Kill the running compilation subprocess.

To run `make` or another compilation command, type `M-x compile`. This reads a shell command line using the minibuffer, and then executes the command by running a shell as a subprocess (or inferior process) of Emacs. The output is inserted in a buffer named `*compilation*`. The current buffer’s default directory is used as the working directory for the execution of the command; normally, therefore, compilation takes place in this directory.

The default compilation command is ‘`make -k`’, which is usually correct for programs compiled using the `make` utility (the ‘`-k`’ flag tells `make` to continue compiling as much as possible after an error). See Make in GNU Make Manual. If you have done `M-x compile` before, the command that you specified is automatically stored in the variable `compile-command`; this is used as the default the next time you type `M-x compile`. A file can also specify a file-local value for `compile-command` (see File Variables).

Starting a compilation displays the `*compilation*` buffer in another window but does not select it. While the compilation is running, the word ‘`run`’ is shown in the major mode indicator for the `*compilation*` buffer, and the word ‘`Compiling`’ appears in all mode lines. You do not have to keep the `*compilation*` buffer visible while compilation is running; it continues in any case. When the compilation ends, for whatever reason, the mode line of the `*compilation*` buffer changes to say ‘`exit`’ (followed by the exit code: ‘`[0]`’ for a normal exit), or ‘`signal`’ (if a signal terminated the process).

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. Otherwise, point remains fixed while compilation output is added at the end of the buffer.

If you change the variable `compilation-scroll-output` to a non-`nil` value, the `*compilation*` buffer scrolls automatically to follow the output. If the value is `first-error`, scrolling stops when the first error appears, leaving point at that error. For any other non-`nil` value, scrolling continues until there is no more output.

To rerun the last compilation with the same command, type `M-x recompile`. This reuses the compilation command from the last invocation of `M-x compile`. It also reuses the `*compilation*` buffer and starts the compilation in its default directory, which is the directory in which the previous compilation was started.

Starting a new compilation also kills any compilation already running in `*compilation*`, as the buffer can only handle one compilation at any time. However, `M-x compile` asks for confirmation before actually killing a compilation that is running; to always automatically kill the compilation without asking, change the variable `compilation-always-kill` to `t`. You can also kill a compilation process with the command `M-x kill-compilation`.

To run two compilations at once, start the first one, then rename the `*compilation*` buffer (perhaps using `rename-uniquely`; see Misc Buffer), then switch buffers and start the other compilation. This will create a new `*compilation*` buffer.

You can control the environment passed to the compilation command with the variable `compilation-environment`. Its value is a list of environment variable settings; each element should be a string of the form `"envvarname=value"`. These environment variable settings override the usual ones.

Next: Compilation Mode, Up: Building [Contents][Index]