FAQ: Boost on HPC Systems

There are boost modules available on our systems. But they might not be adequate because of the wrong version, or incompleteness of features. Or, you may just want to use a different compiler.

Recipe to build and use it

  1. Download the desired boost version from https://www.boost.org/  Or, if internet is available: 
    git clone --recurse-submodules https://github.com/boostorg/boost.git
    cd boost
    git tag                           # select the desired tag
    git checkout boost-1.88.0         # for instance
  2. Load the compiler and MPI modules (if MPI is required; if not, skip it in the following); can be gcc, intel/oneapi/llvm and Intel MPI, MPICH, OpenMPI
    Just go sure that your compiler supports reasonably a modern C++ standard!
    Load a relatively new cmake!
  3. Configure (here, we do that using gcc/13.2.0 and intel-mpi/2021.10.0):
    ./bootstrap.sh --with-toolset=gcc --with-libraries=atomic,chrono,context,date_time,exception,fiber,filesystem,headers,iostreams,locale,log,math,nowide,program_options,random,regex,serialization,stacktrace,system,test,thread,timer,type_erasure

    Check boost's documentation for available toolsets (clang, intel-linux, gcc, for instance) and for available libraries (also check the libs sub-directory in boost's sources). Of course, you don't need to install all of them! Just what you need! We just gave some examples here.
    With that, the b2 tool is build, required for building and installing boost.
    Remark: The tool chain has nothing to do which compiler you later want to use. It is solely about with which compiler to build boost. So, system gcc is most probably fine for all cases unless you desire some higher C++ standard later maybe not supported by this compiler.
    Remark: One can execute ./bootsrap.sh also without any option. Then, only the b2 tool is built. Using ./b2 --show-libraries reveals all available libraries that can be build in boost.
  4. Installing boost:
    ./b2 --prefix=<boost-install-path>/boost_1.88.0 cxxflags="-std=c++17" variant=release,debug link=shared threading=multi runtime-link=shared address-model=64 -j 20 install

Both, bootstrap and b2 have both --help option and online documentation.


Alternatives to this exist. For instance, bootstrapy.sh can be executed without any options first. It then takes the system gcc to build b2. Then, with some compiler module loaded, one can execute b2, limiting the set of needed libraries and other settings. For instance, for Intel OneAPI compiler, and just wanting filesystem, it goes like this,

> ./b2 --prefix=$HOME/boost-1.88 --with-filesystem cxxstd=17  toolset=intel-linux  link=shared address-model=64 architecture=x86 runtime-link=shared

to install boost and only filesystem library into $HOME/boost-1.88.

Usage


The simples way to use boost libraries in own C++ projects is through CMake's find_package. An example is given here. Again, only the needed parts need be included.

For using your just installed boost library, direct both LD_LIBRARY_PATH and CMAKE_PREFIX_PATH to the lib and lib/cmake path, respectively. <boost-install-path>/boost_1.88.0/lib and <boost-install-path>/boost_1.88.0/lib/cmake in our example. CMake's find_package should then find everything.

For filesystem boost module installed, a minimum example looks like this.

CMakeLists.txt
cmake_minimum_required (VERSION 3.20)
project (test LANGUAGES CXX VERSION 1.0.0)
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS False)

find_package(Boost 1.80.0 REQUIRED COMPONENTS filesystem)

add_executable(test test.cxx)
target_link_libraries(test PUBLIC Boost::filesystem)
test.cxx
#include <boost/filesystem.hpp>
#include <iostream>

int main(int argc, char* argv[]) {
  using namespace boost::filesystem;
  std::cout << argv[0] << " " << file_size(argv[0]) << '\n';
}

Building is simple. With LD_LIBRARY_PATH and CMAKE_PREFIX_PATH set as noted above, and cmake and some compiler (better the same boost was build with) module loaded, it goes like that.

> cmake -S . -B build
> cmake --build build

The executable, test, is then in the build directory.


If the boost compiler and the currently used compiler differ, one usually needs to use Boost_COMPILER and CMAKE_CXX_COMPILER_ARCHITECTURE_ID CMake variables during the configure step. In order to avoid this compilcation, installing boost with the same compile as one intends to use for the own application is advisable.

Remarks

  • Boost seems to contain also a CMakeLists.txt. That may simplify boost's installation procedure further.
  • Note that boost is permanently developing. And many parts are also migrated already into the C++ STL (and standard library). That is each version of boost may possess different features.
    Though not all compilers support all of that, like Intel OneAPI C++ compiler suite. Boost is a good alternative in those cases.