C++ - Boost - Setting up on Windows 10

Boost libraries are some of the famous ones in the C++ world.

They contain tons of functionalities but aren't so easy to use or even understand.

First things first, let's try to install it in on our dear Windows 10 operating system (it should work as well on Windows 11).

And test it with Visual Studio 2017 and Visual Studio 2019.

Ready? Let's do it in this Boost installation tutorial for Windows 10.

First of all

We are going to set up our computer in order to use the following libraries and software:

Note the 14.1 version what is corresponding to MSVC 141 in order to be used with MSVC 2017.

For the MSVC 2019, please do the same but with the 14.2 version what is related to the MSVC 142 and download the Boost 1.77.0 version.

Setting up Boost libraries

Downloading and installing Boost libraries

The Boost libraries are in general available directly without building anything (only by including headers).

But some libraries have to be built before being able to use them.

That's the case for example for the Boost.Python or Boost.Regex libraries.

It's possible to build these libraries by yourself but to be honest the Boost documentation isn't really crystal clear and some links are even broken.

So to avoid a headache from the sky we are going to use the binaries directly downloaded from SourceForge (link above).

This executable isn't really an installation but more an extraction.
 

Indeed nothing will be changed on your Windows registries, only a new directory will be created with some files within.

So, once the executable donwloaded, Windows will show you a blue window with a message alerting that this is an unrecognized app.

Click on More info at the end of this message to make the Run anyway push button appear.

For MVSC 2017, install it in the following location:

  • C:\soft\boost_1_71_0

For MVSC 2019, install it in the following location:

  • C:\soft\boost_1_77_0

In this directory you can now see another one called lib64-msvc-14.1/ (for MSVC 2017) or  lib64-msvc-14.2 (for MSVC 2019) in which there are built libraries, all that in an x64 environment.

Building the Boost.Build (b2) engine (optional feature for this tutorial)

If you want to build libraries from Boost on your own way you'll have to use the Boost.Build tool.

It was in the past called bjam but it's now called b2.

So these 2 tools are exactly the same but the latter is the new version used with Boost.Build.

When you download Boost from the official website, b2 isn't installed.

You have to do it by yourself.

Actually it's quite easy.

Open a console from the Boost directory (depending on your MSVC version 141 or 142):

  • C:\soft\boost_1_71_0

or

  • C:\soft\boost_1_77_0

Then type the following command:

.\bootstrap.bat

And you'll have on your console after few seconds this message:

PS C:\soft\boost_1_71_0> .\bootstrap.bat

Building Boost.Build engine

Generating Boost.Build configuration in project-config.jam for msvc...

Bootstrapping is done. To build, run:

    .\b2

To adjust configuration, edit 'project-config.jam'.

Further information:

    - Command line help:

    .\b2 --help

    - Getting started guide:

    http://boost.org/more/getting_started/windows.html

    - Boost.Build documentation:

    http://www.boost.org/build/

In the same directory you can now see the b2.exe tool.

We don't need it for now, just let it for the future.

Environment variables for Boost

We have to set our Environment variables.

So add the 2 following paths for MSVC 2017:

  • C:\soft\boost_1_71_0\
  • C:\soft\boost_1_71_0\lib64-msvc-14.1\

And for MSVC 2019:

  • C:\soft\boost_1_77_0\
  • C:\soft\boost_1_77_0\lib64-msvc-14.2\

If you are using Windows 10 it's not so unusual to use Visual Studio as well.

So let's set it up to use Boost.

Setting up Visual Studio

Visual Studio platform toolset

If your Visual Studio instance was already opened then close it and open it again to get the new Environment variable modifications.

We are going to use Visual Studio 2017 so the binaries version are 14.1.

Or the Visual Studio 2019 with binaries 14.2.

Indeed, each version of Visual Studio has its own platform toolset.

For example:

  • Visual Studio 2017 has a platform toolset 14.1 (often msvc-141)
  • Visual Studio 2019 has a platform toolset 14.2 (often msvc-142)

