Section_MultipleFileProgramACloserLook_Slides
Section_MultipleFileProgramACloserLook_Slides
2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Programs with Multiple Files : A
closer look
3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utilities.h utilities.cpp
main.cpp
4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++ Compilation model
6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One file program
Compiler
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
source TU
object
Preprocessing Compilation
source TU
object
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
object
Linking
object
9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One file program
• Preprocessing
• Compilation
• Linking
10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
IDEs actually call compilers behind the scenes for us to compile our code
and generate an binary we can run. In this lecture, we’ll see how we can
bypass the IDE and do all this ourselves.
12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
GCC will be our compiler in the course but the concepts apply
to any C++ compiler out there : MSVC, Clang,…
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utilities.h utilities.cpp
main.cpp
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile and link in one go!
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile only : generate object files
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Link up object files
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compiler path should be in system environment variables
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Declarations and definitions
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Declaration introduces the name in a file, a definition says
what that name is or what it does.
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A few facts about declarations & definitions
• If you compile without a declaration, and the name is used, you get a
compiler error : unkown name
• If you compile with declaration without definition and the name is not
used,code compiles
• If you compile with declaration, without definition and the name is used, you
get a linker error.
23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Variables
• Functions
• Classes
24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Variables
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Functions
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Functions
27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Classes
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Classes
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One Definition Rule
31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Definitions can’t show up more than once in entire program,
or Translation units.
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One definition rule : context
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Linkage
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A property associated with a name, that controls how wide
or narrow is the visibility of the name across translation units
37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
No linkage
38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Internal linkage
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Extern linkage
40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Linkage options
• No linkage
• Internal Linkage
• External Linkage
• Module Linkage(later)
41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Linkage defaults
42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Global variables
44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
some_other_file.cpp
46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Flipping Linkage
48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
some_other_file.cpp
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inline variables and functions
52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility1.cpp
utility2.cpp
53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility1.cpp
utility2.cpp
55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility1.cpp
56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility.h
57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inline VS static (unnamed
namespaces)
59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Inline will optimize all the definitions for a name into one
• Static or unnamed namespaces won’t do such optimizations
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility1.cpp
61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility2.cpp
62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Forward Declarations
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Sometimes real definitions are needed
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Programs with Multiple Files - A closer
look :Summary
72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utilities.h utilities.cpp
main.cpp
73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
source TU
object
Preprocessing Compilation
source TU
object
74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
object
Linking
object
75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Declarations and definitions
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One Definition Rule
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Linkage
• No linkage
• Internal Linkage
• External Linkage
• Module Linkage(later)
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Flipping Linkage
80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
some_other_file.cpp
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
utility1.cpp
utility2.cpp
83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static(anonymous
inline
namespaces)
84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Forward declarations
85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya