1 Get boost
just find a copy on https://www.boost.org
2 The boost distribution
boost_1_67_0\ .................The “boost root directory”
index.htm .........A copy of www.boost.org starts here
boost\ .........................All Boost Header files
lib\ .....................precompiled library binaries
libs\ ............Tests, .cpps, docs, etc., by library
index.html ........Library documentation starts here
algorithm\
any\
array\
…more libraries…
status\ .........................Boost-wide test suite
tools\ ...........Utilities, e.g. Boost.Build, quickbook, bcp
more\ ..........................Policy documents, etc.
doc\ ...............A subset of all Boost library docs
- The path to the boost root directory (often C:\Program Files\boost\boost_1_67_0) is sometimes referred to as $BOOST_ROOT in documentation and mailing lists .
- To compile anything in Boost, you need a directory containing the
boost\
subdirectory in your#include
path. Specific steps for setting up#include
paths in Microsoft Visual Studio follow later in this document; if you use another IDE, please consult your product's documentation for instructions. - Since all of Boost's header files have the
.hpp
extension, and live in theboost\
subdirectory of the boost root, your Boost#include
directives will look like:c #include <boost/whatever.hpp>
orc #include "boost/whatever.hpp"
depending on your preference regarding the use of angle bracket includes. Even Windows users can (and, for portability reasons, probably should) use forward slashes in #include directives; your compiler doesn't care. - Don't be distracted by the
doc\
subdirectory; it only contains a subset of the Boost documentation. Start withlibs\index.html
if you're looking for the whole enchilada.Header Organization
The organization of Boost library headers isn't entirely uniform, but most libraries follow a few patterns: * Some older libraries and most very small libraries place all public headers directly into
boost\
. * Most libraries' public headers live in a subdirectory of boost\, named after the library. For example, you'll find the Python library's def.hpp header inbash boost\python\def.hpp.
* Some libraries have an “aggregate header” in boost\ that #includes all of the library's other headers. For example, Boost.Python's aggregate header isbash boost\python.hpp.
* Most libraries place private headers in a subdirectory calleddetail\
, oraux_\
. Don't expect to find anything you can use in these directories.
3 Header-Only Libraries
The first thing many people want to know is, “how do I build Boost?” The good news is that often, there's nothing to build.
Nothing to Build?
Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.
The only Boost libraries that must be built separately are:
- Boost.Chrono
- Boost.Context
- Boost.Filesystem
- Boost.GraphParallel
- Boost.IOStreams
- Boost.Locale
- Boost.Log
(see build documentation)
- Boost.MPI
- Boost.ProgramOptions
- Boost.Python
(see the Boost.Python build documentation before building and installing it)
- Boost.Regex
- Boost.Serialization
- Boost.Signals
- Boost.System
- Boost.Thread
- Boost.Timer
- Boost.Wave
A few libraries have optional separately-compiled binaries:
- Boost.DateTime has a binary component that is only needed if you're using its to_string/from_string or serialization features, or if you're targeting Visual C++ 6.x or Borland.
- Boost.Graph also has a binary component that is only needed if you intend to parse GraphViz files.
- Boost.Math has binary components for the TR1 and C99 cmath functions.
- Boost.Random has a binary component which is only needed if you're using random_device.
- Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.
- Boost.Exception provides non-intrusive implementation of
exception_ptr
for 32-bit_MSC_VER==1310
and_MSC_VER==1400
which requires a separately-compiled binary. This is enabled by#define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR
.
4 Build a Simple Program Using Boost
To keep things simple, let's start by using a header-only library. The following program reads a sequence of integers from standard input, uses Boost.Lambda to multiply each number by three, and writes them to standard output:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
Copy the text of this program into a file called example.cpp.
Note
To build the examples in this guide, you can use an Integrated Development Environment (IDE) like Visual Studio, or you can issue commands from the command prompt. Since every IDE and compiler has different options and Microsoft's are by far the dominant compilers on Windows, we only give specific directions here for Visual Studio 2005 and .NET 2003 IDEs and their respective command prompt compilers (using the command prompt is a bit simpler). If you are using another compiler or IDE, it should be relatively easy to adapt these instructions to your environment.
4.1 Build From the Visual Studio IDE
4.2 Or, Build From the Command Prompt
4.3 Errors and Warnings
5 Prepare to Use a Boost Library Binary
If you want to use any of the separately-compiled Boost libraries, you'll need to acquire library binaries.
5.1 Simplified Build From Source
If you wish to build from source with Visual C++, you can use a simple build procedure described in this section. Open the command prompt and change your current directory to the Boost root directory. Then, type the following commands:
bootstrap
.\b2
The first command prepares the Boost.Build system for use. The second command invokes Boost.Build to build the separately-compiled Boost libraries. Please consult the Boost.Build documentation for a list of allowed options.
5.2 Or,Build Binary From Source
5.3 Expected Build Output
5.4 In Case of Buid Errors
6 Link Your Program to a Boost Library
To demonstrate linking with a Boost binary library, we'll use the following simple program that extracts the subject lines from emails. It uses the Boost.Regex library, which has a separately-compiled binary component.
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
There are two main challenges associated with linking:
- Tool configuration, e.g. choosing command-line options or IDE build settings.
- Identifying the library binary, among all the build variants, whose compile configuration is compatible with the rest of your project.
Auto-Linking
Most Windows compilers and linkers have so-called “auto-linking support,” which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search. The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use.
6.1 Link From Within the Visual Studio IDE
Starting with the header-only example project we created earlier:
- Right-click example in the Solution Explorer pane and select Properties from the resulting pop-up menu.
- In Configuration Properties > Linker > Additional Library Directories, enter the path to the Boost binaries, e.g. C:\Program Files\boost\boost_1_67_0\lib\.
- From the Build menu, select Build Solution.
6.2 Or,Link From the Command Prompt
For example, we can compile and link the above program from the Visual C++ command-line by simply adding the bold text below to the command line we used earlier, assuming your Boost binaries are in C:\Program Files\boost\boost_1_67_0\lib:
cl /EHsc /I path\to\boost_1_67_0 example.cpp /link /LIBPATH:C:\Program Files\boost\boost_1_67_0\lib
6.3 Library Naming
Note
If, like Visual C++, your compiler supports auto-linking, you can probably skip to the next step.
In order to choose the right binary for your build configuration you need to know how Boost binaries are named. Each library filename is composed of a common sequence of elements that describe how it was built. For example, libboost_regex-vc71-mt-d-x86-1_34.lib
can be broken down into the following elements:
lib Prefix: except on Microsoft Windows, every Boost library name begins with this string. On Windows, only ordinary static libraries use the lib prefix; import libraries and DLLs do not.
boost_regex Library name: all boost library filenames begin with boost_.
-vc71 Toolset tag: identifies the toolset and version used to build the binary.
-mt
Threading tag: indicates that the library was built with multithreading support enabled. Libraries built without multithreading support can be identified by the absence of -mt
.
-d ABI tag: encodes details that affect the library's interoperability with other compiled code. For each such feature, a single letter is added to the tag:
Key Use this library when: Boost.Build option s linking statically to the C++ standard library and compiler runtime support libraries. runtime-link=static g using debug versions of the standard and runtime support libraries. runtime-debugging=on y using a special debug build of Python. python-debugging=on d building a debug version of your code. variant=debug p using the STLPort standard library rather than the default one supplied with your compiler. stdlib=stlport For example, if you build a debug version of your code for use with debug versions of the static runtime library and the STLPort standard library, the tag would be: -sgdp. If none of the above apply, the ABI tag is ommitted.
-x86 Architecture and address model tag: in the first letter, encodes the architecture as follows:
Key Architecture Boost.Build option x x85-32,x86-64 architecture=x86 a ARM architecture=arm i IA-64 architecture=ia64 s Sparc architecture=sparc m MIPS/SGI architecture=mips* p RS/6000 & PowerPC architecture=power The two digits following the letter encode the address model an follows:
Key Address model Boost.Build option 32 32 bit address-model=32 64 64 bit address-model=64
-1_34 Version tag: the full Boost release number, with periods replaced by underscores. For example, version 1.31.1 would be tagged as "-1_31_1".
.lib
Extension: determined according to the operating system's usual convention. On most unix-style platforms the extensions are .a
and .so
for static libraries (archives) and shared libraries, respectively. On Windows, .dll
indicates a shared library and .lib indicates a static or import library. Where supported by toolsets on unix variants, a full version extension is added (e.g. ".so.1.34") and a symbolic link to the library file, named without the trailing version number, will also be created.
6.4 Test Your Program
To test our subject extraction, we'll filter the following text file. Copy it out of your browser and save it as jayne.txt:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
Now, in a command prompt window, type:
path\to\compiled\example < path\to\jayne.txt
The program should respond with the email subject, “Will Success Spoil Rock Hunter?”
7 Conclusion and Further Resources
This concludes your introduction to Boost and to integrating it with your programs. As you start using Boost in earnest, there are surely a few additional points you'll wish we had covered. One day we may have a “Book 2 in the Getting Started series” that addresses them. Until then, we suggest you pursue the following resources. If you can't find what you need, or there's anything we can do to make this document clearer, please post it to the Boost Users' mailing list.
- Boost.Build reference manual
- Boost Users' mailing list
- Boost.Build mailing list
- Index of all Boost library documentation
Onward
Good luck, and have fun!
—the Boost Developers