Compiling in Editors

Steven Zeil

Last modified: Aug 24, 2023
Contents:

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

  1. Both emacs and vim assume that you will use a make file to provide the compilation commands for your code.

  2. 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:
  1. 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 one makefile.

    emacs -nw makefile
    
  2. 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 command make -k. Since we have a makefile for this project, that suggestion is just fine, so hit Enter and let emacs invoke make for you.

    You should see various compilation error messages accumulate in a separate pane.

  3. Use the emacs next-error command, C-x` , to move through the errors. Notice that emacs loads the appropriate source code files for you at each step.

  4. 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).

  5. Make a second pass through the errors, correcting them. Recompile the code to make sure you have fixed them.

  6. 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:
  1. 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 one makefile.

    vim makefile
    
  2. Now give the vim command: :make

    This runs make, using the make file in the directory, and capturing the error messages.

  3. 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.

  4. 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.

  5. Use the vim next-error command, :cn, to move through the the error messages. Notice that vim loads the appropriate source code file as you go.

    Use :cp to move back to the start of the list.

  6. Make a second pass through the errors, correcting them. Recompile the code to make sure you have fixed them.

  7. Exit emacs and list your directory. You should find the executable program, findPrimes, left behind by your successful compilation.