Next: Compilation Mode, Up: Building
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.
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.
The default for the compilation command is normally ‘make -k’,
which is correct most of the time for nontrivial programs.
(See Make.) If you have done M-x
compile before, the default each time is the command you used the
previous time. compile
stores this command in the variable
compile-command
, so setting that variable specifies the default
for the next use of M-x compile. If a file specifies a file
local value for compile-command
, that provides the default when
you type M-x compile in that file's buffer. See File Variables.
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’, ‘signal’ 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.
When the compiler process terminates, 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), 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. You can also kill the compilation process with M-x kill-compilation.
To rerun the last compilation with the same command, type M-x recompile. This automatically 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.
Emacs does not expect a compiler process to launch asynchronous subprocesses; if it does, and they keep running after the main compiler process has terminated, Emacs may kill them or their output may not arrive in Emacs. To avoid this problem, make the main process wait for its subprocesses to finish. In a shell script, you can do this using ‘$!’ and ‘wait’, like this:
(sleep 10; echo 2nd)& pid=$! # Record pid of subprocess echo first message wait $pid # Wait for subprocess
If the background process does not output to the compilation buffer, so you only need to prevent it from being killed when the main compilation process terminates, this is sufficient:
nohup command; sleep 1
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.