0% found this document useful (0 votes)
14 views

Section_MultipleFileProgramACloserLook_Slides

Uploaded by

Dheeraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Section_MultipleFileProgramACloserLook_Slides

Uploaded by

Dheeraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Slides

Section : Programs with


multiple files – A closer look
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

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!

g++ -o rooster.exe main.cpp utilities.cpp

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile only : generate object files

g++ -c main.cpp utilities.cpp

16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Link up object files

g++ -o rooster.exe main.o utilities.o

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 a name is never used ( function called, or variable read from/written to) in


main, it's definition won't be needed : code will compile just fine.

• 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

• Free standing variables


• Functions
• Classes
• Class member functions
• Class static member variables

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

• Function local variables have no linkage


• Const global variables have internal linkage
• Non const global variables have external linkage
• Functions have external linkage

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

Declaration introduces the name in a file, a definition says


what that name is or what it does.

77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One Definition Rule

Definitions can’t show up more than once in entire program,


or Translation units.

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

Inline variables and functions

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

You might also like