Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learning Boost C++
Learning Boost C++

Learning Boost C++: Solve practical programming problems using powerful, portable, and expressive libraries from Boost

eBook
zł39.99 zł177.99
Paperback
zł221.99
Subscription
Free Trial

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Learning Boost C++

Chapter 1. Introducing Boost

Welcome to learning about the richest collection of C++ libraries around, that is, Boost. In this introductory chapter, we will take a look at:

  • The history and evolution of Boost
  • What is Boost?
  • Getting started with Boost libraries

Like all the chapters in the book, this is a hands-on chapter that will require you to type in commands, write and test your code. Therefore, you should have access to a computer with a reasonably modern C++ compiler and an internet connection to download free software, including Boost libraries.

How it all started

Sometime around 1997-98, when the draft of the first C++ Standard was being finalized for publication as an ISO/IEC Standard, Robert Klarer from the IBM Labs conceived the idea of a programming language that would be called BOOSE (pronounced "booz"), and which would compete with Java in the area of high-performance embedded software development, which the latter had been aimed at. In a 1998 article for the now defunct C++ Report magazine, C++ guru Herb Sutter wrote a tongue-in-cheek spoof on this new language, whose name ostensibly expanded to Bjarne's Object Oriented Software Environment. In this article, he claimed that portability and potability were, among other things, key advantages of this language, which also supposedly promoted extraordinary camaraderie in team environments and made developers excessively happy, communicative, and passionate.

While this was an April Fools' Day article in 1998, the fact remained that the first C++ Standard was going to have a fairly basic standard library consisting of a memory allocation subsystem, type-generic containers and algorithms, a string class, basic abstractions for input and output devices, and sundry utilities. Now around the same time, a few folks from the C++ Standards Committee formed a group that worked on producing a collection of high-quality, peer-reviewed, free, and open source libraries in C++ that would have wide applicability and complement the features in standard C++. Inspired by BOOSE, perhaps for its stated competition with Java, which was a newer language but with a much richer library, they named this initiative Boost, a working title that stuck (source: FAQ on the Boost website, http://www.boost.org).

What is Boost?

Boost is a collection of free, peer-reviewed, portable, open source libraries in C++. Over the last decade and a half, there have been, as of this writing, 57 releases of the Boost libraries. In this span, Boost has released libraries of compelling usefulness that promote correct, portable, efficient, and readable C++ code. A number of prominent Standards Committee members are also the most active participants in Boost and subsequent directions of C++ standardization have been heavily influenced by the work done at Boost. Boost has provided the Standards Committee with the laboratory they need to perfect their ideas for the best new features that C++ should have. Several Boost libraries were included in the Technical Report 1 of the C++ Standards Committee, which considerably enhanced the functionality defined in the C++ 2003 revised standard; these included both language and library features. Most of these libraries made it to the C++11 Standard published in 2011. A couple more library features that originated in Boost have been added to the latest revision of the C++ Standard known as C++14 (published in 2014).

Over the years, Boost has added libraries for string and text processing, including regular expression handling, generic containers compatible with the Standard Library, smart pointers for efficient exception-safe memory management, concurrent programming, network programming, interprocess communication, filesystem handling, template metaprogramming, and many others. The following table lists some of the prominent Boost libraries grouped by category. This is by no means exhaustive:

Category

Libraries

Memory management

Smart Ptr, Align, Pool

Data structures

Container, Array, Multi-Index, Bimap, Pointer Container, Optional, Variant, Any, Tuple, Assign

Algorithms

Algorithm, Range

String and text

Conversion, String Algo, Regex, Tokenizer, Spirit, Xpressive

Systems programming

System, Filesystem, Chrono, Date Time, Thread, Asio, Interprocess

I/O

IOStreams, Locale, Serialization, Format

Higher-order programming

Function, Bind, Phoenix, Signals2

Generic programming

Enable If, Type Traits, MPL, Fusion, Proto

Language features emulation

Foreach, Move, Exception, Parameter

Correctness and testing

Test, Static Assert

Miscellaneous

Utility, Log, Uuid, Program Options, CRC

Boost libraries have found varied use in the industry because of some very high-performance libraries (such as Boost.Asio and Boost.Intrusive), and because of a very permissive and uncomplicated Boost license, which allows source redistribution, distribution of derivative work, and distribution in a binary form for noncommercial as well as commercial purposes with minimal constraints. In the next section, we will set up a development environment that enables us to use any Boost library in our C++ code using consistent conventions. This should serve us well for the rest of the book.

Getting started with Boost libraries

