Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Advanced C++ Programming Cookbook

You're reading from   Advanced C++ Programming Cookbook Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction

Arrow left icon
Product type Paperback
Published in Jan 2020
Publisher Packt
ISBN-13 9781838559915
Length 454 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
 Quinn Quinn
Author Profile Icon Quinn
Quinn
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Getting Started with Library Development 2. Using Exceptions for Error Handling FREE CHAPTER 3. Implementing Move Semantics 4. Using Templates for Generic Programming 5. Concurrency and Synchronization 6. Optimizing Your Code for Performance 7. Debugging and Testing 8. Creating and Implementing Your Own Container 9. Exploring Type Erasure 10. An In-Depth Look at Dynamic Allocation 11. Common Patterns in C++ 12. A Closer Look at Type Deduction 13. Bonus - Using C++20 Features 14. Other Books You May Enjoy

Learning how to use the boost APIs

The boost libraries are a set of libraries designed to work in conjunction with the standard C++ libraries. In fact, a lot of the libraries that are currently being provided by C++ originated from the boost libraries. The boost libraries provide everything from containers, clocks, and timers to more complicated mathematical APIs such as graphs and CRC calculations. In this recipe, we will learn how to use the boost libraries, specifically to demonstrate what a large library looks like and how such a library would be included in a user's project. This recipe is important as it will demonstrate just how complicated a library can get, teaching you how to write your own libraries accordingly.

Getting ready

As with all of the recipes in this chapter, ensure that all of the technical requirements have been met, including installing Ubuntu 18.04 or higher and running the following in a Terminal window:

> sudo apt-get install build-essential git cmake libboost-all-dev

This will ensure your operating system has the proper tools to compile and execute the examples in this recipe. Once you have done this, open a new Terminal. We will use this Terminal to download, compile, and run our examples.

How to do it...

You need to perform the following steps to complete this recipe:

  1. From a new Terminal, run the following to download the source code:
> cd ~/
> git clone https://github.com/PacktPublishing/Advanced-CPP-CookBook.git
> cd Advanced-CPP-CookBook/chapter01
  1. To compile the source code, run the following code:
> mkdir build && cd build
> cmake ..
> make recipe05_examples
  1. Once the source code has been compiled, you can execute each example in this recipe by running the following commands:
> ./recipe05_example01
Date/Time: 1553894555446451393 nanoseconds since Jan 1, 1970
> ./recipe05_example02
[2019-03-29 15:22:36.756819] [0x00007f5ee158b740] [debug] debug message
[2019-03-29 15:22:36.756846] [0x00007f5ee158b740] [info] info message

In the next section, we will step through each of these examples and explain what each example program does and how it relates to the lessons being taught in this recipe.

How it works... 

The boost libraries provide a set of user APIs that implement commonly needed functionality in most programs. These libraries can be included in your own projects to simplify your code and provide an example of what a finished library might look like. To explain how your own libraries could be leveraged by others, let's look at some examples of how to use the boost libraries.

Example 1

In this example, we are using the boost APIs to output the current date and time to stdout, as follows:

#include <iostream>
#include <boost/chrono.hpp>

int main(void)
{
using namespace boost::chrono;

std::cout << "Date/Time: " << system_clock::now() << '\n';
return 0;
}

As shown in the preceding example, the current date and time are outputted to stdout as the total number of nanoseconds since the Unix Epoch (January 1, 1970). In addition to including boost in your source code, you must also link your application against the boost libraries. In this case, we needed to link against the following:

-lboost_chrono -lboost_system -lpthread

An example of how this is done can be seen in the CMakeLists.txt file that was downloaded with this recipe. Once these libraries have been linked to your project, your code will be able to leverage the APIs inside them. This extra step is why header-only libraries can be so useful when creating your own libraries as they obviate the need for additional linking.

Example 2

In this example, we're demonstrating how to log to the console using boost's trivial logging APIs, as follows:

#include <boost/log/trivial.hpp>

int main(void)
{
BOOST_LOG_TRIVIAL(debug) << "debug message";
BOOST_LOG_TRIVIAL(info) << "info message";
return 0;
}

As shown in the preceding example, the "debug message" and "info message" messages were outputted to stdout. In addition to linking against the proper boost libraries, we also had to include the following definition during compilation:

-DBOOST_LOG_DYN_LINK -lboost_log -lboost_system -lpthread

Once again, linking these libraries ensures that the APIs you are using in your code (as shown in the preceding example) exist in the executable. 

See also

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images