The Boost Template Library

One of the most popular add-on libraries for C++, especially numerical or scientific programming, is Boost. Boost is not included with any compilers, but is generally easy to obtain. For Linux systems generally it is available through the package manager.

Installing Boost

Boost uses its own build system on Unix and Mac OS. On Windows there is an experimental CMake system but generally the “bjam” builder is still used. In all cases, installing Boost requires some familiarity with using a command line.

Unix and Mac OS

On a Linux system, the simplest way to install Boost is to utilize the package manager of the distribution. For example on Ubunutu the command is

sudo apt install libboost-all-dev

or on Fedora (or Centos 8 and beyond)

sudo dnf install boost
sudo dnf install boost-devel

If you do not have superuser (sudo) permission or you wish to install Boost somewhere other than the main system libraries, follow the general instructions. The default prefix for installation from b2 install is /usr/local. This is true for both Linux and Mac OS. If you wish to install to /usr/local, which is normally in the system search paths, you will need to run the installation command with sudo

cd path/to/boost/source
sudo ./b2 install

If you do not have sudo privileges or you wish to install it someplace else, keep in mind that this will affect how you reference the headers and libraries with -I and -L. You must provide the prefix to the installation directory to the script to install someplace other than the default location.

./ --prefix=/home/mst3k/boost
./b2 install


Installation on Windows is somewhat more complicated than on Unix variants. First download the zipped source file.

  1. Start a command window. If you wish to install Boost in a standard search path, you will need to run it as administrator.
  2. Unzip the .zip file into a folder of your choice C:\yourpath. It will unpack to a folder with a version number, which may vary from our example C:\yourpath\boost_1_76_0
  3. Create some folders, e.g. mkdir C:\boost-build mkdir C:\yourpath\boost_1_76_0\boost-build mkdir C:\boost
  4. From the command prompt, `cd C:\yourpath\boost_1_76_0\tools\build
  5. Run boostrap.bat gcc
  6. Install the build system with b2 --prefix="C:\boost-build" install
  7. Modify your session PATH with set PATH=%PATH%;C:\boost-build\bin
  8. Return to your source directory cd C:\yourpath\boost_1_76_0
  9. Build with b2 --build-dir="C:\install\boost --prefix="C:\boost" --build-type=complete toolset=gcc install
  10. This will install to “C:\boost\include\boost-1_76_0” and “C:\boost\lib” You may remove temporary unpacking and build directories if you wish. You may also move the header files up to C:\boost\include if you prefer. Remember that <boost/boostheader.hpp> will use -I to start looking for that subdirectory.

Using Boost is probably simplest with a Makefile. The example is for Windows with a particular choice of location for the boost header files and libraries. Change the locations for -I and -L as appropriate if they are located elsewhere on your system. Please refer to the earlier chapter for a review of setting up Makefiles. This example goes with a standard Boost example.

If you have installed Boost onto a Linux or Mac OS system to a system default search location such as /usr or /usr/local you will not need to specify the -I or -L paths at all. The example makefile assumes installation in system paths in the compiler’s default search paths.

Boost MultiArrays

C-style arrays are widely used but lack some important features, such as the ability to check that index references do not occur outside of the arrays bounds. The C++ STL array type has more features, but is limited to one dimension. A workaround is to “stack” vectors, since a vector of a vector amounts to a 2-dimensional structure.

#include <vector>
using namespace std;

   vector<vector<double>> A;

The Boost library provides a popular alternative, the MultiArray. This structure can be N-dimensional, its bounds (extents) can be checked, and it can be reshaped and resized. The price is that it can be slow.

#include <iostream>
#include <ctime>
#include <boost/multi_array.hpp>

using namespace std;

int main(int argc, char*argv[])


    int nrows;
    int ncols;

//  Iterations are to make the time measurable
    const int ITERATIONS = 1000;

    time_t startTime,endTime;

// Set the array dimensions

// Create the boost array
    typedef boost::multi_array<double, 2> Array2D;
    Array2D boostArray(boost::extents[nrows][ncols]);

// Create the C array
    double **C_Array;

//------------------Measure boost----------------------------------------------

    startTime= time(NULL);

    for (int i = 0; i < ITERATIONS; ++i) {
        for (int x = 0; x <nrows; ++x) {
            for (int y = 0; y <ncols; ++y) {
                boostArray[x][y] = 2.345;

    endTime= time(NULL);

    cout<<"[Boost] Elapsed time: "<<(endTime-startTime)/1000.0<<" sec\n";
//------------------Measure native---------------------------------------------

    C_Array= new double* [nrows];
    for (int i=0;i<nrows;++i) {
        C_Array[i]=new double[ncols];

    startTime= time(NULL);

    for (int i = 0; i < ITERATIONS; ++i) {
        for (int x = 0; x <nrows; ++x) {
            for (int y = 0; y <ncols; ++y) {
                C_Array[x][y] = 2.345;

    endTime= time(NULL);

    cout<<"[C style] Elapsed time: "<<(endTime-startTime)/1000.0<<" sec\n";

    return 0;


If you have succeeded in installing Boost, or you have access to a system where it has been installed, download and run the above boost_array.cxx program.