We shall now set up a development sandbox for you to write code using the Boost libraries. We can either install a binary distribution of the Boost libraries, or build them from source. If we build them from source, we have a whole range of concerns to take care of from choosing a suitable naming convention for the library files and building the libraries, to making sure that we are linking them to the correct versions of the library. There are platform-specific differences too that need to be handled; we shall take a look at both the Linux and Windows environments.

Necessary software

On Linux, we will only consider the C++ compiler (g++) version 4.8.1 or later, distributed with the GNU Compiler Collection (GCC). On Windows, we will use Visual Studio 2013. You can get more elaborate software support matrices for each Boost release on the Boost website.

Linux toolchain

You should be able to build Boost on most major Linux distributions. I use a Lubuntu 14.04 32-bit installation with GCC 4.8.1 and Clang 3.4. You can possibly build on much older distributions, as the Boost website lists GCC 3.3 as the minimum supported version. If you also want good C++11 support, use GCC 4.8 or higher.

Required software

Minimum version

Recommended version

Ubuntu package

Fedora/CentOS package

GNU C++ compiler

4.8.x

4.8.4

g++

gcc-c++

GNU Standard C++ Library

4.8.x

4.8.4

libstdc++-dev

libstdc++-devel

GNU Standard C++ runtime

4.8.x

4.8.4

libstdc++

libstdc++

If you want to use Clang instead of GCC, the recommended version is 3.4 or higher. Here are the required packages on Ubuntu:

Required software

Minimum version

Recommended version

Ubuntu package

LLVM compiler toolchain

3.2

3.4

llvm

LLVM C, C++, and Objective-C compiler

3.2

3.4

clang

LLVM C++ Standard Library

3.2

3.4

libc++-dev

Windows toolchain

You should be able to build Boost on Visual Studio 7.1 upwards. I use Visual Studio 2013 on a Windows 7 64-bit installation:

Required

software

Minimum version

Recommended version

Visual Studio with Visual C++

7.1

12 (2013)

I would also recommend installing 7-Zip on Windows to extract Boost sources from the .7z or .tar.bz2 archives, which offer much better compression than the .zip archives.

Obtaining and building Boost libraries

You can build the Boost libraries from source or install them as an operating system package on platforms where such as package is available. All examples in this book use Boost version 1.57. You may choose to download a more recent version of the sources and most of the discussion here should still hold. However, few details may change from one release to the next, so you should be prepared to dig into the online documentation.

Planning your Boost sandbox

As part of our day-to-day development work using Boost, we would need access to Boost's header files and Boost's libraries. A vast number of Boost libraries are header-only, which means that you just need to include the appropriate headers and build your sources. Some others have to be built into binary libraries that can be linked statically or dynamically to your application.

If we build from source, we will first identify a directory on our development machine, where we would like to install these files. The choice is arbitrary, but we can follow conventions if they exist. So on Linux, we can choose to install the library headers and binaries under /opt/boost. On Windows, this could be f:\code\libraries\Boost. You are free to choose different paths, just avoid spaces within them for less hassle.

Library naming conventions

Boost library binaries can have names that are difficult to decipher at first. So, we shall learn about what goes into naming the libraries. Library names have different layouts. Depending on the layout, different components are added to the base name in order to identify different facets of the library's binary compatibility and functionality.

Library name components

Each library, whether static or shared, is named according to a well-defined scheme. The name of a library can be split into several components, not all of which are mandatory:

  • Prefix: Libraries may have a prefix, typically lib. On Windows, only static libraries have this prefix while on Unix, all libraries have this prefix.
  • Toolset identifier: Library names may be tagged with the string, identifying the toolset with which it was built. Roughly speaking, a toolset or toolchain is the set of system utilities, including compiler, linker, archiver, and so on, that are used to build libraries and programs. For example, vc120 identifies the Microsoft Visual C++ 12.0 toolchain.
  • Threading model: If a library is thread-safe, that is, it can be used in multithreaded programs without additional synchronization, then its name may be tagged with mt, which stands for multithreaded.
  • ABI: ABI stands for application binary interface. This component captures details, such as whether the library is a debug library (d) or not, whether it is linked to a debug version of the runtime (g) or not, and whether the link to the runtime is static (s) or not. Thus, a debug library that is statically linked to a release version of the runtime would be marked with only sd, while one that is dynamically linked to a debug version would be marked with gd. A release version of the library dynamically linked to a release version of the runtime will have a blank ABI marker.
  • Version: This is the version string of the Boost library. For example, 1_57 would be the version marker for the Boost 1.57 libraries.
  • Extension: Library extensions identify the file types. On Windows, dynamic libraries have the extension .dll, while static libraries and import libraries have the extension .lib. On Linux and some other Unix systems, dynamic libraries have the extension .so, while static libraries or archives have the extension .a. Dynamic library extensions often have a version suffix, for example, .so.1.57.0.

