This document is for those who have done no software development under UNIX. It is intended to give you a quick jump on using UNIX and to make it easy to edit, compile and run programs.
Under UNIX, the ``standard'' development rule is ``One job, one tool.'' That means that the C compiler does not do anything other than compile. Debugging is done by a different tool, editing (writing or changing your program) is done by yet another tool and so forth. There are recent programs that make it look different, but they usually have a program to do the GUI (graphical user interface) and it then calls all the other tools to get the jobs done. So in some way, the GUI is doing just one job, the GUI!
This document will give you a brief introduction to several things you will need to operate in the UNIX environment. First you need to be able to edit files. Many UNIX installations have many different editors. The My favorite is ``emacs''. Emacs in some flavor is available for almost every computer and operating system made! There are also other editors like ``vi'', ``nedit'', ``VImproved'', ``KDE advanced Editor'', ``pico'', ``nano'' and many others. Vi is a standard editor and every UNIX person should know how to do basic editing in vi. ``nedit'' is a GUI based X11 editor that claims to be ``mouse based.'' It has colorized syntax highlighting for a variety of languages including C++ and C. Most (if not all) of these editors are available on the LDC boxes.
If you desire an IDE, there is ``kdevelop'' on the LDC boxes. I don't recommend it to start your UNIX programming.
Next, you need to be able to interact with the system to ``see'' your files and do other things. This includes compiling your programs and getting them ready to run, printing your programs and so forth.
Finally, this document will give you an introduction to compiling a program from inside emacs and running gdb, the GNU debugger.
To start off, emacs uses ``control characters'' for many commands. The character ``control-g'' (the character ``g'' with the control key down) is written as ``C-g''. The ``C-g'' runs the ``quit'' command. This command is very useful if you end up in some strange mode. Hit ``C-g'' several times and you will often get out of trouble. Also, you should know how to exit emacs. To exit emacs, you need to type the multi-key sequence ``C-x C-c''.
If you are using ``GNU emacs'' you can get help using the ``C-h'' help command. It requires a second character to tell it which type of help you want. The most useful one to the beginner is ``C-h t'' which runs a tutorial. (If the C-h is the same as ``delete'' and you can not run the tutorial using ``C-h t'', try the following: ``M-x help-with-tutorial'' where the ``M'' is usually the ESC key. So ``M-x'' could be the same as ``ESC x''.) The tutorial is very good and makes you use emacs to read the tutorial. This is what I recommend for the new user. Run emacs (just type emacs to your prompt) and then type ``C-h t'' to get the tutorial.
Finally, if you are using GNU emacs, try the following: ``C-u 7 M-x hanoi RET'' where ``RET'' is the return key.
This section gives short descriptions of several commands that are available from csh (and bash) that you will need. These commands helps you manipulate files and directories. I am describing just the basic usage. For most commands, they have options that change the behavior of the command. The csh uses ``*'' as a ``wild card'' to represent any number of characters in a file name. When you login to a UNIX system, you are using a directory called your ``home'' directory. It is also your initial ``working directory''. Unless your command says different, commands use the working directory to find files.
The next few commands are commands that do other things. Most of them have very short descriptions. They are included so you know some commands but if you want more information, you can get it.
Finally, you need to know how to compile and run C programs. First, edit your programs with an editor (emacs). Let us assume you are editing the program in the file ``prog1.c''. To compile the prog1.c give the command:
gcc prog1.c
That one command will compile and if there are no errors, link your
program. The executable file will be named ``a.out''. To run it,
it is best to type ``./a.out''.
To specify the name of the executable file use the command:
gcc -o prog1 prog1.c
The executable file will be named ``prog1''. This should be all you need
to know about the C compiler for the first few week of the quarter.
First, before talking about how to do the commands, we need to review how to talk about key strokes in emacs. Control characters are denoted by the form ``C-g'' (control g). There is also the idea of a ``meta'' key that works similarly to the control key. The ``meta'' keys are denoted by the form ``M-x'' (meta x). On most terminals, there is no ``meta'' key. To allow for the ``M-x'' characters on most terminals, ``M-x'' maps to the key sequence ``ESC x'' where ``ESC'' is the escape key.
Assume you are editing a file named ``prog1.c''. To compile the file from emacs, give the command ``M-x compile''. The first time this command is given for each emacs session, emacs will respond to the command with
Compile command: make -k
in the bottom line of the display. (After you learn how to use
make(1), you will not have to do the following.) Delete the ``make -k'' using
the delete key or using the commands ``C-a C-k''. (C-a == move
cursor to beginning of line. C-k == kill characters to end of line.)
Then type your
usual UNIX command for compiling prog1.c. That would be
gcc prog1.c
or the form
gcc -o prog1 prog1.c
A carriage return after typing the compile command will run the compiler.
(emacs will remember this new command for future executions of the
emacs compile command. For further executions, all that is required is
the carriage return.)
To do the compile, emacs will split your screen into two windows. One window is the original program, the other window is the ``compile log''. You will see the ``gcc ...'' command given by emacs and the results. When the compilation is completed you will see the message
Compilation finished at ....
where the dots represent the date and time. (You may need to scroll the
compile log window to see all the results. Consult your emacs documentation
for dealing with two windows and scrolling windows.)
If you had errors in your compile, emacs will help you find your errors. With your cursor in the program window, give the command ``C-x `''. (That is the single back-quote (`), not the single quote (').) Emacs will then move your program window to the line of the first error message in the compile log. It will also move the compile log so that the first error is displayed at the top of the window. You then edit as usual the program to correct your error. When you have finished dealing with the first error, give the ``C-x `'' command to go to the next error. Again, emacs will place your program at the line of the error and show the error at the top of the window. You continue this edit and ``goto next error'' until you are ready to recompile. To recompile, just give the compile command again.
Finally, if you like this but don't like typing ``M-x compile'' all the time, you can configure emacs by creating a file called ``.emacs'' in your home directory. In this file, add the following line:
(global-set-key "\C-x\C-m" 'compile)
This allows the keys ``C-x C-m'' to run the compile command. Another
favorite key stroke addition that can help is:
(global-set-key "\C-xg" 'goto-line)
gcc -g -o prog prog.c
Also, to get your environment ready for this, add the following line to
your .cshrc file in your home directory. (Do this is you are using CSH
or don't know what shell you are using. Bash users have a slightly different
way to set this.)
limit coredumpsize unlimited
After this change, logout and login again. (There are other ways of getting
your CSH updated, but this is the easiest to tell you.)
Now, with the above two changes, you then develop your programs. If you get a ``segmentation violation'', you should see the message ``core dumped'' and see a new file called ``core''. (On NetBSD you will see a file called ``prog.core''.) Now run gdb as follows:
gdb prog core
It will tell you which file and what line of your program the problem
occurred. You can even look at the values of the variables at the
time of the the problem.
The following is an example session on a NetBSD machine:
---->~/cs352gdb
nooksack[6]$ ls
p1.c
---->~/cs352gdb
nooksack[7]$ cat p1.c
#include <stdio.h>
void f1 (int *a)
{
printf ("a is %d\n", *a);
}
int main()
{
int *myptr=NULL;
f1(myptr);
return 0;
}
---->~/cs352gdb
nooksack[8]$ gcc -g -o p1 p1.c
---->~/cs352gdb
nooksack[9]$ ./p1
Segmentation fault (core dumped)
---->~/cs352gdb
nooksack[10]$ ls
p1 p1.c p1.core
---->~/cs352gdb
nooksack[11]$ gdb p1 p1.core
GNU gdb 5.0nb1
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386--netbsdelf"...
Core was generated by `p1'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/libexec/ld.elf_so...done.
Loaded symbols for /usr/libexec/ld.elf_so
Reading symbols from /usr/lib/libc.so.12...done.
Loaded symbols for /usr/lib/libc.so.12
#0 0x8048910 in f1 (a=0x0) at p1.c:5
5 printf ("a is %d\n", *a);
(gdb) print a
$1 = (int *) 0x0
(gdb) quit
The following is a list of commands that are useful in gdb, available at the ``(gdb)'' prompt:
Note: This section on printing is specific to the LDC lab at WWU.
To print a file (on NetBSD), you use the command ``lpr''. (Read the man page for all possible options.) You do need to know how to select which printer on which to print. This is done by use of the ``-P'' flag. For example:
lpr -Pcf416 file1
prints ``file1'' on the HP laserjet in CF 416. If you don't
use the ``-P'' flag, it defaults to the printer in CF416 unless you
have the ``PRINTER'' environment variable defined to be a different
printer. The possible printer names valid with the ''-P'' flag are
cf416, cf406, cf162, and cf164.
To see the print jobs in the queues, you use the ``lpq'' command. For example:
lpq -Pcf416
will show the list of jobs waiting to be printed on the cf416 printer.
It should print something like:
Rank Owner Job Files Total Size 1st phil 1 Makefile 685 bytesNotice the job number. With that job number and the program ``lprm'' you can delete a print job from the queue. For example:
lprm -Pcf416 1
will delete job 1 from the cf416 print queue.
Also, there are other programs that may help you print. First, some editors can directly print a copy of your source code. ``a2ps'' is a program that takes text files and prints them in a nice format on a postscript printer. All our printers understand postscript. You may need to define the environment variable PRINTER for some of the editors. ``a2ps'' also has a ``-P'' flag just like the ``lpr''. ``a2ps'' also takes a ``-o filename'' flag if you want to generate a postscript file instead of print it. You can view the postscript files using either ``gs'' (ghostscript) or the KDE version called ``kghostview''. The second one produces nicer output. ``kghostview'' also allows you to view .pdf files.
Feedback on how to improve this document is requested.
Last modified: 6 Oct 2005
This document was generated using the LaTeX2HTML translator Version 2002-2-1 (1.70)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split 0 -nonavigation unixhelp
The translation was initiated by Phil Nelson on 2005-10-06