Compiling in Editors
Steven Zeil
In an earlier lesson we saw that the editors emacs
and vim
can be used as programming tools to compile code and capture the inevitable compilation error messages.
In the exercises for that lesson, we had to work around the fact that
-
Both
emacs
andvim
assume that you will use a make file to provide the compilation commands for your code. -
We had not yet covered make files, and so had to work around that assumption to directly enter the compilation commands that we wanted.
It’s worth taking a moment, therefore, to practice with those editors again now that we know how to use make files.
Do one or both of the following sections:
1 Compiling with emacs
Example 1: Try This:
We’re going to start by getting a fresh copy of a slightly modified version of our “Sieve” program.
rm ~/playing/sieve/* cd ~/playing/sieve cp ~cs252/Assignments/sieve/* . ls
You should have two
.cpp
files, one.h
file, and onemakefile
.emacs -nw makefile
Now give the
emacs
command:M-x compile
. At the bottom of the screen, you will be asked for the compile command.emacs
will suggest the commandmake -k
. Since we have a makefile for this project, that suggestion is just fine, so hit Enter and letemacs
invokemake
for you.You should see various compilation error messages accumulate in a separate pane.
Use the
emacs
next-error command,C-x
` , to move through the errors. Notice thatemacs
loads the appropriate source code files for you at each step.To go back to the beginning, switch your cursor into the buffer with the error message (
C-x o
), move the cursor back t othe top of the listing, and return to your source code pane (C-x o
again).Make a second pass through the errors, correcting them. Recompile the code to make sure you have fixed them.
Exit
emacs
and list your directory. You should find the executable program,findPrimes
, left behind by your successful compilation.
2 Compiling with vim
Example 2: Try This:
We’re going to start by getting a fresh copy of a slightly modified version of our “Sieve” program.
rm ~/playing/sieve/* cd ~/playing/sieve cp ~cs252/Assignments/sieve/* . ls
You should have two
.cpp
files, one.h
file, and onemakefile
.vim makefile
Now give the
vim
command::make
This runs
make
, using the make file in the directory, and capturing the error messages.You’ll see a listing of the errors scroll by, leaving you with the last few and a prompt to “Press Enter… to continue”.
Press Enter.
Now you will be back at your editor view showing the code. But the cursor will be placed at the location of the first error, which you can see summarized on the bottom line of the editor.
Use the
vim
next-error command,:cn
, to move through the the error messages. Notice thatvim
loads the appropriate source code file as you go.Use
:cp
to move back to the start of the list.Make a second pass through the errors, correcting them. Recompile the code to make sure you have fixed them.
Exit
emacs
and list your directory. You should find the executable program,findPrimes
, left behind by your successful compilation.