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.
Learning how to use the boost APIs
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:
- 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
- To compile the source code, run the following code:
> mkdir build && cd build
> cmake ..
> make recipe05_examples
- 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
For more information on the boost libraries, please check out https://www.boost.org/.