Hey Seth,

Last night one of the guys was asking about Emacs last night. Specifically
on how to do C++ editing, compiling and running c++ programs with Emacs.

I took the liberty to put the information into a webpage that can be tied
into the homepage for the club.

Please check it out and let me know if there are any discrepancies.

You will find the file attached.

Ron
Title: Using Emacs to Edit, Compile, & Execute C++ Programs
Using Emacs to Edit, Compile, and 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 the consistently quit Emacs to compile at the shell/console level, and then restart 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 mini-buffer (at the bottom of the screen), and then executes the command in a sub-shell with output going to the buffer named *compilation*.

The mini-buffer 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, linuxrocks.cpp, then enter the command g++ -o linuxrocks linuxrocks.cpp. At the next Esc-x compile, you will 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' in parentheses. (If you are compiling on 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 the Esc-x compile. Note: you can repeat your most recent Emacs command with the 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 mini-buffer. (E.g.) "ESC ! a.out".

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

    % linuxrocks

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

    % jobs
    [1] + Stopped Emacs linuxrocks.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

Caution: If you suspended Emacs to get to the shell/terminal, do not use the command "emacs linuxrocks.cpp" to return to Emacs. This will start a second Emacs job, and is not what you want to do. If you suspended Emacs (c-z) then resume it with %1.

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

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


BASIC EMACS COMMANDS

Arrow keys up, down, right, and left
esc-v Back one page
esc > End of file
esc <  Beginning of file
c-f Forward one character
c-b Back one character
c-u Up one line
c-n Down one line
c-d Delete character (also, DEL or Backspace key)
c-v Forward one page
c-g Cancel command (very useful for getting out of trouble)
c-x c-s Save file
c-x c-w Save file 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.
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 curser to the other window. ("ctrl-x oh")
c-x c-f Visit-file. Prompts for the name of a file in the mini-buffer 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 the cursor to the 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 c-h t
 

ls List all the files in the current directory
ls -l List all files in current directory & show permissions
ls -a List all files in current directory including those beginning with a "."
Pwd Return path name to the current directory
cd Change directory
cd .. Move up one directory (can also be linked to move up further: cd ../.. etc.
mv Rename a file or move it to another location
cp Make a copy of a file
rm Delete (remove) a file
mkdir Create (make) a directory
rmdir Delete a directory
more Display the contents of a file
quota -v Display your disk quota and usage
passwd Change log-in password
chmod Change permissions (e.g. chmod 644 filename; chmod 755 directoryname and cgi scripts
man Display online documentation

File permissions for the Web: Directories: 755; files: 644; cgi scripts: 755

This document was blatantly plagiarized with no knowledge of who the original author is, and should fall under the "fair use" clause or at least the "copyleft" paradigm.

Reply via email to