Yes it's quite vague and not so logical but Miscrosoft is Microsoft.

So download the right one for your Visual Studio version, otherwise it won't work.

Anyway let's set up all that tools.

I'll let you install Visual Studio where you want.

We'll just have to set the include and library directories directly from Visual Studio.

Let's start by creating a Windows Console Application from Visual Studio.

From Visual Studio > File > New Project > Installed > Visual C++ > Windows Desktop > Windows Console Application.

Let's name this project for example like this:

  • Name: BadprogTutorial
  • Location: C:\dev\c++\boost\

Setting the Visual Studio project platform

First thing is now to change the project platform in the Configuration manager:

From Visual Studio > Select the BadprogTutorial.cpp file >  Project > BadprogTutorial Properties... > On upper right click the Configuration Mananger... push button > Change Active solution platform from x86 to x64 > Close.

Then still in the BadprogTutorial Property Pages, at top center, change the platform from Win32 to x64.

Then click OK.

You can stay in Debug mode or change it to Release (it won't change anything for our tutorial).

Setting Visual Studio include paths for Boost

First let's set the includes.

From Visual Studio > Select the BadprogTutorial.cpp file > Project BadprogTutorial Properties... > Configuration Properties > C/C++ > General Additional Include Directories > Edit > Add the following line:

  • C:\soft\boost_1_71_0\

or

  • C:\soft\boost_1_77_0\
Then OK > Apply > OK.
 

Setting Visual Studio library paths for Boost

We've now to set the libraries.

Same thing but with the linker:

From Visual Studio > Select the BadprogTutorial.cpp file > Project BadprogTutorial Properties... > Configuration Properties > Linker > General Additional Library Directories > Edit > Add the following line:

  • C:\soft\boost_1_71_0\lib64-msvc-14.1\

or

  • C:\soft\boost_1_77_0\lib64-msvc-14.2\

Then OK > Apply > OK.

Let's code a bit

In our BadprogTutorial.cpp file let's add the following code:

BadprogTutorial.cpp

// badprog.com

#include "pch.h"

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

//
namespace bg = boost::gregorian;

// ----------------------------------------------------------------------------
//
// ----------------------------------------------------------------------------
int main() {
  // date as simple ISO string
  std::string stringDate("20191110");
  bg::date todayDate(bg::from_undelimited_string(stringDate));

  // 
  bg::date::ymd_type ymdDate    = todayDate.year_month_day();
  bg::greg_weekday weekdayDate  = todayDate.day_of_week();

  //
  std::cout 
    << "Tutorial made with love from Badprog.com :D on "
    << weekdayDate.as_long_string() << " "
    << ymdDate.day << " "
    << ymdDate.month.as_long_string() << " "
    << ymdDate.year
    << std::endl;
}

Build and run

Build and run your program, you should see the following appears on your console:

Tutorial made with love from Badprog.com :D on Sunday 10 November 2019

Conclusion

Now that you have installed Boost, plenty of libraries are now available for you.

A lot of fun for the future.

Good job, you did it. cool

Comments

Comment: 

Is this tutorial still up to date? I've followed the instructions perfectly as far as I can tell and am getting a whole slew of errors in Visual Studio. Please let me know if something has changed. Thanks.

Comment: 

Which version of Visual Studio and Boost are you using?

What are the errors?

Comment: 

I followed your instructions and it was great!
I used VS2019 and msvc-142. Thank you!!

Comment: 

Is there a way to have B2 use C++23 as the compile standard? I'm passing in the arg to b2: (cxxflags="-std=c++23") without the parentheses. However I see a lot of cxx11 flags printed to screen when I enter the b2 build command.

I'm very new to C++, so maybe I'm fundamentally misunderstanding something about how B2 and Boost work here. Environment is Windows 10, using VS2022, bootstrap command is "bootstrap vc143".

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.