SlideShare a Scribd company logo
Richard Thomson
legalize@xmission.com
@LegalizeAdulthd
github.com/LegalizeAdulthood
Mechanics of Compilation
Source
File
Header
File#include
Object
File
Compile
Executable
FileLink
Who's Responsible?
 Header files
 Preprocessor
 Source files
 Compiler
 Object files
 Linker
 Executable files
 Run-time loader
Program
"The text of the program is kept in units called source files. A
source file together with all the headers and source files
included via the preprocessor directive #include, less any
lines skipped by conditional inclusion preprocessing
directives, is called a translation unit."
[2.1 lex.separate]
"A program consists of one or more translation units linked
together. A translation unit consists of a sequence of
declarations."
[3.5 basic.link]
Declarations and Definitions
"A declaration may introduce one or more names into a translation
unit or redeclare names introduced by previous declarations.
A declaration is a definition unless it declares a function without
specifying the function's body, it contains the extern specifier or
a linkage specification and neither an initializer nor a function
body, it declares a static data member in a class definition, it is a
class name declaration, it is an opaque enum declaration, it is a
template parameter, it is a parameter declaration in a function
declarator that is not a declarator of a function definition, or it is a
typedef declaration, an alias declaration, a using declaration, a
static assert declaration, an empty declaration, or a using
directive."
[3.1 basic.def]
(don't panic!)
Examples of Declarations
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares d
Examples of Definitions
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines non-static data member x
static int y; // declares static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up, down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
One Definition Rule
"No translation unit shall contain more than one
definition of any variable, function, class type,
enumeration type, or template. [...]
Every program shall contain exactly one definition of
every non-inline function or variable that is used in that
program. [...]
Exactly one definition of a class is required in a
translation unit if the class is used in a way that requires
the class to be complete. [...]"
[3.2 basic.def.odr]
One Definition Rule
"There can be more than one definition of a class type,
enumeration type, inline function with external linkage,
class template, non-static function template, static data
member of a class template, member function of a class
template, or template specialization for which some
template parameters are not specified in a program
provided that each definition appears in a different
translation unit and provided the definitions [are the
same]."
[3.2 basic.def.odr]
Linkage
"A name is said to have linkage when it might denote the
same object, reference, function, type, template, namespace
or value as a name introduced in another scope:
 When a name has external linkage, the entity it denotes
can be referred to by names from scopes of other
translation units or from other scopes of the same
translation unit.
 When a name has internal linkage, the entity it denotes
can be referred to by names from other scopes in the same
translation unit.
 When a name has no linkage, the entity it denotes cannot
be referred to by names from other scopes."
[3.5 basic.link]
The Preprocessor
 Processes preprocessor directives:
 #include, #define, #if, etc.
 Expands macro invocations with macro bodies
 Expansion proceeds recursively
 The result is a sequence of tokens for the compiler.
Header files act as a massive copy/paste of file contents
into your source files. Megabytes of copy/paste...
This is why your builds are slow.
Modules in C++ are the solution, but not in C++17 :-(
The Compiler
 Invokes the preprocessor on source files for you.
 Receives a stream of tokens from the preprocessor.
 Performs syntactic and semantic analysis on the token
stream to produce a sequence of declarations.
 Some of those declarations are definitions.
 Definitions allocate space for data or instructions.
 The output of compilation is an object file.
 The object file encodes the output of compilation:
 definitions provided by the translation unit.
 references to declarations with external linkage.
The Linker
 Accepts object files from compilation.
 Resolves declarations with external linkage.
 Produces an executable file for the run-time loader.
The Run-Time Loader
 Accepts executables from linking.
 Establishes a new process for the executable.
 Performs any dynamic linking necessary for the executable:
 locates dynamic library dependencies
 resolves imported symbols by binding them from the
dynamic library exporting the symbols
 Transfers execution control to the new process.
Symbols and Declarations
 A symbol is a name used to communicate declarations between the compiler,
the linker and the run-time loader.
 Every declaration with external linkage results in a unique symbol.
 The process of encoding each C++ identifier into a unique symbol is
implementation dependent and is called "name mangling".
 Declarations can be overloaded, located within a namespace, const/volatile
qualified, etc., all of which can change the mangled name.
 The linker and run-time loader deal exclusively with mangled symbols.
 Name mangling can be suppressed with C linkage (extern "C").
 Interoperability with other languages is achieved through C linkage.
What is a Library?
 Declares facilities for use by an application.
 Defines the implementation for use by an application.
 Good libraries define good abstractions.
What is a Header Only Library?
 Provided as one or more header files.
 The declarations and definitions are in the headers.
 No linking necessary to use the library.
 Just tell your compiler where to find the files.
 Often referred to as a compile-time dependency of an
executable.
Declarations and Definitions?!?
 Function definitions are declared inline.
 Their definitions are identical for each source file
including the header.
 Therefore they are allowed by the ODR.
What is a Static Library?
 An archive of object files.
 Produced by the linker/archiver from a set of object files.
 May contain indices to accelerate resolution of declarations
with external linkage.
 When supplied to the linker, a static library acts as if all of
its constituent object files were supplied individually to the
linker.
 Often referred to as a static dependency of an executable.
Mechanics of Static Libraries
Object
File
Object
File
Static Library
Link
Object
File
Object
File
Dependencies of Static Libraries
 Source code of a static library can depend on facilities
provided by another static library or another dynamic
library.
 This creates implicit library dependencies for clients of this
static library.
 These dependencies accumulate transitively until they are
resolved by linking an executable file.
 CMake can handle this dependency chain automatically.
Consuming a Static Library
 Object files provide definitions, but not declarations.
 Header files provide declarations, but not definitions.
 Consuming a static library consists of:
 supplying the header files to the compiler
 supplying the library to the linker
Creating a Static Library
 Create header files containing declarations.
 Create source files containing definitions for the
declarations.
 Compile all source files to be included in the library.
 Link the resulting object files into a static library.
 Make the headers and the library available to clients.
What is a Dynamic Library?
 Also called a shared object or shared library.
 Instructions and constant data are shared by all running clients.
 Produced by the linker from a collection of object files, static libraries and
dynamic libraries.
 Encoded as an executable file for use by the run-time loader with:
 A table of exported names provided by this library.
 A table of imported names consumed by this library from other dynamic
libraries.
 Supplied to the linker as an input to resolve names in an executable and mark
them as supplied by the dynamic library.
 Some of the mechanics of shared libraries differ between OS platforms.
Mechanics of Dynamic Libraries
Static
Library
Dynamic
Library
Dynamic
Library
Link
Object
File
Object
File
Dependencies of Dynamic Libraries
 Source code of a dynamic library can depend on facilities
provided by another static library or another dynamic
library.
 This creates implicit dynamic library dependencies for
clients of this dynamic library.
 Static dependencies are resolved when linking the dynamic
library.
 CMake can handle this dependency chain automatically.
Consuming a Dynamic Library
 Dynamic library provides definitions to the run-time
loader.
 Header files provide declarations for the definitions.
 Consuming a dynamic library consists of:
 supplying the header files to the compiler
 supplying the dynamic library to the linker
 supplying the dynamic library and its dynamic library
dependencies to the run-time loader
Creating a Dynamic Library
 Create header files containing declarations.
 Create source files containing definitions for the
declarations.
 Compile all source files to be included in the library.
 Link the resulting object files into a dynamic library.
 Make the headers and the library available to clients.
Principles of Library Design*
 REP: The Release-Reuse Equivalency Principle
 CCP: The Common Closure Principle
 CRP: The Common Reuse Principle
 ADP: The Acyclic Dependencies Principle
 SDP: The Stable Dependencies Principle
 SAP: The Stable Abstraction Principle
* From "Agile Principles, Patterns and Practices in C#" by Robert Martin and Micah Martin
REP: The Release-Reuse
Equivalency Principle
 The granule of reuse is the granule of release.
 The granule of reuse, a library, can be no smaller than
the granule of release. Anything that we reuse must
also be released and tracked.
 Either all the classes in a library are reusable or none of
them are.
CCP: The Common
Closure Principle
 The classes in a library should be closed together
against the same kinds of changes. A change that
affects a library affects all the classes in that library
and no other libraries.
 This is the single responsibility principle (SRP) applied
to libraries.
 CCP gathers together in one place all the classes that
are likely to change for the same reasons.
CRP: The Common Reuse Principle
 The classes in a library are reused together. If you
reuse one of the classes in a library, you reuse them all.
 Classes that are tightly coupled to each other should
be in the same library.
 Classes that are not tightly bound to each other with
class relationships should not be in the same library.
ADP: The Acyclic
Dependencies Principle
 Allow no cycles in the library dependency graph.
 Cycles manifest themselves in link command-lines for
executables where libraries are repeated as link inputs.
 Break cycles by introducing new libraries containing
classes on which other libraries depend.
SDP: The Stable
Dependencies Principle
 Depend in the direction of stability.
 Changes in dependencies cause testing and
integration to propagate up the dependency chain.
SAP: The Stable
Abstractions Principle
 A library should be as abstract as it is stable.
 A stable library should be abstract so that its stability
does not prevent it from being extended.
 An instable library should be concrete, since its
instability allows the code within it to be easily
changed.
Principles for Header Libraries
 REP: the granularity of release is the header file.
 CCP: the same kind of change should affect all the classes
in the header.
 CRP: the classes in a header-only library should all be
reused together.
 ADP: there should be no preprocessor inclusion cycles.
 SDP: the header should only depend on more stable
headers.
 SAP: the more a header is included by other headers and
source files, the more abstract it should be.
Principles for Static Libraries
 REP: the granularity of release is the library and all of its
header files.
 CCP: the same kind of change should affect all the classes
in the library.
 CRP: all the classes in the library are used together.
 ADP: there are no link cycles in the library dependency
graph.
 SDP: the library should depend only on more stable
libraries.
 SAP: the more stable the library is, the more abstract its
header interface should be.
Principles for Dynamic Libraries
 REP: the granularity of release is the library and all of its
header files.
 CCP: the same kind of change should affect all the classes
in the library.
 CRP: all the classes in the library are used together.
 ADP: there are no link cycles in the library dependency
graph or in the run-time dependency graph.
 SDP: the library should depend only on more stable
libraries, either static or dynamic.
 SAP: the more stable the library is, the more abstract its
header interface should be.
Abstract Interfaces in C++
 Template classes/functions use static polymorphism to
express abstract relationships (concepts) between
themselves and their template parameters.
 Concepts can simplify the syntax and directly express
the abstract relationships, but not in C++17 :-(
 Clients implement concepts, leaving the
implementation free to change without inducing
change on the clients.
Abstract Interfaces in C++
 Static and dynamic libraries can use dynamic
polymorphism (virtual functions) to express abstract
relationships between their classes and their clients.
 Publish interfaces as pure virtual base classes
implemented by the library classes.
 Clients depend on interfaces, leaving the
implementation free to change without inducing
change in clients.
Thank You!
Bonus Material!
 Order of initialization of global data
 Global data in static dependencies of dynamic libraries
Order of Initialization
"Non-local variables with static storage duration are
initialized as a consequence of program initiation.
Non-local variables with thread storage duration are
initialized as a consequence of thread [initiation]."
[3.6.2 basic.start.init]
Static Initialization
"Variables with static storage duration or thread storage
duration shall be zero-initialized before any other
initialization takes place.
Constant initialization is performed [if the initializing
expression is constant]. [...]
Together, zero-initialization and constant initialization are
called static-initialization; all other initialization is dynamic
initialization.
Static initialization shall be performed before any dynamic
initialization takes place."
[3.6.2 basic.start.init]
Dynamic Initialization
"Dynamic initialization of a non-local variable with static
storage duration is either ordered or unordered. [...]
Variables with ordered initialization defined within a single
translation unit shall be initialized in the order of their
definitions within a translation unit.
[The] initialization of a variable is indeterminately sequenced
with respect to the initialization of a variable defined in a
different translation unit.
It is implementation-defined whether the dynamic
initialization of a non-local variable with static storage
duration is done before the first statement of main."
[3.6.2 basic.start.init]
Example of Indeterminate Ordering
// g.cpp
extern int f(int);
static int g = f(1);
// is g 2 or 3?
// h.cpp
extern int f(int);
static int h = f(2);
// is h 3 or 4?
// f.cpp
int f(int x) {
static int val = 0;
return ++val + x;
}
Linking Dynamic Libraries
Against Static Libraries
Dynamic
Library 1
Link
Object
File 1
Static
Library 1
Dynamic
Library 2
Link
Object
File 2
Static
Library 1
Intentions Subverted
 Static Library 1 intended that it should have a single copy of its
global data in any program.
 Dynamic Library 1 has an instance of Static Library 1's global
data.
 Dynamic Library 2 has an instance of Static Library 1's global
data.
 Any executable using both dynamic libraries has two copies of
Static Library 1's global data.
 Now you have mysterious bugs....
Intentions Upheld
 Use Static Library 1 as a dynamic library instead.
 Use Dynamic Library 1 and Dynamic Library 2 as static
libraries instead and static dependencies linked once
into executable.
Using dynamic libraries has a tendency to induce the
requirement of dynamic libraries elsewhere.
Simplest solution:
 all libraries static
 all libraries dynamic

More Related Content

What's hot

STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
fawzmasood
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
Sayed Ahmed
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
Abishek Purushothaman
 
Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
Arpana Awasthi
 
C
CC
Unit v
Unit vUnit v
Pointers
PointersPointers
Java8
Java8Java8
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 
C
CC
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
Federico Ficarelli
 
C tutorial
C tutorialC tutorial
C tutorial
Khan Rahimeen
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
Sasha Goldshtein
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
John(Qiang) Zhang
 

What's hot (16)

STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Python libraries
Python librariesPython libraries
Python libraries
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
 
C
CC
C
 
Unit v
Unit vUnit v
Unit v
 
Pointers
PointersPointers
Pointers
 
Java8
Java8Java8
Java8
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
 
C
CC
C
 
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
 
C tutorial
C tutorialC tutorial
C tutorial
 
basics of c++
basics of c++basics of c++
basics of c++
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
 

Viewers also liked

Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
MOHIT TOMAR
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
Frankie Jones
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
Junyoung Jung
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
pratikborsadiya
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Sachin Sharma
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation India
Marion Welch
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
webhostingguy
 
Introduction to MySQL
Introduction to MySQLIntroduction to MySQL
Introduction to MySQL
Giuseppe Maxia
 
Steve Jobs Inspirational Quotes
Steve Jobs Inspirational QuotesSteve Jobs Inspirational Quotes
Steve Jobs Inspirational Quotes
InsideView
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
Deep C
Deep CDeep C
Deep C
Olve Maudal
 

Viewers also liked (12)

Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Custom Android App Development – Web Animation India
Custom Android App Development – Web Animation IndiaCustom Android App Development – Web Animation India
Custom Android App Development – Web Animation India
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Introduction to MySQL
Introduction to MySQLIntroduction to MySQL
Introduction to MySQL
 
Steve Jobs Inspirational Quotes
Steve Jobs Inspirational QuotesSteve Jobs Inspirational Quotes
Steve Jobs Inspirational Quotes
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Deep C
Deep CDeep C
Deep C
 

Similar to Consuming and Creating Libraries in C++

7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
abdulhaq467432
 
C++ shared libraries and loading
C++ shared libraries and loadingC++ shared libraries and loading
C++ shared libraries and loading
Rahul Jamwal
 
Linkers
LinkersLinkers
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
kunj desai
 
Linkers in compiler
Linkers in compilerLinkers in compiler
Linkers in compiler
Asaad Al-nour
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
yogita kachve
 
Build process ppt.pptx
Build process ppt.pptxBuild process ppt.pptx
Build process ppt.pptx
SHIVANISRECECE
 
Answer ado.net pre-exam2018
Answer ado.net pre-exam2018Answer ado.net pre-exam2018
Answer ado.net pre-exam2018
than sare
 
Enterprise Library 2.0 Core Architecture
Enterprise Library 2.0 Core ArchitectureEnterprise Library 2.0 Core Architecture
Enterprise Library 2.0 Core Architecture
mcgurk
 
THE CLR AND THE .NET FRAMEWORK, C#
THE CLR AND THE .NET FRAMEWORK, C#THE CLR AND THE .NET FRAMEWORK, C#
THE CLR AND THE .NET FRAMEWORK, C#
MANOJ BURI
 
SPRING 1. overview
SPRING 1. overviewSPRING 1. overview
SPRING 1. overview
Francesco Ierna
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 
Subversion
SubversionSubversion
Linkers
LinkersLinkers
Linkers
Rahul Dhiman
 
ELF
ELFELF
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
Malek Sumaiya
 
Lightning web components
Lightning web components Lightning web components
Lightning web components
Cloud Analogy
 
Linkers And Loaders
Linkers And LoadersLinkers And Loaders
Linkers And Loaders
Satpal Parmar
 
Hibernate.pdf
Hibernate.pdfHibernate.pdf
Hibernate.pdf
SaadAnsari73
 
C# Unit 1 notes
C# Unit 1 notesC# Unit 1 notes
C# Unit 1 notes
Sudarshan Dhondaley
 

Similar to Consuming and Creating Libraries in C++ (20)

7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
 
C++ shared libraries and loading
C++ shared libraries and loadingC++ shared libraries and loading
C++ shared libraries and loading
 
Linkers
LinkersLinkers
Linkers
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
 
Linkers in compiler
Linkers in compilerLinkers in compiler
Linkers in compiler
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
Build process ppt.pptx
Build process ppt.pptxBuild process ppt.pptx
Build process ppt.pptx
 
Answer ado.net pre-exam2018
Answer ado.net pre-exam2018Answer ado.net pre-exam2018
Answer ado.net pre-exam2018
 
Enterprise Library 2.0 Core Architecture
Enterprise Library 2.0 Core ArchitectureEnterprise Library 2.0 Core Architecture
Enterprise Library 2.0 Core Architecture
 
THE CLR AND THE .NET FRAMEWORK, C#
THE CLR AND THE .NET FRAMEWORK, C#THE CLR AND THE .NET FRAMEWORK, C#
THE CLR AND THE .NET FRAMEWORK, C#
 
SPRING 1. overview
SPRING 1. overviewSPRING 1. overview
SPRING 1. overview
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 
Subversion
SubversionSubversion
Subversion
 
Linkers
LinkersLinkers
Linkers
 
ELF
ELFELF
ELF
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Lightning web components
Lightning web components Lightning web components
Lightning web components
 
Linkers And Loaders
Linkers And LoadersLinkers And Loaders
Linkers And Loaders
 
Hibernate.pdf
Hibernate.pdfHibernate.pdf
Hibernate.pdf
 
C# Unit 1 notes
C# Unit 1 notesC# Unit 1 notes
C# Unit 1 notes
 

More from Richard Thomson

Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdfVintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
Richard Thomson
 
Automated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDashAutomated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDash
Richard Thomson
 
Feature and platform testing with CMake
Feature and platform testing with CMakeFeature and platform testing with CMake
Feature and platform testing with CMake
Richard Thomson
 
Consuming Libraries with CMake
Consuming Libraries with CMakeConsuming Libraries with CMake
Consuming Libraries with CMake
Richard Thomson
 
BEFLIX
BEFLIXBEFLIX
SIMD Processing Using Compiler Intrinsics
SIMD Processing Using Compiler IntrinsicsSIMD Processing Using Compiler Intrinsics
SIMD Processing Using Compiler Intrinsics
Richard Thomson
 
Cross Platform Mobile Development with Visual Studio 2015 and C++
Cross Platform Mobile Development with Visual Studio 2015 and C++Cross Platform Mobile Development with Visual Studio 2015 and C++
Cross Platform Mobile Development with Visual Studio 2015 and C++
Richard Thomson
 
Web mashups with NodeJS
Web mashups with NodeJSWeb mashups with NodeJS
Web mashups with NodeJS
Richard Thomson
 

More from Richard Thomson (8)

Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdfVintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
Vintage Computing Festival Midwest 18 2023-09-09 What's In A Terminal.pdf
 
Automated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDashAutomated Testing with CMake, CTest and CDash
Automated Testing with CMake, CTest and CDash
 
Feature and platform testing with CMake
Feature and platform testing with CMakeFeature and platform testing with CMake
Feature and platform testing with CMake
 
Consuming Libraries with CMake
Consuming Libraries with CMakeConsuming Libraries with CMake
Consuming Libraries with CMake
 
BEFLIX
BEFLIXBEFLIX
BEFLIX
 
SIMD Processing Using Compiler Intrinsics
SIMD Processing Using Compiler IntrinsicsSIMD Processing Using Compiler Intrinsics
SIMD Processing Using Compiler Intrinsics
 
Cross Platform Mobile Development with Visual Studio 2015 and C++
Cross Platform Mobile Development with Visual Studio 2015 and C++Cross Platform Mobile Development with Visual Studio 2015 and C++
Cross Platform Mobile Development with Visual Studio 2015 and C++
 
Web mashups with NodeJS
Web mashups with NodeJSWeb mashups with NodeJS
Web mashups with NodeJS
 

Recently uploaded

DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
sandeepmenon62
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
alowpalsadig
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 

Recently uploaded (20)

DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 

Consuming and Creating Libraries in C++

  • 3. Who's Responsible?  Header files  Preprocessor  Source files  Compiler  Object files  Linker  Executable files  Run-time loader
  • 4. Program "The text of the program is kept in units called source files. A source file together with all the headers and source files included via the preprocessor directive #include, less any lines skipped by conditional inclusion preprocessing directives, is called a translation unit." [2.1 lex.separate] "A program consists of one or more translation units linked together. A translation unit consists of a sequence of declarations." [3.5 basic.link]
  • 5. Declarations and Definitions "A declaration may introduce one or more names into a translation unit or redeclare names introduced by previous declarations. A declaration is a definition unless it declares a function without specifying the function's body, it contains the extern specifier or a linkage specification and neither an initializer nor a function body, it declares a static data member in a class definition, it is a class name declaration, it is an opaque enum declaration, it is a template parameter, it is a parameter declaration in a function declarator that is not a declarator of a function definition, or it is a typedef declaration, an alias declaration, a using declaration, a static assert declaration, an empty declaration, or a using directive." [3.1 basic.def]
  • 7. Examples of Declarations extern int a; // declares a extern const int c; // declares c int f(int); // declares f struct S; // declares S typedef int Int; // declares Int extern X anotherX; // declares anotherX using N::d; // declares d
  • 8. Examples of Definitions int a; // defines a extern const int c = 1; // defines c int f(int x) { return x+a; } // defines f and defines x struct S { int a; int b; }; // defines S, S::a, and S::b struct X { // defines X int x; // defines non-static data member x static int y; // declares static data member y X(): x(0) { } // defines a constructor of X }; int X::y = 1; // defines X::y enum { up, down }; // defines up and down namespace N { int d; } // defines N and N::d namespace N1 = N; // defines N1 X anX; // defines anX
  • 9. One Definition Rule "No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template. [...] Every program shall contain exactly one definition of every non-inline function or variable that is used in that program. [...] Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class to be complete. [...]" [3.2 basic.def.odr]
  • 10. One Definition Rule "There can be more than one definition of a class type, enumeration type, inline function with external linkage, class template, non-static function template, static data member of a class template, member function of a class template, or template specialization for which some template parameters are not specified in a program provided that each definition appears in a different translation unit and provided the definitions [are the same]." [3.2 basic.def.odr]
  • 11. Linkage "A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced in another scope:  When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.  When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit.  When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes." [3.5 basic.link]
  • 12. The Preprocessor  Processes preprocessor directives:  #include, #define, #if, etc.  Expands macro invocations with macro bodies  Expansion proceeds recursively  The result is a sequence of tokens for the compiler. Header files act as a massive copy/paste of file contents into your source files. Megabytes of copy/paste... This is why your builds are slow. Modules in C++ are the solution, but not in C++17 :-(
  • 13. The Compiler  Invokes the preprocessor on source files for you.  Receives a stream of tokens from the preprocessor.  Performs syntactic and semantic analysis on the token stream to produce a sequence of declarations.  Some of those declarations are definitions.  Definitions allocate space for data or instructions.  The output of compilation is an object file.  The object file encodes the output of compilation:  definitions provided by the translation unit.  references to declarations with external linkage.
  • 14. The Linker  Accepts object files from compilation.  Resolves declarations with external linkage.  Produces an executable file for the run-time loader.
  • 15. The Run-Time Loader  Accepts executables from linking.  Establishes a new process for the executable.  Performs any dynamic linking necessary for the executable:  locates dynamic library dependencies  resolves imported symbols by binding them from the dynamic library exporting the symbols  Transfers execution control to the new process.
  • 16. Symbols and Declarations  A symbol is a name used to communicate declarations between the compiler, the linker and the run-time loader.  Every declaration with external linkage results in a unique symbol.  The process of encoding each C++ identifier into a unique symbol is implementation dependent and is called "name mangling".  Declarations can be overloaded, located within a namespace, const/volatile qualified, etc., all of which can change the mangled name.  The linker and run-time loader deal exclusively with mangled symbols.  Name mangling can be suppressed with C linkage (extern "C").  Interoperability with other languages is achieved through C linkage.
  • 17. What is a Library?  Declares facilities for use by an application.  Defines the implementation for use by an application.  Good libraries define good abstractions.
  • 18. What is a Header Only Library?  Provided as one or more header files.  The declarations and definitions are in the headers.  No linking necessary to use the library.  Just tell your compiler where to find the files.  Often referred to as a compile-time dependency of an executable.
  • 19. Declarations and Definitions?!?  Function definitions are declared inline.  Their definitions are identical for each source file including the header.  Therefore they are allowed by the ODR.
  • 20. What is a Static Library?  An archive of object files.  Produced by the linker/archiver from a set of object files.  May contain indices to accelerate resolution of declarations with external linkage.  When supplied to the linker, a static library acts as if all of its constituent object files were supplied individually to the linker.  Often referred to as a static dependency of an executable.
  • 21. Mechanics of Static Libraries Object File Object File Static Library Link Object File Object File
  • 22. Dependencies of Static Libraries  Source code of a static library can depend on facilities provided by another static library or another dynamic library.  This creates implicit library dependencies for clients of this static library.  These dependencies accumulate transitively until they are resolved by linking an executable file.  CMake can handle this dependency chain automatically.
  • 23. Consuming a Static Library  Object files provide definitions, but not declarations.  Header files provide declarations, but not definitions.  Consuming a static library consists of:  supplying the header files to the compiler  supplying the library to the linker
  • 24. Creating a Static Library  Create header files containing declarations.  Create source files containing definitions for the declarations.  Compile all source files to be included in the library.  Link the resulting object files into a static library.  Make the headers and the library available to clients.
  • 25. What is a Dynamic Library?  Also called a shared object or shared library.  Instructions and constant data are shared by all running clients.  Produced by the linker from a collection of object files, static libraries and dynamic libraries.  Encoded as an executable file for use by the run-time loader with:  A table of exported names provided by this library.  A table of imported names consumed by this library from other dynamic libraries.  Supplied to the linker as an input to resolve names in an executable and mark them as supplied by the dynamic library.  Some of the mechanics of shared libraries differ between OS platforms.
  • 26. Mechanics of Dynamic Libraries Static Library Dynamic Library Dynamic Library Link Object File Object File
  • 27. Dependencies of Dynamic Libraries  Source code of a dynamic library can depend on facilities provided by another static library or another dynamic library.  This creates implicit dynamic library dependencies for clients of this dynamic library.  Static dependencies are resolved when linking the dynamic library.  CMake can handle this dependency chain automatically.
  • 28. Consuming a Dynamic Library  Dynamic library provides definitions to the run-time loader.  Header files provide declarations for the definitions.  Consuming a dynamic library consists of:  supplying the header files to the compiler  supplying the dynamic library to the linker  supplying the dynamic library and its dynamic library dependencies to the run-time loader
  • 29. Creating a Dynamic Library  Create header files containing declarations.  Create source files containing definitions for the declarations.  Compile all source files to be included in the library.  Link the resulting object files into a dynamic library.  Make the headers and the library available to clients.
  • 30. Principles of Library Design*  REP: The Release-Reuse Equivalency Principle  CCP: The Common Closure Principle  CRP: The Common Reuse Principle  ADP: The Acyclic Dependencies Principle  SDP: The Stable Dependencies Principle  SAP: The Stable Abstraction Principle * From "Agile Principles, Patterns and Practices in C#" by Robert Martin and Micah Martin
  • 31. REP: The Release-Reuse Equivalency Principle  The granule of reuse is the granule of release.  The granule of reuse, a library, can be no smaller than the granule of release. Anything that we reuse must also be released and tracked.  Either all the classes in a library are reusable or none of them are.
  • 32. CCP: The Common Closure Principle  The classes in a library should be closed together against the same kinds of changes. A change that affects a library affects all the classes in that library and no other libraries.  This is the single responsibility principle (SRP) applied to libraries.  CCP gathers together in one place all the classes that are likely to change for the same reasons.
  • 33. CRP: The Common Reuse Principle  The classes in a library are reused together. If you reuse one of the classes in a library, you reuse them all.  Classes that are tightly coupled to each other should be in the same library.  Classes that are not tightly bound to each other with class relationships should not be in the same library.
  • 34. ADP: The Acyclic Dependencies Principle  Allow no cycles in the library dependency graph.  Cycles manifest themselves in link command-lines for executables where libraries are repeated as link inputs.  Break cycles by introducing new libraries containing classes on which other libraries depend.
  • 35. SDP: The Stable Dependencies Principle  Depend in the direction of stability.  Changes in dependencies cause testing and integration to propagate up the dependency chain.
  • 36. SAP: The Stable Abstractions Principle  A library should be as abstract as it is stable.  A stable library should be abstract so that its stability does not prevent it from being extended.  An instable library should be concrete, since its instability allows the code within it to be easily changed.
  • 37. Principles for Header Libraries  REP: the granularity of release is the header file.  CCP: the same kind of change should affect all the classes in the header.  CRP: the classes in a header-only library should all be reused together.  ADP: there should be no preprocessor inclusion cycles.  SDP: the header should only depend on more stable headers.  SAP: the more a header is included by other headers and source files, the more abstract it should be.
  • 38. Principles for Static Libraries  REP: the granularity of release is the library and all of its header files.  CCP: the same kind of change should affect all the classes in the library.  CRP: all the classes in the library are used together.  ADP: there are no link cycles in the library dependency graph.  SDP: the library should depend only on more stable libraries.  SAP: the more stable the library is, the more abstract its header interface should be.
  • 39. Principles for Dynamic Libraries  REP: the granularity of release is the library and all of its header files.  CCP: the same kind of change should affect all the classes in the library.  CRP: all the classes in the library are used together.  ADP: there are no link cycles in the library dependency graph or in the run-time dependency graph.  SDP: the library should depend only on more stable libraries, either static or dynamic.  SAP: the more stable the library is, the more abstract its header interface should be.
  • 40. Abstract Interfaces in C++  Template classes/functions use static polymorphism to express abstract relationships (concepts) between themselves and their template parameters.  Concepts can simplify the syntax and directly express the abstract relationships, but not in C++17 :-(  Clients implement concepts, leaving the implementation free to change without inducing change on the clients.
  • 41. Abstract Interfaces in C++  Static and dynamic libraries can use dynamic polymorphism (virtual functions) to express abstract relationships between their classes and their clients.  Publish interfaces as pure virtual base classes implemented by the library classes.  Clients depend on interfaces, leaving the implementation free to change without inducing change in clients.
  • 43. Bonus Material!  Order of initialization of global data  Global data in static dependencies of dynamic libraries
  • 44. Order of Initialization "Non-local variables with static storage duration are initialized as a consequence of program initiation. Non-local variables with thread storage duration are initialized as a consequence of thread [initiation]." [3.6.2 basic.start.init]
  • 45. Static Initialization "Variables with static storage duration or thread storage duration shall be zero-initialized before any other initialization takes place. Constant initialization is performed [if the initializing expression is constant]. [...] Together, zero-initialization and constant initialization are called static-initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place." [3.6.2 basic.start.init]
  • 46. Dynamic Initialization "Dynamic initialization of a non-local variable with static storage duration is either ordered or unordered. [...] Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions within a translation unit. [The] initialization of a variable is indeterminately sequenced with respect to the initialization of a variable defined in a different translation unit. It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main." [3.6.2 basic.start.init]
  • 47. Example of Indeterminate Ordering // g.cpp extern int f(int); static int g = f(1); // is g 2 or 3? // h.cpp extern int f(int); static int h = f(2); // is h 3 or 4? // f.cpp int f(int x) { static int val = 0; return ++val + x; }
  • 48. Linking Dynamic Libraries Against Static Libraries Dynamic Library 1 Link Object File 1 Static Library 1 Dynamic Library 2 Link Object File 2 Static Library 1
  • 49. Intentions Subverted  Static Library 1 intended that it should have a single copy of its global data in any program.  Dynamic Library 1 has an instance of Static Library 1's global data.  Dynamic Library 2 has an instance of Static Library 1's global data.  Any executable using both dynamic libraries has two copies of Static Library 1's global data.  Now you have mysterious bugs....
  • 50. Intentions Upheld  Use Static Library 1 as a dynamic library instead.  Use Dynamic Library 1 and Dynamic Library 2 as static libraries instead and static dependencies linked once into executable. Using dynamic libraries has a tendency to induce the requirement of dynamic libraries elsewhere. Simplest solution:  all libraries static  all libraries dynamic