Program Development on NetBSD (UNIX)

By Phil Nelson

This is a brief primmer on the tools necessary to do C and C++ development on NetBSD (UNIX). (This is specific for WWU's LDC laboratory running NetBSD/i386. It should also work in many other UNIX like environments including Cygwin.)

The typical things you need to do to compile programs are: edit your source file (create and modify), compile the program, link the program, run the program, debug the program and print the source code. I'll also mention a few utilities that are useful for classes taken from Phil Nelson.

You will also need access. For remote access to WWU's LDC you will need a ssh client. There are many available. Most UNIX varients include ssh(1) as a standard command. For Windows, you can use cygwin and make sure you include ssh in the install or you can use a Windows program like putty.


Editing

The editor "vi" comes with virtually every UNIX system. So in some sense it is the standard editor. Not everyone likes vi, but most UNIX users should be able to use vi a little bit.

Vi is a mode based editor. There is a command mode that includes motion around the file and an insert mode where characters are inserted into the file you are editing. For all the details on how to use vi, give the unix command:

     man vi

"Emacs" is another editor that is often used on UNIX. Emacs or some some editor with the feel of Emacs is available for MANY OSes. The one in use here at WWU is GNU emacs. When using X, it provides a nice point and click interface with pulldown menus.

Emacs is an editor in which commands are often control characters or sequences of control characters and other characters. Emacs is highly configurable, so the feel of emacs for one user may be completely different than for anothe user. The easiest way to learn emacs (GNU version) is to read the tutorial. To read the tutorial, run emacs and then type characters Control-H followed by "t".

Another editor that is available is "nano". It is the editor similar to the one used by the mail program "pine" for composing messages. It works quite well.


Compiling, Linking and Running

To compile a program on UNIX, you normally run the C compiler from a command line prompt. Assume you have a file call "prog.c" with C source in the file. The command:
     gcc prog.c
will compile the program to a temporary object file, then link the object file into an executable named "a.out". To run the program, give the command:
    ./a.out
(Note: The ./ may not be required if . is in your PATH. If you don't understand that statement, read more about your shell. If you are using csh give the command man csh. If you are using sh, give the command man sh.)

Usually, more options are useful in using the C compiler. For example, if you don't want the executable called "a.out", you need to use the -o name flag. For example,

     gcc -o myprog prog.c
would compile prog.c to a temporary object file and then link it to produce the executable called "myprog". The -g option tells the compiler to include debugging information in the program so you can use a symbolic debugger. gdb is the debugger that is available on the WWU NetBSD systems.

The -DNAME=VALUE option defines a C preprocessor symbol NAME will be defined as VALUE. For example:

     gcc -g -o prog -DMAX=35 prog.c
will compile the file prog.c to the executable prog and will replace the symbol MAX with the value 35. It will also include debugging symbols in prog.

Sometimes it is convenient to have your C source in several files, for example, file1.c, file2.c, and file3.c. They may be compiled and linked using the command:

     gcc -g -o myprog -DMAX=35 file1.c file2.c file3.c
Often, when you have multiple file, only one file needs to be recompiled as you are working on your program. With the previous command, all files will be recompiled each time you compile. With multiple files, it is often better to separate the compile and link commands so one needs only compile the file that needs to be compiled. The commands
     gcc -g -c -DMAX=35 file1.c
     gcc -g -c -DMAX=35 file2.c
     gcc -g -c -DMAX=35 file3.c
     gcc -g -o myprog file1.o file2.o file3.o
will do the same thing as the one line command except it also keeps the object files (file1.o, file2.o and file3.o). With the .o files kept, if one makes a change in file2.c and wants to compile again, one only now needs to give the commands:
     gcc -g -c -DMAX=35 file2.c
     gcc -g -o myprog file1.o file2.o file3.o
This is still a lot of work, so once you have multiple files, it is nicer to use the utility make.

C++ works the same way, except files usually end with .C, .cc, or .cxx and you use the command g++ instead of gcc.


Debugging

The debugger available on NetBSD is gdb. It is quite a powerful debugger. To be able to use it well will require a lot of time of working with it. This is intended to get you using it and not make you a real good. NetBSD 1.4 and later systems have full documentation online. They can be read with the GNU info reader by
    info gdb
(You might want to learn emacs key strokes and the info reader first. Typing control-H i will get you into info mode in emacs. From there h will get an info help page.)

To run with the debugger, one must compile your program and link with the "-g" flag. (See above.) It is wise to always compile with that flag during program development so you don't have to recompile if you do need to use the debugger.


Last modified: Sept 29, 2004. Copyright (C) 2001. You may print or use this page for your personal learning of UNIX. Please to not copy this for any other purpose without prior written permission.