C++ Crash Course: a fast-Paced Introduction


Libraries: Pulling in External Code



Yüklə 7 Mb.
Pdf görüntüsü
səhifə40/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   36   37   38   39   40   41   42   43   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

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.



Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   36   37   38   39   40   41   42   43   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin