3.1. Unix

Since C is the programming language of Unix, it used to be the case that virtually all Unix machines had C compilers. That's less true today (alas!), but C compilers are still very common. One extremely popular Unix compiler, which happens to be of extremely high quality, and also happens to be free, is the Free Software Foundations's gcc, or GNU C Compiler.

Step 1: entering or editing your source code

The most popular Unix text editors are vi and emacs. I'm not going to provide tutorials on them here; if you've been using Unix for a while, there's a good chance that you've become familiar with one or the other of them. Like most text editors, you can invoke them in one of two slightly different ways: either with no command-line arguments, in which case you are given a blank screen in which to begin typing (following which you must save your work to a file, after selecting a filename); or with a filename as a command-line argument, in which case that file is read in so that you can make incremental changes to it (following which you must of course again save your work, which by definition will go back to the same file as you initially read in). Another popular text editor is pico, which is easy for beginners to use, particularly if you use the PINE e-mail handling program. (pico is PINE's ``composer'', made available as a standalone program.) See the ``University of Washington'' section below for more information about pico.

For example, for the first ``Hello, world!'' program, you might type one of the following four commands:

	$ vi
	$ emacs
	$ vi hello.c
	$ emacs hello.c
(In these and the later examples, I'm using $ as a generic Unix shell prompt.) In the first two cases, you'd be given a blank slate, and you'd save your work (with :w in vi or ^X^W in emacs), specifying a filename of hello.c. In the second two cases, you'd be presented with hello.c (if it existed), and when you were finished, you'd save it back to that file (with :w again under vi, or ^X^S under emacs).

In any case, whichever text editor you're using and however you choose to invoke it, make sure to type in the program (if it's an initial example from the class notes or from a book) 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.)

Step 2: compiling the program

Most Unix C compilers use about the same command-line syntax. To compile our ``Hello, world!'' program, you'd type

	$ cc -o hello 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're using gcc, the command line is almost identical:

	$ gcc -o hello hello.c
If cc gives you strange syntax errors, it may be that it's an old-fashioned, pre-ANSI compiler, in which case it won't be able to compile most of the sample programs we'll be working with (unless you were to edit them slightly). For many systems where plain cc is obsolete, there is an ANSI C compiler available, often called acc. Again, the invocation is probably the same:
	$ acc -o hello hello.c

For any of these invocations, if you leave out the -o hello part (i.e. if you just type cc hello.c), the default is usually to leave your executable program in a file named a.out, which is cryptic and somewhat less than perfectly useful.

If you're compiling 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 typically have to request explicitly that the compiler include the math library:

	$ cc -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 use cc (or gcc, or acc, or any of the others) 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.

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 your 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 (.).

If the program prints its output to the screen (as most programs do), and if you'd like to save it (for posterity, or to print out or mail to me for review), you can type something like

	$ hello > hello.out
On the Unix command line, a greater-than symbol > requests that the program's output be redirected to the named file, instead of going to the screen. (Later, when you begin writing programs that read input from the keyboard, you may be interested to know that it's possible to redirect a program's input as well, using the less-than symbol, <. An invocation like program < inputfile requests that the program read its input from the file named inputfile instead of from the keyboard.)

Finally, one last word on those text editors. I glossed over one detail, which may be troubling you: If you invoke a text editor with a filename as a command line argument, but the file does not exist, most editors assume that you're about to create it. You start out with a blank screen, almost as if you'd invoked the editor with no filename argument at all, but when it comes time to save or quit, the editor remembers the filename, 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