8. Running Programs on UNIX

This chapter describes the general procedures needed to compile, load, link, and execute programs written in C. To run programs written in other languages, see the appropriate man pages.

C Programs

The C compiler on UNIX is cc, which is called with the command
     cc [options] file ...  
CC gives special interpretations to filename suffixes. It treats filenames ending in ``.c'' as C source programs. When it compiles these programs, it writes object programs to files that have the same name but with the ``.c'' suffix replaced by ``.o''. Unless options on the cc command prevent it, the ``.o'' file is loaded into an executable file named a.out and the ``.o'' file is then removed. To execute the resulting program, simply type
     a.out 
as a command.

Here are some of the most useful options to the cc command:

-c

Suppresses loading and compiles only: it merely creates ``.o'' files (which may be used as input to a subsequent cc command).
-g
Generates a special symbol table that can be used by debugging programs (see chapter 8).
-o outfile
Names the resulting executable file outfile instead of a.out, thus preventing you from overwriting any existing file named a.out.
For a complete list of options, see the man page for the cc command.

Getting Listings of Source Code and Error Messages

To print a listing of your source file and add to the listing page headers, page breaks, and line numbers, use the -n flag on the pr command, like this:
     pr -n mysource.p | lpr -Pacwl_lw 
To intersperse compiler error messages with source code, you can use the error program. For example, suppose you have a C program in a file named testrun.c and you compile only:
     cc -c testrun.c 
If there are errors, the C compiler will write its error messages to your terminal. It would be more useful to intersperse these error messages into the source code at the place where they occur, and the error command does this. For the sample C program (and assuming you are running the C shell), you could use
     cc -c testrun.c |& error -q 
The |& here assures that both standard output and error output from cc will be piped to the error program. The -q option tells the error program to query you before putting error messages into the file testrun.c. Its output might look like this:
     File ``testrun.c'' has 3 errors.
          3 of these errors can be inserted into the file.
     Do you want to preview the errors first?
At this point, if you answer y, you will see just the messages and be prompted again:
     Do you want to touch file ``testrun.c''?
If you answer y again, the error program rewrites testrun.c, with each error message preceding the line that caused the error. You can then edit the file and try compiling it again.

For more information about how error handles messages, see

     man error

Tools for Maintaining Large Programs

If you write large programs that require libraries of routines, or programs that consist of many files, there are UNIX tools to help you maintain them and keep track of modifications.

The ar command maintains groups of files combined into a single archive file. The ranlib command converts archives to random libraries for easier loading by the loader. On UNIX systems running Solaris, use the lorder command instead.

The make program is useful for maintaining and updating groups of programs in parallel.

The sccs program provides mechanisms for tracking revisions so that you can revert to an earlier version of a program or maintain multiple versions.

For more information, see

     man sccs 
Go to next chapter

Go back to table of contents