Libraries: Pulling in External Code Libraries are helpful code collections you can import into your programs
to prevent having to reinvent the wheel. Virtually every programming lan-
guage has some way of incorporating library functionality into a program:
•
Python, Go, and Java have
import
.
•
Rust, PHP, and C# have
use
/
using
.
•
JavaScript, Lua, R, and Perl have
require
/
requires
.
•
C and C++ have
#include
.
Listing 1-1 included
cstdio
u
, a library that performs input/output
operations, such as printing to the console.
The Compiler Tool Chain After writing the source code for a C++ program, the next step is to turn
your source code into an executable program. The compiler tool chain (or
tool chain) is a collection of three elements that run one after the other to
convert source code into a program:
1. The
preprocessor performs basic source code manipulation. For
example,
#include u
is a directive that instructs the prepro-
cessor to include information about the
cstdio
library directly into
your program’s source code. When the preprocessor finishes process-
ing a source file, it produces a single translation unit. Each translation
unit is then passed to the compiler for further processing.
2. The
compiler reads a translation unit and generates an object file. Object
files contain an intermediate format called object code. These files con-
tain data and instructions in an intermediate format that most humans
wouldn’t understand. Compilers work on one translation unit at a time,
so each translation unit corresponds to a single object file.
3. The
linker generates programs from object files. Linkers are also
responsible for finding the libraries you’ve included within your
source code. When you compile Listing 1-1, for example, the linker
will find the
cstdio
library and include everything your program
needs to use the
printf
function. Note that the
cstdio
header is dis-
tinct from the
cstdio
library. The header contains information about
how to use the library. You’ll learn more about libraries and source
code organization in Chapter 21.