The material covered here is for those who are new to linux. If you are familiar with linux, skip it.
emacs &
Soon, an emacs window will appear.
New comers might feel very uncomfortable using emacs. Don't panic. It's a matter of habit. Once you get used to it, you might find that emacs is the best editor (even more than that) you've ever seen.
For beginners, please start learning emacs by reading the tutorial of emacs. To do so, in the emacs window,
hold the CONTROL key while typing `h' type `t'
Then read the tutorial while doing some practices as you are told. For your convenience, Below are some basic operations in emacs:
Notice:
C-chr | means hold the CONTROL key while typing chr. Thus, C-f would be: hold the CONTROL key and type f. |
M-chr | means hold the META or EDIT or ALT key down while typing chr. If there is no META, EDIT or ALT key, press and release the ESC key and then type chr. |
Moving Point | |
C-f | Move forward a character |
C-b | Move backward a character |
M-f | Move forward a word |
M-b | Move backward a word |
C-n | Move to next line |
C-p | Move to previous line |
C-a | Move to beginning of line |
C-e | Move to end of line |
M-a | Move back to beginning of sentence |
M-e | Move forward to end of sentence |
M-< | Go to beginning of file |
M-> | Go to end of file |
Undoing Changes | |
C-x u | undo one batch of changes -- usually, one command worth |
Mark | |
C-SPC | set the mark where point is |
Killing | |
C-d | delete next character |
DEL | delete previous character |
C-k | kill rest of line or one or more lines |
C-w | kill region (from point to the mark) |
Yanking | |
C-y | yank last killed text |
M-w | save region as last killed text without actually killing it |
Display | |
C-l | clear screen and redisplay, scrolling the selected window to center point vertically within i. |
C-v | scroll forward (a windowful or a specified number of lines) |
M-v | scroll backward |
Search | |
C-s | Incremental search forward |
C-r | Incremental search backward |
Files | |
C-x C-f | visit a file |
C-x C-s | save the current buffer in its visited file |
Directories | |
C-x d | List the contents of a directory |
M-x make-directory | create a new directory |
M-x delete-directory | delete the directory. It must be empty, or you get an error |
Multiple Windows | |
C-x 2 | split the selected window into two windows, one above the other |
C-x 3 | split the selected window into two windows positioned side by side |
C-x o | select another window |
C-x 0 | delete the selected window (`delete-window') |
C-x 1 | delete all windows in the selected frame except the selected window |
Exiting Emacs | |
C-z | Suspend Emacs (`suspend-emacs') or iconify a frame |
C-x C-c | Kill Emacs |
Help | |
C-h i | Run Info, the program for browsing documentation files (`info'). The complete Emacs manual is available on-line in Info. |
C-h t | Enter the Emacs interactive tutorial. |
For more details about how to use emacs, visit GNU Emacs Manual
Some students are unwilling to use emacs. The reason might be that key CONTROL is heavily used in emacs but the position of CONTROL make you feel very uncomfortable. If Control and Caps_Lock can be swapped, you'll find life becomes easy with emacs. Here is the solution:
Please add the following lines in `.bash_profile', `.bash_login' or `.profile' in your home directory. (choose the one that is already in your home directory and PLEASE copy the following lines exactly, NO MORE NO LESS.)
# Swap Caps_Lock and Control_L if necessary # # When the runlevel is 3 (full multiuser mode), loadkeys works # When the runlevel is 5 (X11), xmodmap works rl=`runlevel | awk '{print $2}'` case $rl in 3) loadkeys << " EOF" keymaps 0-2,4-5,8,12 keycode 29 = Caps_Lock keycode 58 = Control EOF ;; 5) lock=`xmodmap | awk '/^lock/ { print $3 }'` test "$lock" = "(0x25)" || xmodmap - << " EOF" remove Lock = Caps_Lock remove Control = Control_L keysym Control_L = Caps_Lock keysym Caps_Lock = Control_L add Lock = Caps_Lock add Control = Control_L EOF ;; esac
After you finish editing a program and save it in a file, you have to compile it before you execute it. g++ is for this purpose.
To comiple a file, say "assignment.cc", type in the command line:
g++ -g -Wall assignment.cc -o assignment
where:
-g | Produce debugging information so that debugger (such as gdb or ddd) can work with this debugging information |
-Wall | Issue warnings for conditions which pertain to usage that we recommend avoiding and that we believe is easy to avoid, even in conjunction with macros. |
-o file | Place output in file |
For details about how to use g++, type in command line:
man g++
or type in emacs:
C-h i m gcc
or visit GNU GCC Manuals
When you write a big program, it is a good idea to orginize your program in several files called source files. A utility called make can help you do this work. Make uses a file called makefile that describes the relationships among files in your program, and the states the commands for updating each file. Once a suitable makefile exists, each time you change some source files, this simple shell command:
make
suffices to perform all necessary recompilations.
Here is an example showing you how to write makefile. Suppose we split our program into six source files a.cc, b.cc, c.cc a.h, b.h, and c.h. The inclusion is as follow:
a.cc | includes | a.h |
b.cc | includes | b.h |
c.cc | includes | c.h |
a.h | includes | b.h |
b.h | includes | c.h |
# This is a comment line # CXX is an implied variable used as the name of C++ compiler. The # default value is `g++'. So you don't have to write the following line. CXX = g++ # CXXFLAGS is an implied variable used as the C++ compiler options. # No default value. CXXFLAGS = -g -Wall $(INCLUDES) # Specify the libraries. LIBS = -lpthread # Specify the directories to be serach for header files. INCLUDES = # Object files. OBJS = a.o b.o c.o hello : $(OBJS) $(CXX) $(CXXFLAGS) $(OBJS) $(LIBS) -o hello # ^ # |____ Be noticed that the space here must be produced by TAB. # The following is the default rule for generating .o files from # .cc files. You don't have to write it in makefile. .cc.o : $(CXX) -c $(CXXFLAGS) $< # File dependence. a.o : a.h b.h c.h b.o : b.h c.h c.o : c.h
That's all. Simple, isn't it?
Usually, in makfile, we also include the following lines to clean files that can be generated by other files. They are very useful when you find that you've used up all your quota.
realclean : clean -rm -f hello *~ clean : -rm -f $(OBJS) core
For full details on make and makefile, type this shell command:
man make
or visit The GNU Make Manual
No one dares to say his programs are 100% correct. Sometimes you'll find it difficult to find out where the bug is. A window-interface tool ddd can help you out in such situations. To debug a program, for example, lab1, type in the command line:
ddd lab1 &
Notice: if instead of popping up a window the following error message appears:
ddd: error in loading shared libraries: libXm.so.1: cannot open shared object file: No such file or directory
type in the command line:
LD_LIBRARY_PATH=/usr/local/lib export LD_LIBRARY_PATH
Some most frequently used buttons:
Break | Set a breakpoint. |
Run | Start your program. |
Display the value of a variable or an expression. | |
Cont | Continue running your program (after stopping, e.g. at a breakpoint). |
Next | Execute next program line (after stopping); step over any function calls in the line. |
Step | Execute next program line (after stopping); step into any function calls in the line. |
For details about how to use ddd, visit GNU DDD Manuals
You've already know ddd. In fact, ddd is actually only an interface and it uses gdb - the GNU debugger.
To debug a program with gdb, for example, lab1, type in the command line:
gdb lab1
Some most frequently used commands in gdb are:
b [file:]lineno | Set a breakpoint at line lineno in file. |
b [file:]function | Set a breakpoint at function in file. |
r [arglist] | Start your program. |
p expr | Display the value of an expression expr. |
c | Continue running your program (after stopping, e.g. at a breakpoint). |
n | Execute next program line (after stopping); step over any function calls in the line. |
s | Execute next program line (after stopping); step into any function calls in the line. |
h [name] | Show information about GDB command name, or general information about using GDB. |
q | Exit from GDB. |
You can also use gdb in emacs. To launch gdb in emacs, type in emacs:
M-x gdb
Then give the executable file you want to debug as an argument.
The same commands in gdb can be used in emacs environment. Moreove, you don't have to know the line number before you set a breakpoint. To do so, split the emacs window into two. Open the source file you want to debug in one window and move the cursor to the line you want to set a breakpoint. Then, type:
C-x SPC
For full details on gdb, type this shell command:
man gdb
or type in emacs:
C-h i m gdb
or visit The GNU GDB Manual