boost

Getting started with boost

Remarks#

What is Boost?

Boost is a large collection of free, high quality C++ libraries that cover a broad range of topics. It is often considered a “second standard library” for C++, since many common problems in C++ are solved by using Boost.

From boost.org:

Boost provides free peer-reviewed portable C++ source libraries.

We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use.

Some Boost libraries have even made their way into the C++11 standard library, and some other, such as Boost.Optional and Boost.Variant, will be a part of C++17.

What can Boost do?

Boost covers most corners of programming. From the boost tag wiki here on Stack Overflow:

It includes libraries for:

  • String and text processing
  • Containers
  • Iterators
  • Algorithms
  • Function objects and higher-order programming
  • Generic Programming
  • Template Metaprogramming
  • Preprocessor Metaprogramming
  • Concurrent Programming
  • Math and numerics
  • Correctness and testing
  • Data structures
  • Image processing
  • Input/Output
  • Inter-language support
  • Memory
  • Parsing
  • Programming Interfaces
  • Miscellaneous
  • Broken compiler workarounds

Versions#

VersionNew LibrariesRelease NotesRelease Date
1.10.0notes1999-12-14
1.11.0Rational Numbernotes2000-02-01
1.12.0notes2000-02-23
1.13.0Utility, Type Traits, Call Traits, Compressed Pairnotes2000-02-29
1.14.0notes2000-03-05
1.15.0Random Numbernotes2000-06-17
1.16.0Functional, iterator headernotes2000-06-28
1.17.0Arraynotes2000-08-03
1.18.0Graph, Regular Expressionnotes2000-09-28
1.19.0Concept Check, Python, Static Assert,
Property Map Concepts
notes2000-12-10
1.20.0Conversionnotes2001-01-06
1.21.0Iterator Adaptor, Pool, Testnotes2001-03-09
1.22.0CRCnotes2001-05-25
1.23.0Any, Function, Tokenizer,
Special Functions, Octonions, Quaternions
notes2001-07-06
1.24.0Tuplenotes2001-08-19
1.25.0Thread, Bindnotes2001-10-01
1.26.0Common Factor, Preprocessornotes2001-11-30
1.27.0notes2002-02-05
1.28.0Lambda, I/O State Savernotes2002-05-15
1.29.0Date-Time, Dynamic Bitset, Formatnotes2002-10-10
1.30.0Filesystem, Optional, Interval, MPL, Spiritnotes2003-03-19
1.31.0enable_if, Variantnotes2004-01-26
1.32.0Assignment, Minmax, Multi-Index,
Numeric Conversion, Program Options, Range,
Serialization, String, Tribool
notes2004-11-19
1.33.0Iostream, Hash, Parameter,
Pointer Container, Wave
notes2005-08-11
1.34.0Foreach, Statechart, TR1, Typeof, Xpressivenotes2007-05-12
1.35.0Asio, Bimap, Circular Buffer, Function Types,
Fusion, GIL, Interprocess, Intrusive,
Math/Special Functions,
Math/Statistical Distributions, MPI, System
notes2008-03-29
1.36.0Accumulators, Exception, Units, Unorderednotes2008-08-14
1.37.0Protonotes2008-11-03
1.38.0Flyweight, ScopeExit, Swapnotes2009-02-08
1.39.0Signals2notes2009-05-02
1.40.0notes2009-08-27
1.41.0Property Treenotes2009-11-17
1.42.0Uuidnotes2010-02-02
1.43.0Functional/Factor, Functional/Forwardnotes2010-05-06
1.44.0Meta State Machine, Polygonnotes2010-08-13
1.45.0notes2010-11-19
1.46.0Iclnotes2011-02-21
1.46.1notes2011-03-12
1.47.0Chrono, Geometry, Phoenix, Rationotes2011-07-11
1.48.0Container, Locale, Movenotes2011-11-15
1.49.0Heapnotes2012-02-24
1.50.0Algorithm, Functional/OverloadedFunction,
LocalFunction, Utility/IdentityType
notes2012-06-28
1.51.0Contextnotes2012-08-20
1.52.0notes2012-11-05
1.53.0Atomic, Coroutine, Lockfree, Multiprecision,
Odeint
notes2013-02-04
1.54.0Log, TTI, Type Erasurenotes2013-07-01
1.55.0Predefnotes2013-11-11
1.56.0Align, TypeIndexnotes2014-08-07
1.57.0notes2014-11-03
1.58.0Endian, Sortnotes2015-04-17
1.59.0Convert, Coroutine2notes2015-08-13
1.60.0VMDnotes2015-12-17
1.61.0Compute, DLL, Hana, Metaparsenotes2016-05-13
1.62.0Fiber, QVMnotes2016-09-28
1.63.0notes2016-12-26
1.64.0Processnotes2017-04-19

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 www.boost.org
  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)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(YourTarget ${SOURCE_FILES})
    target_link_libraries(your_target ${Boost_LIBRARIES} -missing_libs)        
endif()

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)
project(your_project)

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)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(your_project ${SOURCE_FILES})
    target_link_libraries(your_project ${Boost_LIBRARIES})
endif()

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow