Boost C++ Library¶
Boost is a collection of free peer-reviewed portable C++ source libraries, and is available as a module on Apocrita.
Using Boost¶
The Apocrita cluster supports Boost, and the available versions can be
determined by calling module avail boost. The default version of the module can
be loaded on Apocrita by calling:
module load boost
Loading of the module will set unique environment variables, which can be determined by calling:
module show boost
Building a simple program using Boost¶
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. They only have to be included in the source code and should work when the program is recompiled.
A sample program that utilises a header-only library is given as an example (taken
from the Getting started guide). The 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) << " " );
}
Following the loading of the Boost module, copy the sample program into
a file called example1.cpp and compile with:
module load boost
g++ example1.cpp -o example
Separately compiled Boost libraries¶
Some of the Boost libraries have to be built separately. However, they are already provided on Apocrita. These are:
- Boost.Chrono
- Boost.Context
- Boost.Filesystem
- Boost.GraphParallel
- Boost.IOStreams
- Boost.Locale
- Boost.Log
- Boost.MPI
- Boost.ProgramOptions
- Boost.Python
- Boost.Regex
- Boost.Serialization
- Boost.Signals
- Boost.System
- Boost.Thread
- Boost.Timer
- Boost.Wave
Some libraries have a varying set of features when they are used as header-only or are separately-compiled. Where applicable, they are provided already compiled:
- Boost.DateTime
has a binary component that is only needed if you're
using its
to_string/from_stringor 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
cmathfunctions. - Boost.Random has a binary component which is only needed if you're using
random_device. - Boost.Test
- Boost.Exception
Linking to a Boost library¶
The following sample program uses the Boost.Regex
library (taken from the Getting started guide), which has a
separate binary component that needs to be linked against. The program
extracts the subject lines from emails.
#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;
}
}
To link and compile the sample program example2.cpp with the use of the
Boost.Regex library, specify -lboost_regex.
g++ example2.cpp -o example2 -lboost_regex
Generally, the required library name is called with -l<library>,
dropping the filename's leading lib and trailing suffix
(.a for a static or .so for a dynamic library).
Explicit rules on how the Boost libraries are named can be found here.
Documentation¶
Getting Started guide on Unix Variants