Tuesday, September 21, 2010

The Library and Linking

Technically speaking, you can create a useful, functional C or C++ program that
consists solely of the statements that you actually created. However, this is quite
rare because neither C nor C++ provides any keywords that perform such things as
input/output (I/O) operations, high-level mathematical computations, or character
handling. As a result, most programs include calls to various functions contained in
the standard library.
All C++ compilers come with a standard library of functions that perform most
commonly needed tasks. Standard C++ specifies a minimal set of functions that will be
supported by all compilers. However, your compiler will probably contain many other
functions. For example, the standard library does not define any graphics functions,
but your compiler will probably include some.
The C++ standard library can be divided into two halves: the standard function
library and the class library. The standard function library is inherited from the C
language. C++ supports the entire function library defined by Standard C. Thus, all
of the standard C functions are available for use in C++ programs that you write.
global declarations
return-type main(parameter list)
{
statement sequence
}
return-type f1(parameter list)
{
statement sequence
}
return-type f2(parameter list)
{
statement sequence
}
.
.
.
return-type fN(parameter list)
{
statement sequence
}
Figure 1-1. The general form of a C program.

In addition to the standard function library, C++ also defines its own class library.
The class library provides object-oriented routines that your programs may use. It also
defines the Standard Template Library (STL), which offers off-the-shelf solutions to a
variety of programming problems. However, both the class library and the STL are
discussed later in this book. In Part One, only the standard function library is used,
since it is the only one that is also defined by C.
The implementors of your compiler have already written most of the generalpurpose
functions that you will use. When you call a function that is not part of your
program, the compiler "remembers" its name. Later, the linker combines the code youwrote with the object code already found in the standard library. This process is called
linking. Some compilers have their own linker, while others use the standard linker
supplied by the operating system.
The functions in the library are in relocatable format. This means that the memory
addresses for the various machine-code instructions have not been absolutely
defined—only offset information has been kept. When your program links with the
functions in the standard library, these memory offsets are used to create the actual
addresses used. There are several technical manuals and books that explain this
process in more detail. However, you do not need any further explanation of the
relocation process to program in C++.
Many of the functions that you will need as you write programs are in the standard
library. They act as building blocks that you combine. If you write a function that you
will use again and again, you can place it into a library, too.

No comments:

Post a Comment