USING EMACS TO EDIT/COMPILE/EXECUTE C++ PROGRAMS

 

COMPILING C++ PROGRAMS IN EMACS   Emacs can run compilers (CC, g++, etc.), directing the error log into an Emacs text buffer.  It can then parse the error messages and visit the file where the error was found, moving the cursor right to the line where the error occurred.  You do not have to constantly quit Emacs to compile at the shell level, and then re-start Emacs. LEARN THE TECHNIQUES PRESENTED HERE-- YOU WILL SAVE COUNTLESS HOURS OF YOUR TIME.

 

To Run a Compiler  in Emacs:   Esc-x compile. Emacs assumes your keyboard has a Meta key (for "Bucky bits"), but Esc  is equivalent.  Esc-x compile reads a shell command line using the minibuffer (at the bottom of the screen), and then executes the command in a sub-shell with output going to the buffer named *compilation*.  

 

The minibuffer will prompt you with a default compiler command (make -k the first time).  Use the standard emacs commands to move the cursor and edit the command to suit (e.g., just backspace over it, and enter the compiler command you want). If you're editing a C++ source file, bullwinkle.cpp, then enter the compiler command g++ -o bullwinkle  bullwinkle.cpp   At the next Esc-x compile, you'll be prompted with the compiler command you used most recently.

 

When the compilation begins, the screen splits into two windows-- the source file will be displayed in one and the *compilation* buffer in the other.  Observe the 'mode line' at the bottom of the *compilation* buffer-- it tells you whether the compilation is finished, with the word 'run' or 'exit' inside parentheses. (If you are compiling a very large system, and wish to terminate, use the Esc-x kill-compilation command.)

 

"Parsing" Error Messages.   To read the error messages in order ("parse"), enter c-x `  (control-x backquote).  The error messages are displayed in one window and the file in which the error occurred is displayed in the other (emacs will automatically load any file necessary in a multi-file program).  The cursor is moved to the line where the error was found.  The corresponding error message is scrolled to the top of the *compilation* window.  After studying the error message, you can edit the line of code on the spot. Enter  c-x `  again to process the next error message. If you would like to process the error messages again from the top, enter c-u c-x `

 

Recompiling:  After editing the errors in the source file, recompile with Esc-x compile.  NOTE: You can repeat your most recent Emacs command with c-x ESC ESC;  if the last command was the compile, this will repeat it without re-typing the command.

 

RUN YOUR PROGRAM IN EMACS   Use the ESC ! command to execute your program in the minibuffer. (eg) "ESC ! a.out"

 

OR: SUSPEND EMACS & RUN YOUR PROGRAM FROM THE SHELL-LEVEL.   Suspend emacs with c-z (this resumes your login shell).  Run the program by entering its name as a csh command. E.g.,  if the name of the executable is bullwinkle, as above:

 

            % bullwinkle

 

Now, list your stopped  jobs by entering the jobs command:

 

            % jobs

            [1] + Stopped              emacs                bullwinkle.cpp

 

Note that your Emacs job is listed  as job [1].  You can resume Emacs by entering %1 as a csh command:

 

            % %1

 

The following aliases will streamline this procedure further;  edit your .cshrc startup file to include the following aliases:

 

            alias      j           jobs                 

            alias      1          %1                  

            alias      2          %2

            alias      3          %3

 

Now, it's possible to check your jobs by entering the command j at the csh prompt, and resume job [2] by simply entering 2,  job [3] by entering 3, etc.:

 

            % 1                  <-- resumes Emacs

 

IMPORTANT: If you suspended Emacs to get to the shell, do not use the command "emacs bullwinkle.cpp" to return to Emacs. This will start a second Emacs job--  not a good idea! (and it rubs the fur of the "friendly" gladstone systems staff the wrong way.) If you suspended Emacs (c-z) then resume it with %1.

 

EMACS "SURVIVAL KIT"

 

c-f                    forward one char,

c-b                   back one char  

c-u                   up one line                   

c-n                   down one line

arrow keys      up, down, forward, back

c-d                   delete character (also, DEL or BackSpace key)

c-v                   forward one page                     

esc-v               back one page

esc >               end of file                    

esc <               beginning of file

c-g                   cancel command (very useful to get out of trouble)

c-x c-s             File Save

c-x c-w             File Save As...

c-x c-f              Find File (see more, below)

c-x c-c             Exit emacs

Esc !                Execute Unix command in mini buffer

c-space            Set mark

c-h                   online help        

c-h c-h             help for the help command

c-h t                 emacs tutorial (very useful for new users)

 

c-x 1    one-window. 

After Esc-x compile, your display will be divided into two windows.  Put the cursor in the window showing your source file and enter C-x 1 to restore the display.

 

c-x 2    two-windows. 

This splits the current window into two. The same buffer will be displayed in each-- this is handy for looking at two parts of a large file.  Use C-x C-f to load another file into one window-- then you can view two files at once.

 

c-x o    other window.  Moves the cursor to the other window.  ("cntrl-x oh")

 

c-x c-f  visit-file. 

Prompts for the name of a file in the minibuffer at the bottom of the screen.  If the file exists, it will be loaded into the current buffer.  If it doesn't exist, it will be created. USE THIS COMMAND TO CREATE A NEW FILE IN EMACS.

 

c-x c-b list buffers. 

Shows you a list of all the files you have loaded into Emacs-- this is useful for editing a multi-file program.

 

c-x b    switch to buffer.  Allows you to switch to a named buffer (prompts you with a default).

 

MOVING BLOCKS OF TEXT ("copy and paste", "cut and paste")

1.  Set mark:  move the cursor to the top of the block of code, and type c-spacebar.

2.  Move the cursor to the bottom of the block

3.  Copy (or Delete) the block:  esc-w copies,  c-w  deletes.

4.  Move cursor to target location (this could be in a different buffer entirely, for example).

5.  Yank text:  c-y  "yanks" the text from the kill-ring.

 

LEARNING MORE EMACS

1. Read the Emacs manual page online:   % man emacs

2. Run the Emacs tutorial: start Emacs and enter the command c-h t

 

NOTE TO LINUX AND XWINDOWS USERS:  HOW TO SIMULATE A TTY INTERFACE

 

Usually, if you start emacs under XWindows,  it will start in a new window.  To experiment with job control in emacs (c-z, etc.), you'll want emacs to start in your console window.  To do this, enter the command "unsetenv DISPLAY". "setenv DISPLAY" will reverse the situation (it is also set in .login).  Alternately, you can invoke emacs with emacs -t `tty`.

 

 

See Also