Getting familiar with emacs, g++, make, ddd and gdb

The material covered here is for those who are new to linux. If you are familiar with linux, skip it.

Contents

Editor - emacs

Emacs is a very powerful editor. To lanuch it, type in a terminal:
  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-chrmeans hold the CONTROL key while typing chr. Thus, C-f would be: hold the CONTROL key and type f.
M-chrmeans 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-fMove forward a character
C-bMove backward a character
M-fMove forward a word
M-bMove backward a word
C-nMove to next line
C-pMove to previous line
C-aMove to beginning of line
C-eMove to end of line
M-aMove back to beginning of sentence
M-eMove forward to end of sentence
M-<Go to beginning of file
M->Go to end of file
Undoing Changes
C-x uundo one batch of changes -- usually, one command worth
Mark
C-SPCset the mark where point is
Killing
C-ddelete next character
DELdelete previous character
C-kkill rest of line or one or more lines
C-wkill region (from point to the mark)
Yanking
C-yyank last killed text
M-wsave region as last killed text without actually killing it
Display
C-lclear screen and redisplay, scrolling the selected window to center point vertically within i.
C-vscroll forward (a windowful or a specified number of lines)
M-vscroll backward
Search
C-sIncremental search forward
C-rIncremental search backward
Files
C-x C-fvisit a file
C-x C-ssave the current buffer in its visited file
Directories
C-x dList the contents of a directory
M-x make-directorycreate a new directory
M-x delete-directorydelete the directory. It must be empty, or you get an error
Multiple Windows
C-x 2split the selected window into two windows, one above the other
C-x 3split the selected window into two windows positioned side by side
C-x oselect another window
C-x 0delete the selected window (`delete-window')
C-x 1delete all windows in the selected frame except the selected window
Exiting Emacs
C-zSuspend Emacs (`suspend-emacs') or iconify a frame
C-x C-cKill Emacs
Help
C-h iRun Info, the program for browsing documentation files (`info'). The complete Emacs manual is available on-line in Info.
C-h tEnter 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

Compiler - g++

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:
-gProduce debugging information so that debugger (such as gdb or ddd) can work with this debugging information
-WallIssue 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 filePlace 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

Program maintenance - make

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.ccincludesa.h
b.ccincludesb.h
c.ccincludesc.h
a.hincludesb.h
b.hincludesc.h
The executable file is called hello. It also uses pthread library. Then, in your makefile, type:

  # 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

Debugger - ddd

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:
BreakSet a breakpoint.
RunStart your program.
PrintDisplay the value of a variable or an expression.
ContContinue running your program (after stopping, e.g. at a breakpoint).
NextExecute next program line (after stopping); step over any function calls in the line.
StepExecute next program line (after stopping); step into any function calls in the line.

For details about how to use ddd, visit GNU DDD Manuals

Debugger - gdb

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:]linenoSet a breakpoint at line lineno in file.
b [file:]functionSet a breakpoint at function in file.
r [arglist]Start your program.
p exprDisplay the value of an expression expr.
cContinue running your program (after stopping, e.g. at a breakpoint).
nExecute next program line (after stopping); step over any function calls in the line.
sExecute 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.
qExit 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