Library name layouts

How a library name is made up of its components determines its name layout. There are three kinds of name layouts supported by Boost: versioned, system, and tagged.

Versioned layout

It is the most elaborate layout and is the default layout on Windows. The general structure of the versioned layout name is libboost_<name>-<toolset>-<threading>-<ABI>-<version>.<ext>. For example, here is the Boost.Filesystem library debug DLL for Windows: boost_filesystem-vc100-mt-gd-1_57.dll. The tokens in the filename tell the complete story. This DLL was built using Visual C++ 10.0 compiler (-vc100), is thread-safe (-mt), and is a debug DLL (d) linked dynamically to the debug version of the runtime (g). The version of Boost is 1.57 (1_57).

System layout

The default layout on Unix is the system layout that removes all the name decorations. The general structure of library names in this layout is libboost_<name>.<ext>. For example, here is the Boost.System shared library on Linux: libboost_filesystem.so.1.57.0. Looking at it, there is no way to tell whether it supports multithreading, whether it is a debug library, or any other detail that you could wean from a filename in the versioned layout. The 1.57.0 suffix of the extension indicates the version of the shared library. This is the Unix convention for versioning shared libraries and is not affected by the Boost name layout.

Tagged layout

There is a third layout called the tagged layout, which is midway between the versioned and system layouts in terms of detail. It removes all the version information but retains other information. Its general structure is libboost_<name>-<threading>-<ABI>.<ext>.

Here is the Boost.Exception static library from Windows built using the non-default tagged layout: libboost_filesystem-mt.lib. This is a static library as indicated by its lib- prefix. Also, -mt indicates that this library is thread-safe, and the lack of an ABI indicator means that this is not a debug library (d), nor does it link to the static runtime (s). Also, it does not link to the debug version of the runtime (g).

The versioned layout is a bit unwieldy. On systems where you need to manually specify names of libraries to link against, moving from one version of Boost to the next would require some effort to fix the build scripts. The system layout is a bit minimalistic and is great for environments where you need only one variant of a given library. However, you cannot have both debug and release versions of the library, or thread-safe and thread-unsafe ones side by side, with system layout. For this reason, in the rest of this book, we will only use tagged layout for the libraries. We will also only build thread-safe libraries (-mt) and shared libraries (.dll or .so). Some libraries can only be built as static libraries and, as such, would be automatically created by the Boost build system. So now, we finally get to the point where we have enough information to start creating our Boost sandbox.

Installing a Boost binary distribution

On Microsoft Windows and several distributions of Linux, you can install a binary distribution of the Boost libraries. The following table lists the methods of installing Boost on some of the popular operating systems:

Operating system

Package name

Install method

Microsoft Windows

boost_1_57_0-msvc-12.0-64.exe (64-bit)

boost_1_57_0-msvc-12.0-32.exe (32-bit)

Download executable from http://sourceforge.net/projects/boost/files/boost-binaries/ and install it by running the executable

Ubuntu

libboost-all-dev

sudo apt-get install libboost-all-dev

Fedora/CentOS

boost-devel

sudo yum install boost-devel

Installing a binary distribution is convenient because it is the fastest way to be up and running.

Installing on Windows

Starting with Boost 1.54, you can download a binary distribution of the Boost libraries, built using Microsoft Visual Studio, from SourceForge. The download is available as a 64-bit or 32-bit installable executable that contains header files, libraries, sources, documentation, and tools. There are separate distributions for different versions of Visual Studio, from version 12 (VS 2013) backward through version 8 (VS 2005). The name of the executable is of the form boost_ver-msvc-vcver-W.exe, where ver is the Boost version (for example, 1_57_0), vcver is the version of Visual C++ (for example, 12.0 for Visual Studio 2013), and W is the native word size of your operating system (for example, 64 or 32).

As part of the installation, you can choose the directory where you want to install the Boost libraries. Let us consider that you choose to install it under boost-dir. Then, the following directories contain the necessary headers and libraries:

Directory

Files

boost-dir

This is the base directory of the Boost installation. All the header files are present in a hierarchy under the boost subdirectory.

boost-dir/libW-msvc-vcver

This directory contains all variants of the Boost libraries, static and shared (DLLs), debug and release. The library filenames follow the versioned layout.

W: 32 or 64 depending on whether you installed a 32-bit version or 64-bit version.

vcver: Visual Studio version.

boost-dir/doc

This directory contains the library documentation in the HTML format and contains scripts to build PDF docs.

Installing on Linux

On Ubuntu, you need to install the libboost-all-dev package. You need to perform the installation using superuser privileges, so run the following command:

