COSC450 / Producing Executable C Programs under UNIX

C source code is translated into an executable program in 3 stages under UNIX:

1. The C preprocessor reads one or more source files (each typically ending in ".c") and processes them according to preprocessor--such as #define and #include--directives in them.

2. The C compiler takes as input one or more pre-processed files and produces as output the same number of object modules, each ending in ".o" (default).

3. The C linker takes as input one or more object modules and produces as output a single "load module". The load module is given the name "a.out" by default. Only this load module is executable.

UNIX provides a single command "cc" to accomplish all three stages of translation.

Ex: % cc main.c junk.c morejunk.c Produces a single load module from three C source code modules.

The files may be listed in any order! Once the load module has been produced, the object modules are automatically deleted.

To execute the load module ("the program"), enter its name at the system prompt,as in--

% a.out

SEPARATE COMPILATION:

A source code module can be separately compiled by using the "compile only" flag "-c". In this case only an object module is produced, not a load module.

Ex: % cc -c mypgm.c <-- Produces mypgm.o (object code)

LINKING OBJECT MODULES:

Object modules can be linked into one load module by issuing this command--

% cc main.o junk.o morejunk.o <-- Produces a.out, the default (executable) load module

The "cc" command may be invoked with any mix of .c and .o files, as in--

% cc main.c junk.o morejunk.c <-- Produces a.out

These flags may be used in any combination to give you more flexibility. Notice the difference between upper and lower case flags. Consult the man pages on "cc" for further information.

-c Compile separately, producing an object module, not a load module

-lm Load referenced modules from the mathematics library.

-o name Name the resulting load module "name" instead of a.out.

-w Suppress warning messages

-O Use the optimizer

Ex: % cc -o goofy -w -O main.c junk.o

Directs the compiler to produce a load module from the files main.c and junko. Give the load module the name "goofy"; suppress warning messages; and use the optimizer.