Getting started with boost

Installation or Setup

See Boost Getting Started.

Most of the Boost libraries are header-only, meaning that there’s nothing you have to compile or link to.

Make sure you are getting the most recent version of Boost:

  1. Visit
  2. Look for the Current Release download. Currently, this links here. Shows where on the boost website you can download Boost
  3. Select the appropriate archive file for your operating system, and download.

Header-only libraries can then be used by simply including the respective header files.

A few Boost libraries require compilation:

  • Boost.Chrono
  • Boost.Context
  • Boost.Filesystem
  • Boost.GraphParallel
  • Boost.IOStreams
  • Boost.Locale
  • Boost.MPI
  • Boost.ProgramOptions
  • Boost.Python
  • Boost.Regex
  • Boost.Serialization
  • Boost.Signals
  • Boost.System
  • Boost.Thread
  • Boost.Timer
  • Boost.Wave

Also, the following libraries have components which must be compiled:

  • Boost.DateTime

  • Boost.Graph

  • Boost.Math

  • Boost.Random

  • Boost.Test

  • Boost.Exception

The source for Boost can be obtained through the download link on the site, which will re-direct to its SourceForge page for the latest version (1.61.0 as of July 2016). This can be unzipped (or un-tared, etc) to a directory (such as C:\local\boost_1_61_0). This directory can then be added to the include path for the software you are building. After this, you can include Boost headers in C++ files with #include <boost/header/path.hpp>.

The majority of the libraries in Boost are header-only. If you only need these then the above source distribution is all that is needed. However, if you need to use one of the libraries that requires a compiled binary to be built, you will need that as well.

On any system, the most reliable way to get the correct binaries is to build them yourself. These directions are somewhat different for Windows or Linux/Unix/POSIX.

On Windows with Visual Studio, an alternative to building the libraries yourself is to download pre-built libraries from Boost’s SourceForge page (1.61.0 as of July 2016). On that page you can select an installer that will install a version for a specific Visual Studio build or the 7-zip file (boost_X_XX_X-bin-all-32-64.7z) that contains the binaries for all the supported Visual Studio versions. Either of these options includes the source/headers as well as the binaries, so there is no need to have downloaded the source distribution above. Once you have it, extract/install to a directory (such as C:\local\boost_1_61_0) and add that directory to your include path, then add the directory containing the binaries that correspond to your version of Visual Studio (e.g. C:\local\boost_1_61_0\lib32-msvc-12.0 for Visual Studio 2013 32-bit projects) to the library path.

Installing and Running Boost (Cygwin)

(Beginner level; IDE: CLion)

First, install boost from the Cygwin mirror: open the install exe, search for boost, install the packages.

After boost is installed: it will be located in /usr/include/boost. This is where everything is. All #include statements will be a path from the boost folder, as in: #include <boost/archive/text_oarchive.hpp>.

Once you include the boost files of your choice in your .cpp files, your code will still not compile in your IDE of choice until you link the libraries and tell cmake to search for your downloaded boost code.

In order to get cmake to search for your boost code,

find_package(Boost 1.60.0 COMPONENTS components_you_want)

# for example: 
find_package(Boost 1.60.0 COMPONENTS serialization)

Then, include the directories: include_directories(${Boost_INCLUDE_DIRS})

Finally, add your executable and link the libraries:

add_executable(your_target ${SOURCE_FILES})
target_link_libraries(your_target ${Boost_LIBRARIES} -any_missing_boostlibs)

Before starting your program, avoid an error dump by testing to see if boost has been found before including anything or running your code:

if (Boost_FOUND)
    add_executable(YourTarget ${SOURCE_FILES})
    target_link_libraries(your_target ${Boost_LIBRARIES} -missing_libs)        

I included -missing_libs because an error you may run into is that some boost library or another might not have been linked, and you must manually add it–for instance, the link I referenced earlier.

All together, a final CMakeLists.txt file might look something like:

cmake_minimum_required(VERSION 3.7)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp tmap.cpp tmap.h)
find_package(Boost 1.60.0 COMPONENTS serialization)

    add_executable(your_project ${SOURCE_FILES})
    target_link_libraries(your_project ${Boost_LIBRARIES})