$ sudo apt-get install libboost-all-dev

This installs the necessary headers and libraries in the following directories:

Directory

Files

/usr/include

This contains all the header files present in a hierarchy under the boost subdirectory.

/usr/lib/arch-linux-gnu

This contains all the Boost libraries, static and shared (DSOs). The library filenames follow the system layout.

Replace arch with x86_64 for 64-bit operating systems and with i386 for 32-bit operating systems.

On CentOS/Fedora, you need to install the boost-devel package. You need to perform the installation using superuser privileges, so this is the command to run:

$ sudo yum install boost-devel

This installs the necessary headers and libraries in the following directories:

Directory

Files

/usr/include

This contains all the header files present in a hierarchy under the boost directory.

/usr/lib

This contains all the Boost libraries, static and shared (DSOs). The library filenames follow the system layout.

Building and installing the Boost libraries from source

Building the Boost libraries from source offers more flexibility, as it is easy to customize the build, use alternative compilers/toolchains, and change the default name layout like we plan to. We shall build the Boost libraries from a source archive downloaded from the Boost website http://www.boost.org or http://sourceforge.net/projects/boost. I prefer the 7-Zip or the bzip2 archives, as they have the best compression ratios. We will use Boost libraries Version 1.57, and we will look at building them only on Linux and Windows operating systems.

Optional packages

There are several optional packages that are used to provide additional functionality by certain Boost libraries when present. These include:

  • The zlib and bzip2 development libraries, used by Boost.IOStream to read and write compressed archives in gzip and bzip2 formats
  • The ICU i18n development libraries, which are heavily used by Boost.Locale and also by Boost.Regex to support Unicode regular expressions
  • The expat XML parser library, used by the Boost.Graph library to support the GraphML XML vocabulary for describing graphs

Some of these libraries may be made available through your native package management systems, particularly on Linux. When installed from such packages, the Boost build system may find these libraries automatically and link them by default. If you chose to build these libraries from source and installed them at non-standard locations instead, then you should use specific environment variables to point to the installation directory of these libraries or to the include and library directories. The following table summarizes these optional libraries, their source websites, Ubuntu package names, and the environment variables needed by Boost to identify them when installed from source:

Library

Details

