37,99 €
Design and architect real-world scalable C++ applications by exploring advanced techniques in low-level programming, object-oriented programming (OOP), the Standard Template Library (STL), metaprogramming, and concurrency
Key Features
Book Description
C++ has evolved over the years and the latest release – C++20 – is now available. Since C++11, C++ has been constantly enhancing the language feature set. With the new version, you'll explore an array of features such as concepts, modules, ranges, and coroutines. This book will be your guide to learning the intricacies of the language, techniques, C++ tools, and the new features introduced in C++20, while also helping you apply these when building modern and resilient software.
You'll start by exploring the latest features of C++, and then move on to advanced techniques such as multithreading, concurrency, debugging, monitoring, and high-performance programming. The book will delve into object-oriented programming principles and the C++ Standard Template Library, and even show you how to create custom templates. After this, you'll learn about different approaches such as test-driven development (TDD), behavior-driven development (BDD), and domain-driven design (DDD), before taking a look at the coding best practices and design patterns essential for building professional-grade applications. Toward the end of the book, you will gain useful insights into the recent C++ advancements in AI and machine learning.
By the end of this C++ programming book, you'll have gained expertise in real-world application development, including the process of designing complex software.
What you will learn
Who this book is for
This C++ book is for experienced C++ developers who are looking to take their knowledge to the next level and perfect their skills in building professional-grade applications.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 726
Veröffentlichungsjahr: 2020
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Richa TripathiAcquisition Editor: Denim PintoContent Development Editor: Pathikrit RoySenior Editor: Rohit SinghTechnical Editor: Romy DiasCopy Editor: Safis EditingProject Coordinator:Francy PuthiryProofreader: Safis EditingIndexer:Tejal Daruwale SoniProduction Designer: Alishon Mendonsa
First published: April 2020
Production reference: 1100420
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-83855-265-7
www.packt.com
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems.
Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
Lou Mauget learned to program long ago at Michigan State University as a physics major, learning to use software in designing a cyclotron. Afterward, he worked for 34 years at IBM. He is currently consulting for Keyhole Software of Leawood, Kansas. Lou has coded in C++, Java, JavaScript, Python, and newer languages, as each was conceived. Current interests include reactive functional programming, containers, Node.js, NoSQL, geospatial systems, mobile, and any new language or framework. He has coauthored three computer books. He has written two IBM DeveloperWorks XML tutorials and co-written several J2EE certification tests for IBM. He has been a reviewer for Packt Publishing and others.
Scott Hutchinson leads a team of C++ and F# developers in Oxnard, California. After a few years as a VB/VBA developer, he started developing with .NET Framework immediately after its launch in 2002. Since 2016, he has done most of his development in C++. He is a mentor for the F# track on Exercism, and teaches functional programming in F# to his team at work. His main professional interests are functional programming and machine learning. When he's not learning some new software development skill, he's usually hiking in the mountains of Southern California.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Expert C++
Dedication
About Packt
Why subscribe?
Contributors
About the authors
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Section 1: Under the Hood of C++ Programming
Introduction to Building C++ Applications
Technical requirements
Introduction to C++20
Concepts
Coroutines
Ranges
More C++20 features
Building and running programs
Understanding preprocessing
Header files
Using modules in C++20
Understanding Compiling
Tokenization
Syntax analysis
Semantic analysis
Intermediate code generation
Optimization
Machine code generation
Platforms and object files
Introducing Linking
Linking libraries
Summary
Questions
Further reading
Low-Level Programming with C++
Technical requirements
Program execution
main()
Special properties of main()
constexpr
Recursion
Working with data
Virtual memory
Addressing
Data types
Pointers
Memory segments
The heap
Arrays
Control flow
Conditionals
The switch statement
Replacing conditionals with function pointers
Functions as types
Loops
Summary
Questions
Further reading
Details of Object-Oriented Programming
Technical requirements
Understanding objects
Low-level details of objects
High-level details of objects
State
Identity
Behavior
Mimicking a class
Working with classes
Classes from the compiler perspective
Initialization and destruction
Copying objects
Moving objects
Lvalue references
Rvalue references
Notes on operator overloading
Encapsulation and the public interface
Structs in C++
Class relationships
Aggregation and composition
Inheritance
Inheritance from the compiler perspective
Composition versus inheritance
Protected inheritance
Polymorphism
Virtual functions under the hood
Design patterns
Summary
Questions
Further reading
Understanding and Designing Templates
Technical requirements
Exploring function and class templates
Motivation
Function templates
Syntax
Instantiation
Explicit instantiations
Implicit instantiations
Deduction
Specialization and overloading
Class templates
Syntax
Instantiation
Explicit instantiation
Implicit instantiation
Specialization
Understanding variadic templates
Syntax
Examples
Exploring template parameters and arguments
Template parameters
Non-type template parameter
Type template parameter
Template template parameter
Template arguments
Template non-type arguments
Template type arguments
Template template arguments
Default template arguments
Exploring traits
Type trait implementation
boost::is_void
boost::is_pointer
Optimizing algorithms using traits
Exploring template metaprogramming
Compile-time computation
Compile-time code optimization
Static polymorphism
Summary
Questions
Further reading
Memory Management and Smart Pointers
Technical requirements
Understanding computer memory
Designing a memory storage device
Understanding computer memory from a higher-level perspective
Registers
Cache memory
Main memory
Permanent storage
The basics of memory management
An example of memory management
Using smart pointers
Leveraging the RAII idiom
std::unique_ptr
std::shared_ptr and std::weak_ptr
Garbage collection
Using allocators
Summary
Questions
Further reading
Section 2: Designing Robust and Efficient Applications
Digging into Data Structures and Algorithms in STL
Technical requirements
Data structures
Sequential data structures
Node-based data structures
Containers in memory
STL containers
Using std::vector and std::list
Using container adapters
Iterating containers
Concepts and iterators
Understanding concepts 
Using iterators in C++20
Mastering algorithms
Searching
Binary search
Sorting
Exploring trees and graphs
Hash tables
Graphs
Strings
Summary
Questions
Further reading
Functional Programming
Technical requirements
Unveiling functional programming
Using ranges
Why use functional programming?
Principles of functional programming
Pure functions
Higher-order functions
Folding
Diving deeper into recursion
Head recursion
Tail recursion
Metaprogramming in functional C++
Summary
Questions
Further reading
Concurrency and Multithreading
Technical requirements
Understanding concurrency and multithreading
Processes
Challenges with processes
Threads
Working with threads
Waiting for threads
Using std::jthread
Passing arguments to the thread function
Managing threads and sharing data
Sharing data
Protecting shared data using a mutex
Avoiding deadlocks
Designing concurrent code
Introducing coroutines
Summary
Questions
Further reading
Designing Concurrent Data Structures
Technical requirements
A closer look at data races
A synchronized increment
Implementing a thread-safe stack
Designing lock-free data structures
Using atomic types
Operations on atomic types
Designing a lock-free stack
More operations on atomics
Summary
Questions
Further reading
Designing World-Ready Applications
Technical requirements
Project development life cycle
Requirements gathering and analysis
Specification creation
Design and test planning
Decomposing entities
Coding
Testing and stabilization
Release and maintenance
Diving into the design process
Using SOLID principles
The single responsibility principle
The open-closed principle
The Liskov substitution principle
The interface segregation principle
The dependency inversion principle
Using domain-driven design
Leveraging design patterns
The repository pattern
The factory pattern
Summary
Questions
Further reading
Designing a Strategy Game Using Design Patterns
Technical requirements
Introduction to game design
Introduction to the Readers and Disturbers game
Strategy game components
Interaction between the components
Designing the game
Designing character units
Designing buildings
Designing game controllers
Concurrent actions
The game event loop
Using design patterns
The Command pattern
The Observer pattern
The Flyweight pattern
The Prototype pattern
Designing the game loop
Summary
Questions
Further reading
Networking and Security
Technical requirements
Discovering network programming in C++
Network applications under the hood
Programming network applications using sockets
Network protocols
Designing a network application
Using POSIX sockets
Implementing a POSIX socket wrapper class
Securing the C++ code
Securing network applications
Summary
Questions
Further reading
Debugging and Testing
Technical requirements
Understanding the root cause of an issue
The RCA overview
Prevention is better than the cure – a good coding behavior
Uninitialized variable problem
Side effects in compound expressions
Mixing signed and unsigned problems
Order of evaluation problem
Compile-time checking versus runtime checking
Avoiding memory leaks
Debugging C++ programs
Tools to debug a C/C++ program
GDB overview
Examples of GDB
Setting breakpoints and inspection variable values
Function breakpoints, conditional breakpoints,  watchpoint, and the continue and finish commands
Logging gdb into a text file
Practical debugging strategies
Understanding static and dynamic analysis
Static analysis
Dynamic analysis
Exploring unit testing, TDD, and BDD
Unit testing
TDD
Example of TDD
Step 1 – writing a failing test
Step 2 – developing code to let the test pass
Step 3 – refactoring
BDD
Summary
Further reading
Exercises and questions
Graphical User Interface with Qt
Technical requirements
Understanding cross-platform GUI programming
Using C++ as Java
Qt's cross-platform model
Writing a simple application
Discovering Qt
Grasping signals and slots
Understanding Model/View programming
Using Qt widgets
Common Qt widgets
Composing widgets using layouts
Summary
Questions
Further reading
Section 3: C++ in the AI World
Using C++ in Machine Learning Tasks
Technical requirements
Introduction to AI
Computer vision
NLP
Knowledge reasoning
ML
Understanding ML
Designing an algorithm that learns
Categories of ML
Applications of ML
Neural networks
Clustering
Regression analysis
C++ and ML
Summary
Questions
Further reading
Implementing a Dialog-Based Search Engine
Technical requirements
Understanding the structure of a search engine
Providing a convenient user interface
Dealing with typos in queries
Crawling websites
Indexing documents
Tokenizing documents
Sorting the results
Building a recommendation engine
Using a knowledge graph
Implementing a dialog-based search engine
Implementing the query parser
Implementing the query processor
Summary
Questions
Further reading
Assessments
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Other Books You May Enjoy
Leave a review - let other readers know what you think
This book will provide readers with details of C++ programs with regard to the C++17 and C++20 standards, and how they are compiled, linked, and executed. It will also cover how memory management works, what the best practices are as regards memory management problems, what classes are and how they are implemented, how a compiler optimizes code, and what the compiler's approach is in terms of supporting class inheritance, virtual functions, and templates.
The book will also tell readers how to apply memory management, object-oriented programming, concurrency, and design patterns to create world-ready production applications.
Readers will learn the inner details of efficient data structures and algorithms, and will understand how to measure and compare them to choose what fits best for a specific problem.
This book will help readers to incorporate system design skills with essential design patterns into C++ applications.
By way of a bonus, the book also provides an introduction into the AI world, including machine learning basics using the C++ programming language.
By the end of this book, readers should be confident enough to design and architect real-world, scalable C++ applications using efficient data structures and algorithms.
C++ developers seeking to find details relating to the language and program structure, or who are trying to advance their expertise by digging into the essence of programs to design reusable, scalable architectures, will benefit from this book. Those developers who are intending to design efficient data structures and algorithms using the new features of C++17 and C++20 will also benefit.
Chapter 1, Introduction to Building C++ Applications, contains an introduction to the C++ world, its applications, and recent updates to the language standard. This chapter also includes a good overview of the topics covered by C++ and an introduction to the code compilation, linking, and execution phases.
Chapter 2, Low-Level Programming with C++, focuses on a discussion of C++ data types, arrays, pointers, and addressing and manipulation with pointers, along with low-level details of conditionals, loops, functions, function pointers, and structs. This chapter also includes an introduction to structures (structs).
Chapter 3, Details of Object-Oriented Programming, dives into the structure of classes and objects, and how a compiler implements object lifetimes. By the end of this chapter, the reader will understand the implementation details of inheritance and virtual functions, as well as the essential inner details of OOP in C++.
Chapter 4, Understanding and Designing Templates, introduces C++ templates, examples of template functions, template classes, template specialization, and template meta-programming in general. Traits and meta-programming will incorporate the magic in C++ applications.
Chapter 5, Memory Management and Smart Pointers, dives into the details of memory sections, allocation, and management in general, including the use of smart pointers to avoid potential memory leaks.
Chapter 6, Digging into Data Structures and Algorithms in STL, introduces data structures and their STL implementation. This chapter also includes a comparison of data structures and a discussion of proper applications with real-world examples.
Chapter 7, Functional Programming, focuses on functional programming, which is a different programming paradigm, allowing readers to concentrate on the"functional"rather than the "physical"structure of the code. Mastering functional programming provides developers with a new skill that helps to offer even better solutions to problems.
Chapter 8, Concurrency and Multithreading, focuses on how to make your programs run faster by leveraging concurrency. When an efficient data structure with efficient algorithms hits the limits of program performance, concurrency comes to the rescue.
Chapter 9, Designing Concurrent Data Structures, focuses on leveraging data structures and concurrency to design lock-based and lock-free concurrent data structures.
Chapter 10, Designing World-Ready Applications, focuses on incorporating the knowledge acquired from previous chapters into designing robust real-world applications by using design patterns. This chapter also includes understanding and applying domain-driven design by designing an Amazon clone.
Chapter 11, Designing a Strategy Game Using Design Patterns, deals with incorporating the knowledge acquired from previous chapters into designing a strategy game by using design patterns and best practices.
Chapter 12, Networking and Security, introduces network programming in C++ and how to build a dropbox backend clone by leveraging network programming skills. This chapters also includes a discussion of how to ensure coding best practices.
Chapter 13, Debugging and Testing, focuses on debugging C++ applications and best practices to avoid bugs in code, applying static code analysis in order to reduce issues in the program, introduction, and application of test-driven development and behavior-driven development. This chapter also includes a discussion of the difference between behavior-driven development and TDD as well as use cases.
Chapter 14, Graphical User Interface with Qt, introduces the Qt library and its main components. This chapter also includes an understanding of the cross-platform nature of the Qt, continuing the dropbox example by building a simple desktop client.
Chapter 15, Using C++ in Machine Learning Tasks, deals with a brief introduction to the AI concepts and recent developments in the field. This chapter also includes an introduction to machine learning and tasks such as regression analysis and clustering, as well as how to build a simple neural network.
Chapter 16, Implementing a Dialog-Based Search Engine, deals with applying the knowledge of all previous chapters to design an efficient search engine described as dialog-based because it finds the right document by asking (and learning) the corresponding questions of the user.
Basic C++ experience, including a familiarity with memory management, object-oriented programming, and basic data structures and algorithms, will be a big plus. If you have a yearning to learn how this complex program works under the hood, as well as a desire to understand the details of the programming concepts and best practices of C++ application design, then you should definitely proceed further with this book.
Software/hardware covered in the book
OS requirements
g++ compiler
Ubuntu Linux is a plus, but not a requirement
You will also need the Qt framework to be installed on your computer. Details are covered in the relevant chapter.
At the time of writing this book, not all C++ compilers were supporting all the new C++20 features, consider using the latest version of your compiler in order to test out more features introduced in the chapter.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Expert-CPP. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838552657_ColorImages.pdf
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In this section, the reader will learn the details of C++ program compilation and linking, and dive into the details of Object-Oriented Programming (OOP), templates, and memory management.
This section comprises the following chapters:
Chapter 1
,
Introduction to
Building C++ Applications
Chapter 2
,
Low-Level Programming with C++
Chapter 3
,
Details of Object-Oriented Programming
Chapter 4
,
Understanding and Designing Templates
Chapter 5,
Memory Management and Smart Pointers
Programming languages differ by their program execution model; the most common are interpreted and compiled languages. Compilers translate source code into machine code, which a computer can run without intermediary support systems. Interpreted language code, on the other hand, requires support systems, interpreters, and the virtual environment to work.
C++ is a compiled language, which makes programs run faster than their interpreted counterparts. While C++ programs should be compiled for each platform, interpreted programs can operate cross-platform.
We are going to discuss the details of a program-building process, starting with the phases of processing the source code – done by the compiler- and ending with the details of the executable file (the compiler's output). We will also learn why a program built for one platform won't run on another one.
The following topics will be covered in this chapter:
Introduction to C++
20
Details of the C++ preprocessor
Under the hood of the source code compilation
Understanding the linker and its functionality
The process of loading and running an executable file
The g++ compiler with the option -std=c++2a is used to compile the examples throughout the chapter. You can find the source files used in this chapter at https://github.com/PacktPublishing/Expert-CPP .
C++ has evolved over the years and now it has a brand-new version, C++20. Since C++11, the C++ standard has grown the language feature set tremendously. Let's look at notable features in the new C++20 standard.
Concepts are a major feature in C++20 that provides a set of requirements for types. The basic idea behind concepts is the compile-time validation of template arguments. For example, to specify that the template argument must have a default constructor, we use the default_constructible concept in the following way:
template <
default_constructible
T>void make_T() { return T(); }
In the preceding code, we missed the typename keyword. Instead, we set a concept that describes the T parameter of the template function.
We can say that concepts are types that describe other types – meta-types, so to speak. They allow the compile-time validation of template parameters along with a function invocation based on type properties. We will discuss concepts in detail in Chapter 3, Details of Object-Oriented Programming, and Chapter 4, Understanding and Designing Templates.
C++20 is a new big release of the C++ language. It contains many features that make the language more complex and flexible. Concepts, ranges, and coroutines are some of the many features that will be discussed throughout the book.
One of the most anticipated features is modules, which provide the ability to declare modules and export types and values within those modules. You can consider modules an improved version of header files with the now redundant include-guards. We'll cover C++20 modules in this chapter.
Besides notable features added in C++20, there is a list of other features that we will discuss throughout the book:
The spaceship operator:
operator<=>()
. The verbosity of operator overloading can now be controlled by leveraging
operator<=>()
.
constexpr
conquers more and more space in the language. C++20 now has the
consteval
function,
constexpr std::vector
and
std::string
, and many more.
Math constants, such as
std::number::pi
and
std::number::log2e
.
Major updates to the Thread library, including stop tokens and joining threads.
The iterator concepts.
Move-only views and other features.
To better understand some new features and also dive into the essence of the language, we will introduce the language's core starting from previous versions. This will help us to find better uses for new features compared to older ones, and will also help in supporting legacy C++ code. Let's now start by gaining an understanding of the C++ application building-process.
You can use any text editor to write code, because, ultimately, code is just text. To write code, you are free to choose between simple text editors such as Vim, or an advanced integrated development environment (IDE)such as MS Visual Studio. The only difference between a love letter and source codeis that the latter might be interpreted by a special program called acompiler(whilethe love letter cannot be compiled into a program,itmight give you butterflies in your stomach).
To mark the difference between a plain text file and source code, a special file extension is used. C++ operates with the .cpp and .h extensions(youmay also occasionally encounter .cxx and .hpp as well). Before getting into the details, think of the compiler as a tool that translates the source code into a runnable program, known as an executable file or just an executable. The process of making an executable from the source code is called compilation. Compiling a C++ program is a sequence of complex tasks that results in machine code generation. Machine code is the native language of the computer— that's why it's called machine code.
Typically, a C++ compiler parses and analyzes the source code, then generates intermediate code, optimizes it, and finally, generates machine code in a file calledan object file. You may have already encountered object files; they have individual extensions – .o in Linux and .obj in Windows. The created object file contains more than just machine code that can be run by the computer. Compilation usually involves several source files, and compiling each source file produces a separate object file. These object files are then linked together by a tool called the linker to form a single executable file. The linker uses additional information stored in object files to link them properly (linking will be discussed later in this chapter).
The following diagram depicts the program-building phases:
The C++ application-building process consists of three major steps:preprocessing, compiling, and linking. All of these steps are done using different tools, but modern compilers encapsulate them in a single tool, thereby providing a single and more straightforward interfaceforprogrammers.
The generated executable file persists on the hard drive of the computer. In order to run it, it should be copied to the main memory, the RAM. The copying is done by another tool, named the loader. The loader is a part of the operating system that knows what and where should be copied from the contents of the executable file. After loading the executable file to the main memory, the original executable file won't be deleted from the hard drive.
The loading and running of a program is done by the operating system (OS). The OS manages the execution of the program, prioritizes it over other programs, unloads it when it's done, and so on. The running copy of the program is called aprocess. A process is an instance of an executable file.
The most common use of the preprocessor is the #include directive, intended to include header files in the source code. Header files contain definitions for functions, classes, and so on:
// file: main.cpp #include <iostream> #include "rect.h"
int main() { Rect r(3.1, 4.05) std::cout << r.get_area() << std::endl;}
Let's suppose the header file rect.h is defined as follows:
// file: rect.h
struct Rect {private: double side1_; double side2_;public: Rect(double s1, double s2); const double get_area() const;};
The implementation is contained in rect.cpp:
// file: rect.cpp#include "rect.h"Rect::Rect(double s1, double s2) : side1_(s1), side2_(s2){}const double Rect::get_area() const { return side1_ * side2_;}
After the preprocessor examines main.cpp and rect.cpp, it replaces the #include directives with corresponding contents of iostream and rect.hfor main.cpp andrect.h for rect.cpp. C++17 introduces the __has_include preprocessor constant expression. __has_include evaluates to 1 if the file with the specified name is found and 0 if not:
#if __has_include("custom_io_stream.h")
#include "custom_io_stream.h"#else#include <iostream>#endif
When declaring header files, it's strongly advised to use so-called include-guards (#ifndef, #define, #endif) to avoid double declaration errors. We are going to introduce the technique shortly. Those are, again, preprocessor directives that allow us to avoid the following scenario: type Square is defined in square.h, which includes rect.h in order to derive Square from Rect:
// file: square.h#include "rect.h"struct Square : Rect { Square(double s);};
Including both square.h and rect.h in main.cpp leads to including rect.h twice:
// file: main.cpp
#include <iostream> #include "rect.h" #include "square.h"/* preprocessor replaces the following with the contents of square.h*/// code omitted for brevity
After preprocessing, the compiler will receive main.cpp in the following form:
// contents of the iostream file omitted for brevity
struct Rect { // code omitted for brevity};struct Rect { // code omitted for brevity};struct Square : Rect { // code omitted for brevity};int main() { // code omitted for brevity}
The compiler will then produce an error because it encounters two declarations of type Rect. A header file should be guarded against multiple inclusions by using include-guards in the following way:
#ifndef RECT_H
#define RECT_H
struct Rect { ... }; // code omitted for brevity
#endif // RECT_H
When the preprocessor meets the header for the first time, RECT_H is not defined and everything between #ifndef and #endif will be processed accordingly, including the RECT_H definition. The second time the preprocessor includes the same header file in the same source file, it will omit the contents because RECT_H has already been defined.
These include-guards are part of directives that control the compilation of parts of the source file. All of the conditional compilation directives are #if, #ifdef, #ifndef, #else, #elif, and #endif.
Conditional compilation is useful in many cases; one of them is logging function calls in so-called debug mode. Before releasing the program, it is advised to debug your program and test against logical flaws. You might want to see what happens in the code after invoking a certain function, for example:
void foo() {
log("foo() called");
// do some useful job}void start() {
log("start() called");
foo(); // do some useful job}
Each function calls the log() function, which is implemented as follows:
void log(const std::string& msg) {
#if DEBUG
std::cout << msg << std::endl;
#endif
}
The log() function will print the msg if DEBUG is defined. If you compile the project enabling DEBUG (using compiler flags, such as -D in g++), then the log() function will print the string passed to it; otherwise, it will do nothing.
Modules fix header files with annoying include-guard issues. We can now get rid of preprocessor macros. Modules incorporate two keywords, import and export. To use a module, we import it. To declare a module with its exported properties, we use export. Before listing the benefits of using modules, let's look at a simple usage example. The following code declares a module:
export module
test;
export
int twice(int a) { return a * a; }
The first line declares the module named test. Next, we declared the twice() function and set it to export. This means that we can have functions and other entities that are not exported, thus, they will be private outside of the module. By exporting an entity, we set it public to module users. To use module, we import it as done in the following code:
import test;
int main(){
twice(21);
}
Modules are a long-awaited feature of C++ that provides better performance in terms of compilation and maintenance. The following features make modules better in the competition with regular header files:
A module is imported only once, similar to precompiled headers supported by custom language implementations. This reduces the compile time drastically. Non-exported entities have no effect on the translation unit that imports the module.
Modules allow expressing the logical structure of code by allowing you to select which units should be exported and which should not. Modules can be bundled together into bigger modules.
Getting rid of workarounds such as include-guards, described earlier. We can import modules in any order. There are no more concerns for macro redefinitions.
Modules can be used together with header files. We can both import and include headers in the same file, as demonstrated in the following example:
import <iostream>;
#include <vector>
int main(){ std::vector<int> vec{1, 2, 3}; for (int elem : vec) std::cout << elem;}
When creating modules, you are free to export entities in the interface file of the module and move the implementations to other files. The logic is the same as in managing .h and .cpp files.
The C++ compilation process consists of several phases. Some of the phases are intended to analyze the source code, and others generate and optimize the target machine code. The following diagram shows the phases of compilation:
Let's look at each of these phases in detail.
After all the analysis is completed, the compiler generates intermediate code that is a light version of C++ mostly C. A simple example would be the following:
class A { public:
int get_member() { return mem_; }
private: int mem_; };
After analyzing the code, intermediate code will be generated (this is an abstract example meant to show the idea of the intermediate code generation; compilers may differ in implementation):
struct A {
int mem_; };int A_get_member(A* this) { return this->mem_; }
The abstract output that we just saw is somewhat similar to the actual object file structure that the compiler produces after the compilation of a unit. The structure of an object file depends on the platform; for example, in Linux, it is represented in ELF format (ELF stands for Executable and Linkable Format). A platform is an environment in which a program is executed. In this context, by platform, we mean the combination of the computer architecture (more specifically, the instruction set architecture) and operating system. Hardware and operating systems are designed and created by different teams and companies. Each of them has different solutions to design problems, which leads to major differences between platforms. Platforms differ in many ways, and those differences are projected onto the executable file format and structure as well. For example, the executable file format in Windows systems is Portable Executable (PE), which has a different structure, number, and sequence of sections than the ELF format in Linux.
An object file is divided into sections. Most important for us are the code sections (marked as .text) and the data section (.data). The .text section holds the program instructions and the .data section holds the data used by instructions. Data itself may be split into several sections, such as initialized, uninitialized, and read-only data.
An important part of the object files in addition to the .text and .data sections is the symbol table. The symbol table stores the mappings of strings (symbols) to locations in the object file. In the preceding example, the compiler-generated output had two portions, the second portion of which was marked as information:, which holds the names of the functions used in the code and their relative addresses. This information: is the abstract version of the actual symbol table of the object file. The symbol table holds both symbols defined in the code and symbols used in the code that need to be resolved. This information is then used by the linker in order to link the object files together to form the final executable file.
In this chapter, we touched on a few of the many new features of C++20 and are now ready to dive deeper into the language. We discussed the process of building a C++ application and its compilation phases. This includes analyzing the code to detect syntactical and grammatical errors, generating intermediate code to make optimizations, and finally, generating the object file that will be linked together with other generated object files to form the final executable file.
In the next chapter, we will learn about C++ data types, arrays, and pointers. We will also gain an understanding of what pointers are and look at low-level details of conditionals.
What is the difference between a compiler and an interpreter?
List the program compilation phases.
What does the preprocessor do
?
What are the tasks of the linker?
What is the difference between statically and dynamically linked libraries?
For more information, refer to Advanced C and C++ Compiling at https://www.amazon.com/Advanced-C-Compiling-Milan-Stevanovic/dp/1430266678/
LLVM Essentials, https://www.packtpub.com/application-development/llvm-essentials
