Compiling in emacs
Steven Zeil
When your programs contain mistakes, compiling them in the shell can result in large numbers of error messages scrolling by faster than you can read them. For this reason, I find it best to compile from within the emacs
editor.
1 Compiling with emacs
Example 1: Try This:
Get into
emacs
and call up one of the “hello” programs from the prior section. Change it so that it contains one or more syntax errors, and save this file.Now give the
emacs
command:M-x compile
. At the bottom of the screen, you will be asked for the compile command. If the suggested command is not what you want (it won’t be, until you learn to usemake
to manage your program compilations), then type in the proper command just as if you were typing it into the shell.
emacs
will invoke the compiler, showing its output in a window. Figure 1 shows a typical emacs session after such a compilation.In this case, there should be one or more error messages. The
emacs
next-error command (C-x
` ) will move you to the source code location of the first error. Each subsequent use ofC-x
` will move you to the next error location in turn, until all the reported error messages have been dealt with.Use this command to step through the errors. (Note carefully that the second character in the
C-x
` command is the “backwards” apostrophe, not the regular one.)
2 Compiling with vim
vim
(“vi improved”) is an reasonable alternative to emacs
on many Unix systems. It provides syntax-highlighting editor, can launch the compiler and will display the error messages, synchronized with the source code (here). It provides no interface to the debugger, however.
To learn the basics of running vim, give the command vimtutor
.
After you worked through the basics, cd into a directory where you have a C++ program and a makefile, and load one of the source files (e.g., vim foo.cpp
). The command “:make
” (including the leading colon “:”) will run the make
program to attempt compilation. If you have errors, the command :cc
will display the first one, :cn
moves you to the next error, and :cp
moves you to the previous one.