Zlib library (http://www.zlib.net)

Environment variable: ZLIB_SOURCE (extracted source directory)

Ubuntu packages: zlib1g, zlib1g-dev, and zlib1c

Bzip2 library (http://www.bzip.org/downloads.html)

Environment variable: BZIP2_SOURCE (extracted source directory)

Ubuntu packages: libbz2 and libbz2-dev

ICU library (http://www.icu-project.org/download)

Environment variables:

HAVE_ICU=1

ICU_PATH (installation root)

Ubuntu package: libicu-dev

Expat library (http://sourceforge.net/projects/expat)

Environment variables: EXPAT_INCLUDE (expat include dir) and EXPAT_LIBPATH (expat library dir)

Ubuntu packages: libexpat1 and libexpat1-dev

We will be using the gzip and bzip2 libraries in Chapter 9, Files, Directories, and IOStreams, to compress data, while we will not be using the ICU and Expat libraries for the code examples in this book.

Building the Boost libraries on Linux

If you choose not to install a binary distribution of Boost or if such a distribution is not available for your platform, then you must build the Boost libraries from source. Download the source archives for the Boost libraries, zlib and bzip2. Assuming that you want to install Boost in the/opt/boost directory, perform the following steps from a shell command prompt to build Boost with the GNU toolchain:

  1. Create a directory and extract the Boost source archive in it:
    $ mkdir boost-src
    $ cd boost-src
    $ tar xfj /path/to/archive/boost_1_57_0.tar.bz2
    $ cd boost_1_57_0
    
  2. Generate the Boost build system for your toolset. The following should work if you are building with g++:
    $ ./bootstrap.sh
    

    If you are using Clang instead, run the following:

    $ ./bootstrap.sh toolset=clang cxxflags="-stdlib=libc++ -std=c++11" linkflags="-stdlib=libc++"
    
  3. Extract the bzip2 and zlib source archives and make a note of the directories they have been extracted to.
  4. Build the libraries and install them. For GCC, run the following command:
    $ ./b2 install --prefix=/opt/boost --build-dir=../boost-build --layout=tagged variant=debug,release link=shared runtime-link=shared threading=multi cxxflags="-std=c++11" -sZLIB_SOURCE=<zlib-source-dir> -sBZIP2_SOURCE=<bzip2-source-dir>
    

    For Clang, run the following command instead:

    $ ./b2 install toolset=clang --prefix=/opt/boost --build-dir=../boost-build --layout=tagged variant=debug,release link=shared runtime-link=shared threading=multi cxxflags="-stdlib=libc++ -std=c++11" linkflags="-stdlib=libc++" -sZLIB_SOURCE=<zlib-source-dir> -sBZIP2_SOURCE=<bzip2-source-dir>
    

The last step should build all the Boost libraries and install them under the /opt/boost directory, as identified by the --prefix option. All the libraries will be installed under /opt/boost/lib and all include files under /opt/boost/include. In addition to the Boost libraries, you should also see libboost_zlib-mt.so and libboost_bzip2-mt.so—the dynamic shared objects for zlib and bzip2, which libboost_iostreams-mt.so depends on.

  • The --build-dir option would identify the directory in which the intermediate products of the build are created.
  • The --layout=tagged option chooses the tagged layout for library names.
  • We will build only thread-safe (threading=multi) shared libraries (link=shared) if possible, linked them to the dynamic runtime (runtime-link=shared). We would need both debug and release versions of the library (variant=debug,release).
  • The -sZLIB_SOURCE=<zlib-source-dir> option is used to point the build to the directory under which the zlib sources were extracted in step 3; likewise, for the bzip2 source directory, using -sBZIP2_SOURCE=<bzip2-source-dir>.
  • If you want to build Boost libraries using support for C++11, then you should use the cxxflags="-std=c++11" option. Throughout the rest of the book, many of the code examples use features from C++11. Enabling a C++11 build of Boost at this point might be a good idea. Make sure that your compiler has good support for C++11. For g++, it would be version 4.8.1 or later. Also, make sure that you compile all your own code using the Boost libraries with C++11 as well.

Note

Most of the examples in this book use C++11 features, and so you should keep the C++11 option on while compiling Boost. Appendix provides a short introduction to the important C++11 features used in this book, and also describes how you can emulate them in C++03 using Boost if you are still using an older compiler.

Building the Boost libraries on Windows

Once you have downloaded the Boost source archive, from a Windows Explorer session, create a directory called boost-src and extract the source archive inside this directory. Assuming that you want to install Boost in the boost-dir directory and boost-build is the directory in which the intermediate products of the build are kept, perform the following steps from a command prompt:

  1. Initialize the 32-bit Visual C++ build environment to build the Boost build system (even if you want to build 64-bit):
    "C:\Program Files\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
    
  2. On a 64-bit system with a 32-bit Visual Studio installation, Visual Studio is typically installed under C:\Program Files (x86), so you will have to run this command instead:
    "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
    
  3. Generate the Boost build system for your toolset:
    cd /d drive:\path\to\boost-src
    bootstrap.bat
    
  4. If you want to build 64-bit Boost libraries, initialize the 64-bit Visual C++ build environment:
    "C:\Program Files\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64
    
  5. On a 64-bit system with 32-bit Visual Studio installation, you will have to run this command instead:
    "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64
    
  6. Extract the bzip2 and zlib source archives, and make a note of the directories they have been extracted to.
  7. Build the libraries and install them. If you want to build 32-bit libraries, use the following command line:
    b2 install --libdir=boost-dir\libs --includedir= boost-dir\include --build-dir= boost-build --layout=tagged variant=debug,release threading=multi link=shared runtime-link=shared -sZLIB_SOURCE=<zlib-src-dir> -sBZIP2_SOURCE=<bzip2-src-dir>
    
  8. If you want to build 64-bit libraries, use the following command line:
    b2 install --libdir=boost-dir\libs64 --includedir= boost-dir\include --build-dir= boost-build64 --layout=tagged variant=debug,release threading=multi link=shared runtime-link=shared address-model=64 –sZLIB_SOURCE=<zlib-src-dir> -sBZIP2_SOURCE=<bzip2-src-dir>
    

This last step builds and installs the necessary headers and libraries in the following directories:

Directory

Files

boost-dir/include

All header files present in a hierarchy under the boost directory.

boost-dir/libs

All 32-bit Boost libraries, static and shared libraries (DLLs), debug and release.

boost-dir/libs64

All 64-bit Boost libraries, static and shared libraries (DLLs), debug and release

In addition to the Boost libraries, you should also see boost_zlib-mt.dll and boost_bzip2-mt.dll—the DLLs for zlib and bzip2, which boost_iostreams-mt.dll depends on.

Let us take a look at the various options we have used in the preceding commands:

  • The --build-dir option will identify the directory in which the intermediate products of the build are created.
  • The --layout=tagged option chooses the tagged layout for the library names, as explained earlier.
  • We will build only the shared libraries (link=shared). If possible, link them to the dynamic runtime (runtime-link=shared), and create thread-safe libraries (threading=multi).
  • We will want both debug and release versions of the library (variant=debug,release).
  • The 32- and 64-bit builds will take place in separate intermediate directories identified by the --build-dir option and will be copied to separate library directories identified by the --libdir option.
  • The address-model=64 option would trigger the 64-bit build.

Under Visual Studio 2013, C++11 support is automatically enabled, and you do not need to use any specific switches for the purpose.

Using Boost libraries in your projects

We shall now write our first small C++ program that uses the Boost Filesystem library to check for the existence of a file whose name is passed to on the command line and then build on Linux and Windows.

Here is the listing for chkfile.cpp:

 1 #include <iostream>
 2 #include <boost/filesystem.hpp>
 3 // define a short alias for the namespace
 4 namespace boostfs = boost::filesystem;
 5
 6 int main(int argc, char *argv[])
 7 {
 8   if (argc <= 1) {
 9     std::cerr << "Usage: " << argv[0] << " <filename>"
10               << std::endl;
11     return 1;
12   }
13
14   boostfs::path p(argv[1]);
15
16   if (boostfs::exists(p)) {17     std::cout << "File " << p << " exists." << std::endl;
18   } else {
19     std::cout << "File " << p << " does not exist." << '\n';
20   }
21
22   return 0;
23 }

Linking against Boost libraries on Linux

If you have installed Boost in a nonstandard location (which is typically the case if you have not installed it from a native package), then you will need to make sure that your preprocessor can find the Boost header files you have included using the –I option in the compiler:

$ g++ -c chkfile.cpp -I/opt/boost/include -std=c++11

This step will create an object file called chkfile.o, which we will link to the binary. You can specify which library to link to using the -l option. In case of a nonstandard installation, you will need to ensure that the linker can find the path to the library you want to link against using the -L option:

$ g++ chkfile.o -o chkfile -L/opt/boost/lib -lboost_filesystem-mt -lboost_system-mt -std=c++11

Note

Use the -std=c++11 option only if you built your Boost libraries using C++11.

The preceding command line will work for either a static or a shared library. However, if both types of library are found, it will use the shared version. You can override this with appropriate linker options:

$ g++ chkfile.o -o chkfile -L/opt/boost/lib -Wl,-Bstatic -lboost_filesystem-mt -Wl,-Bdynamic -lboost_system-mt -std=c++11

In the preceding case, the filesystem library is linked statically while others are linked dynamically. The -Wl switch is used to pass its arguments to the linker. In this case, it passes the -Bstatic and -Bdynamic switches.

If it is a shared library that you link against, then at runtime the dynamic linker needs to locate the shared library and load it too. The way to ensure this varies from one version of Unix to the other. One way to ensure this is to embed a search path in your executable using the rpath linker directive:

$ g++ -o chkfile chkfile.o -L/opt/boost/lib -lboost_filesystem-mt -lboost_system-mt -Wl,-rpath,/opt/boost/lib:/usr/lib/boost -std=c++11

On the target system, where the binary mytest is run, the dynamic linker would look for the filesystem and system shared libraries under /opt/boost/lib and /usr/lib/boost.

Other ways besides using the rpath mechanism also exist. Linux uses a utility called ldconfig to locate shared libraries and update search paths. For more details, look at the man pages for ldconfig (8). On Solaris, the crle utility performs a similar action.

Linking against Boost libraries on Windows

Using the Visual Studio IDE, we will have to tweak certain project settings in order to link against the Boost libraries.

First, ensure that your compiler is able to find the necessary header files:

  1. Open your C++ project in Visual Studio. From the menu, select Project | Project Properties.
  2. In the Property Pages dialog that comes up, expand Configuration Properties and select C/C++.
  3. Edit the value of Additional Include Directories by adding the path to your Boost, include directories. Separate it from other entries in the field using a semicolon:
    Linking against Boost libraries on Windows
  4. Next, ensure that your linker is able to find the shared or static libraries. In the Project Properties dialog, under Configuration Properties, choose Linker.
  5. Edit the Additional Library Directories field to add the path to the Boost libraries, separated by a semicolon from any other entries:
    Linking against Boost libraries on Windows
  6. Now you can leverage Boost's auto-linking feature on Windows to automatically link to the correct libraries. To enable this, you have to define the BOOST_ALL_DYN_LINK preprocessor symbol. To do this, in the Project Properties dialog, navigate to Configuration Properties | C/C++ | Preprocessor, and add BOOST_ALL_DYN_LINK to the Preprocessor Definitions field, separating it from other entries with a semicolon.

If you built your Boost libraries on Windows with the default layout (versioned), this is all you will need to do for linking correctly. If we use the tagged layout, we must also define a second preprocessor symbol BOOST_AUTO_LINK_TAGGED. If we use system layout for naming, we will need to define BOOST_AUTO_LINK_NOMANGLE instead. You will get a linker error without these definitions:

Linking against Boost libraries on Windows

You should now be able to build your project from your IDE without any problems. In order to run your program, the dynamic linker must be able to locate the dynamic library. To take care of this, on Windows, you can add the path of your Boost libraries to the PATH environment variable. For running your programs from within the IDE, you can add the path of your Boost libraries to the PATH variable by navigating to Debugging | Environment, as shown in the following screenshot:

Linking against Boost libraries on Windows

Building the code listings in this book

Each chapter in this book includes the example source code, which is also available for download from the Packt website (http://www.packtpub.com). You should download and build these examples on your development machines.

CMake

In order to build the examples, you need to install CMake, which is one of the most popular cross-platform build tools for C++ programs. With CMake, you can easily generate a build system of your choice on an operating system of your choice, using a single set of CMake specifications.

You can download a binary package for CMake from www.cmake.org, or download a source archive and build it on a platform of your choice.

Note

Minimum version required: CMake 2.8.

Windows: A 32-bit exe-installer is available for Windows that works for both 32-bit and 64-bit builds.

Linux: CMake is usually bundled with all major Linux distributions and is available as an optional package. Consult your distribution's package repository.

Code examples

Download the source code archive and extract it to a directory on your development machine. The layout of the extracted directory would look like this:

Code examples

The source code archive available for download contains separate directories for each chapter. Within each chapter directory, you will find the complete source code for each example. The source code files are named based on the listing identifier.

A listing identifier is a unique tag used for examples in this book, as shown in the following screenshot:

Code examples

Here, the listing identifier is Listing 11.18 and indicates that this is the eighteenth example in Chapter 11, Network Programming Using Boost Asio. Therefore, in the ch11 folder, you will find listing11_18.cpp, which contains the asynchronous UDP server example that appears in Chapter 11, Network Programming Using Boost Asio. In some cases, a big example is broken down into multiple listings in the text, but they all form part of the same source file. In such cases the listings are tagged with letters; for example, listing 7.22a, 7.22b, 7.22c, and so on. You can still expect a file called listing7_22.cpp, which combines the code from these listings.

In order to build all the examples in this book, you need to follow these steps:

  1. Make sure that CMake 2.8 or higher is installed.
  2. Extract the source archive for the book to a directory, say srcdir.
  3. Change to the cmake_bin directory under the source directory:
    $ cd srcdir/lbcpp-src/cmake_bin
    
  4. Export the BOOST_DIR environment variable to point to the Boost installation directory.

    For example, if it is /opt/boost on Linux, you can run the following command:

    $ export BOOST_DIR=/opt/boost
    

    If you have installed Boost from a standard package in the package repository of your distribution, then you can skip this step.

    On Windows, if you have installed it under f:\boost, you can run this command:

    set BOOST_DIR=f:\boost
    
  5. If the Boost include directory and the Boost library directory do not share a common parent, as may be the case if you installed a binary distribution of Boost, then you should skip setting BOOST_DIR and instead set the following two environment variables:
    • BOOST_INCDIR should be set to the directory that contains the Boost header files, for example, /usr/include on Ubuntu.
    • BOOST_LIBDIR should be set to the directory that contains the Boost library files, for example, /usr/lib/x86_64-linux-gnu on Ubuntu.
  6. Generate the build system of your choice using CMake.

    On Linux, run the following command:

    $ cmake
    

    This generates a Makefile-based build system using GNU g++. If you want to use clang++ instead, export the environment variables CC and CXX, as shown here:

    export CC=`which clang`
    export CXX=`which clang++`

    On Windows, run the following command:

    $ cmake .. -G "Visual Studio 12"
    

    This generates a Visual C++ 2013 solution file and project files. The string passed with the -G option is called the generator string and identifies the toolchain for which you want to generate the build system. The CMake documentation lists all the supported generator strings. For our purposes, we will use Visual Studio 12 or Visual Studio 12 Win64.

  7. Build the sources using the generated build system.

    On Linux, you can build it by simply running the following command:

    $ gmake
    

    On Windows, it is best to build by opening the generated solution file in Visual C++ IDE and then building all the sources or a single source at a time. You can run the examples by running the executables formed under srcdir/lbcpp-src/bin.

We do not cover CMake in this book. It is worth exploring CMake further on your own, and a great place to get started is the CMake Wiki (http://www.cmake.org/Wiki/CMake).

Self-test questions

  1. What are the different types of name layouts supported by Boost libraries?

    a. Tagged, native, and mangled

    b. Tagged, mangled, and versioned

    c. Tagged, versioned, and system

    d. Versioned, systems, and decorated

  2. Boost allows you to automatically link to necessary Boost libraries on Windows.

    a. True

    b. False

  3. What does the following filename tell you about the library?

    boost_date_time-vc100-mt-gd-1_57.dll

    Tick all that apply.

    a. It is the DateTime library.

    b. It is a thread-safe library.

    c. It was built using g++.

    d. It is not a debug library.

  4. What is the name layout of the following library?

    libboost_exception-mt-gd.lib

    a. Tagged

    b. System

    c. Versioned

    d. Default

Summary

In this chapter, we got an overview of the Boost C++ libraries and set up a development environment for us, which should help us to easily build and run C++ programs, using Boost libraries that we will learn in the rest of the book.

In the next chapter, we will learn a variety of techniques using different Boost libraries, which simplify some common day-to-day programming tasks and set us up for the heavy lifting to be done in the later chapters.

Left arrow icon Right arrow icon

Description

If you are a C++ programmer who has never used Boost libraries before, this book will get you up-to-speed with using them. Whether you are developing new C++ software or maintaining existing code written using Boost libraries, this hands-on introduction will help you decide on the right library and techniques to solve your practical programming problems.

Who is this book for?

If you are a C++ programmer who has never used Boost libraries before, this book will get you up-to-speed with using them. Whether you are developing new C++ software or maintaining existing code written using Boost libraries, this hands-on introduction will help you decide on the right library and techniques to solve your practical programming problems.

What you will learn

  • Write efficient and maintainable code using expressive interfaces from Boost libraries
  • Leverage a variety of flexible, practical, and highly efficient containers and algorithms beyond STL
  • Solve common programming problems by applying a wide array of utility libraries
  • Design and write portable multithreaded code that is easy to read and maintain
  • Craft highly scalable and efficient TCP and UDP servers
  • Build and deploy Boost libraries across a variety of popular platforms
  • Use C++11 functionality and emulate C++11 language features in C++03 code
Estimated delivery fee Deliver to Poland

Premium delivery 7 - 10 business days

zł110.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 31, 2015
Length: 558 pages
Edition : 1st
Language : English
ISBN-13 : 9781783551217
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Poland

Premium delivery 7 - 10 business days

zł110.95
(Includes tracking information)

Product Details

Publication date : Jul 31, 2015
Length: 558 pages
Edition : 1st
Language : English
ISBN-13 : 9781783551217
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 577.97
Boost.Asio C++ Network Programming
zł133.99
Learning Boost C++
zł221.99
Boost C++ Application Development  Cookbook
zł221.99
Total 577.97 Stars icon
Banner background image

Table of Contents

13 Chapters
1. Introducing Boost Chevron down icon Chevron up icon
2. The First Brush with Boost's Utilities Chevron down icon Chevron up icon
3. Memory Management and Exception Safety Chevron down icon Chevron up icon
4. Working with Strings Chevron down icon Chevron up icon
5. Effective Data Structures beyond STL Chevron down icon Chevron up icon
6. Bimap and Multi-index Containers Chevron down icon Chevron up icon
7. Higher Order and Compile-time Programming Chevron down icon Chevron up icon
8. Date and Time Libraries Chevron down icon Chevron up icon
9. Files, Directories, and IOStreams Chevron down icon Chevron up icon
10. Concurrency with Boost Chevron down icon Chevron up icon
11. Network Programming Using Boost Asio Chevron down icon Chevron up icon
A. C++11 Language Features Emulation Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(2 Ratings)
5 star 50%
4 star 50%
3 star 0%
2 star 0%
1 star 0%
Colby Mainard Jan 17, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Code and explanations are useful. Some code samples have issues that occur on raspberry pi and other ARM chips.
Amazon Verified review Amazon
Emanuele Galli Sep 17, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
If you qualify yourself as a proficient C++ programmer, you should have a good knowledge of the Boost libraries. This was true before C++11, and it is still true today, when a good number of Boost functionality are available in standard C++.This book is an excellent source to check what is currently available in Boost, and it offer a wide range of use cases, examples, and comparisons with what is available on the off-the-shelf C++.It is assumed you have already a good basic knowledge of C++, and this is in my opinion a plus, because it helps to keep the size of the book to a manageable 500+ pages. For the same reason, it is good that it is not too detailed in discussing complex stuff like the ASIO library. Here you would get a nice introduction (chapter 11) that would give you a good idea of it.In my opinion, you will get all the information you need on concepts like smart pointers (chapter 3) or the most commonly used string related functionality.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela