3.2. University of Washington

If you're a student or staff member at the University of Washington, you have access to the Uniform Access computers, several of which give you command lines at which you can do programming and C compilers that you can invoke in the process. (There may also be Macs or PC's in some of the computing centers with C compilers installed on them. If you're using one of those compilers, see the appropriate section below. But if you're using a Mac or PC to log in to one of the Uniform Access computers, using a program such as telnet, then this section applies.)

Not all of the Uniform Access computers have shell prompts and C compilers available. If your primary login is on a machine such as homer or milton, that machine is intended primarily for reading e-mail and the like, and uses a dedicated menu user interface. (It may be possible to escape from those menus to a shell prompt, but I'm not sure how, or if there are C compilers included in the selection of programs underneath.) Therefore, you will need to have an account on one of the ``shell prompt'' machines, such as saul or mead. (You can request an account on one of these shell prompt machines, in addition to your existing account(s), at one of the support desks. Your login name and password will be the same; you'll just have another choice of machine to connect to.)

Step 1: entering or editing your source code

A popular text editor is pico, which is the ``composer'' from the PINE e-mail program, broken out as a separate program for use in editing arbitrary text files. If you use PINE for e-mail, you'll find pico easy to use as well; all of the commands you normally use when editing messages as you compose them (moving the cursor, cutting and pasting, etc.) will apply, except that you'll be editing a text file containing your C code instead of an e-mail message.

If you're doing this for the first time, just type

	% pico
at the shell prompt. (In this example, I'm using % as the shell prompt, yours may be different.) pico will clear the screen and give you a PINE-line menu, with some control-key commands listed at the bottom. (If you get error messages about your terminal type, or if the screen or the text you try to type doesn't appear correctly, you'll have to get this problem resolved somehow, but if you're logging in in the same way that you do when you read your e-mail, the terminal type and emulations are presumably already set up correctly, and pico should work just as well as PINE.)

Type in your program. If you're typing in an initial example from the class notes or from a book, type it in exactly as shown, including all punctuation, and with the lines laid out just as they are in the code you're typing in. (Later, we'll learn what does and doesn't matter in terms of program layout: where your degrees of freedom are, what the compiler does and doesn't care so much about, which syntax you have to get exactly right and which parts allow you to express some creativity. For now, though, just type everything verbatim.)

When you're done typing, type control-O to write the source code that you've just typed in out to a text file. pico will prompt you for a file name; use hello.c (assuming you're typing in the first ``Hello, world!'' example). Exit pico by typing control-X.

If you get compilation errors in step 2, or if the program doesn't work in step 3, such that you have to come back here to step 1, you will edit (modify) your program by typing

	% pico hello.c
(or whatever the filename is), making your changes, writing the file out again with control-O, and then quitting again with control-X.

Step 2: compiling the program

Type

	% gcc -o hello hello.c
(again, assuming you're working with a source file named hello.c). The -o hello part says that the output, the executable program which the compiler will build, should be named hello (that is, placed in a file of that name in the current directory). The C source file to be read and compiled is obviously hello.c.

(If you leave out the -o hello part, the default is usually to leave your executable program in a file named a.out, which is cryptic and somewhat less than perfectly useful.)

Later in the class, when it comes time to compile a program which uses any of the math functions declared in the header file <math.h>, such as sqrt, sin, cos, or pow, you'll have to request explicitly that gcc include the math library:

	% gcc -o myprogram myprogram.c -lm
Notice that, unlike most command-line options, the -lm option which requests the math library must be placed at the end of the command line.

It's also possible to compile multiple C source files, either at once or separately, and to link them all together into one program, but we'll cover those details later, when we start talking about separate compilation at all.

When I tested gcc on saul during the process of writing this handout, it gave me an unusual warning message of the form ``as0: Warning: extraneous values on version stamp ignored''. I have a pretty good idea of what this message means, and it indicates a slight problem in the installation of one of the parts of the compiler, not a problem with my (or your) C program. You can ignore this message; the programs produced by the compiler seem to be just fine in spite of it.

Step 3: running the program

Assuming you did use the -o option to specify the output filename hello when compiling, you can now attempt to run your program by just typing its name:

	% hello
If you get a message like ``not found'' or ``no such file or directory'', it probably indicates that the shell is not searching the current directory for commands that you type. You can either adjust your search path (the PATH environment variable), or else invoke
	% ./hello
to explicitly indicate that the hello you're trying to run is in the current directory, known as dot (.).

As mentioned in the Unix section above, you can capture your program's output in a file (instead of having it go to the screen) by typing

	% hello > hello.out
The output appears in the file hello.out, which will be a plain text file.

Finally, one footnote about pico: If you invoke it with the name of a file that does not exist, e.g. by typing pico hello.c the very first time, pico assumes that you're about to create that file (as, in fact, you are). It gives you the same initially empty editing screen as if you'd invoked it with no filename argument at all, but when you type ^O to write the file out, it remembers the filename from the command line, so you don't have to type it in again. That's all. (In other words, you get to do a ``Save'' instead of ``Save As''.)


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995-9 // mail feedback