Compiling and linking
The C compilation system consists of a
compiler, assembler, and link
editor. The cc command invokes each of these components automatically
unless you use command line options to specify otherwise.
An executable C program is created with the following four steps:
-
The preprocessor component of the compiler reads lines
in your source files that direct it to
replace a name with a token string (#define),
perhaps conditionally (#if, for example).
It also accepts directives in your source files
to include the contents of a named file in your program (#include).
Included header files for the most part
consist of #define directives and
declarations of external symbols,
definitions and declarations that you want to
make available to more than one
source file.
See
``Libraries and header files''
for details.
-
The compiler proper,
cc(CP),
translates the C language code
in your source
files, which now contain the preprocessed
contents of any included header files,
into assembly language code.
-
The assembler,
as(CP),
translates the assembly language
code into the machine instructions of the computer
your program is to run on.
These instructions are stored in
object files that correspond to each of your source files.
Each object file contains a binary
representation of the C language code in the
corresponding source file.
Object files are made up of sections,
of which there are usually at least two.
The text section
consists mainly of program instructions.
Text sections normally have read and execute, but not write, permissions.
Data sections normally have read, write, and execute permissions.
-
See
``Common Object File Format (COFF)'',
and
``ELF object files''
for details of the supported object file formats.
-
The link editor,
ld(CP)
links these object files with each other and with
any library functions that you have called in your program.
The link editing model you have chosen determines
when the library functions are linked:
-
An archive, or statically linked library,
is a collection of object files each of
which contains the code for a
function or a group of
related functions in the library.
When you use a library function in your program,
a copy of the object file that contains the function is incorporated in
your executable at link time.
-
A static shared library
is a file containing object code
that several processes may use simultaneously while executing.
When a program is link edited with a static shared library,
the library code that defines the program's external references is not
copied into the program's object file.
Instead, a special section called .lib
that identifies the library code is created in the object file.
When the resulting a.out file is executed,
it uses the information in this section to bring
the required shared library code
into the address space of the process. It should be noted
that only one copy of the static shared library code is in memory
and is shared by all processes using that library.
-
A shared object, or
dynamically linked library
as in our case here,
is a single object file that contains the code for
every function in the library.
When you call a library function in your program
and specify a dynamic linking option (-dy)
on the cc command line, the
entire contents of the dynamically linked library
are mapped into the virtual address space of your
process at run time.
As its name implies, a dynamically linked library contains
code that can be used simultaneously by different programs at run time.
See
``Link editing''
for more information on how statically and dynamically linked
libraries are implemented, and details on
how to combine the static and dynamic linking
approaches in different ways
according to your needs.
NOTE:
You can build dynamically linked libraries only for
ELF binaries.
See
``Shared libraries''
for more information on
how to build a statically shared library in COFF file format.
``Organization of C Compilation System''
shows the organization of the C compilation system.
Details concerning the optimizer are omitted here because it
is optional.
See
``Commonly used cc command line options''
for more information concerning invoking the optimizer.
Organization of C Compilation System
Next topic:
Basic cc command line syntax
Previous topic:
C compilation system
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003