Skip to main content

Unix Guide - OIT Unix Compilers


This is an overview of how to compile programs on Unix machines. The examples are specific to the C compiler but can be applied to all Unix compilers. Note that compilers are available only on rintintin, rastro, and eddie--not on the information-only machines like spot. To find out which compilers are on a particular machine, type:

man -k compile

This command will produce a list of all the compilers that have manual pages and will indicate which sections address compilers. For example, on spot the output looks like this:

Spot Compilers

To find out more about a specific compiler, for example, the Zic time zone compiler, type:

spot> man zic

Basic Command Form

The basic form of a compile command line is:

rintintin> compiler_name [options] source_file

For example:

rintintin> cc hw.c

will compile the C source file hw.c and create an executable file. By default, Unix compilers will compile and link a program, creating an executable named a.out. This can be overridden by using options on the command line. It is important that your source files have the proper file extensions when you try to compile them. For example, C files should have a .c extension, FORTRAN files should have a .f extension, and PASCAL files should have a .p extension. If you try to compile a program with the wrong file extension, you might get an error message.


Unix compilers have many options; here is a list of some of the most commonly used and helpful ones.

-o fname
Create an executable named fname instead of a.out.

Suppress the loading phase of the compilation, i.e., don't create the executable file, just the .o object file.

Have the compiler produce symbol table information for the debugger dbx(1).

Define name for the macro preprocessor as if it was done with a #define statement.

Search dir_name for #include files before searching in the standard directory, /usr/include.

Run the code through the optimizer. Most compilers let you specify the level of optimization with a number after the 0. Levels usually run between 0 and 3 where 0 is no optimization and 3 is the highest level of optimization. See the individual man pages for details. In general it is not a good idea to use the higher level of optimization with the -g debugging information option.

Produce execution-profiling information for the prof(1) postprocessor.

Other Options

In addition to specifying options to the compiler, you can specify options to the link editor, ld(1). Options that the compiler does not recognize are passed to ld. The most common option given to ld is the -l option.


Search library libname.a. Libraries are searched as they are found on the command line so placement is important. Common libraries used are:

libm.a - Math library
libcurses.a - Curses library
libtermcap.a - Termcap library
libX11.a - Xlib library
libXt.a - Xtoolkit library
libXaw.a - X11 Athena Widget library

The following example compilation line compiles the C source file myprog.c with default optimization and symbol table information into the executable file myprog. The name OIT is defined to the C macro preprocessor and during the link phase the Math Library (/usr/lib/libm.a) is searched.

cc -o myprog -g -O -DCNS myprog.c -lm

If your program consists of separate files, you will want to compile each piece separately into an object file and then link all the object files together. The compiler passes object files to ld(1), the link editor, to be linked. For example, if my program consists of two files, main.c and utils.c, I would use the following commands to compile them into a program called myprog.

First create the individual object files, main.o and utils.o:

cc -c main.c
cc -c utils.c

Now link the object files together into an executable named myprog:

cc -o myprog main.o utils.o

Other Programs

Several programs can be helpful in developing programs on Unix machines. Here is a short description of some of them; look at individual man pages for more information.

make - Maintain, update, and regenerate groups of programs

lint - Perform type checking and detect bugs, non-portable & wasteful code

rcs - Revision Control System, manage multiple versions of program files

dbx - Source level debugger

prof - Analyze execution profile data