How to compile BOOST with MS Visual Studio 2010-2017

Fortunately, to compile BOOST with MS VC 2010 we need Visual Studio Command Prompt and five commands:

Navigate to BOOST directory, for example:

F:
cd F:\Projects\Lib\boost_1_53_0

Build bjam that is used to build BOOST:

bootstrap.bat

Build BOOST debug and release static libraries:

b2.exe --toolset=msvc-10.0 link=static runtime-link=static variant=debug stage
b2.exe --toolset=msvc-10.0 link=static runtime-link=static variant=release stage

After that BOOST lib files appears in stage\lib\ directory.

For building BOOST 1.66.0 with VS2017 I used the following bat file:

@echo off
rem Directory to boost root
set boost_dir=boost_1_66_0

rem Number of cores to use when building boost
set cores=%NUMBER_OF_PROCESSORS%

rem What toolset to use when building boost.

rem Visual Studio 2012 -> set msvcver=msvc-11.0
rem Visual Studio 2013 -> set msvcver=msvc-12.0
rem Visual Studio 2015 -> set msvcver=msvc-14.0
rem Visual Studio 2017 -> set msvcver=msvc-14.1

set msvcver=msvc-14.1

rem Start building boost
echo Building %boost_dir% with %cores% cores using toolset %msvcver%.

cd %boost_dir%
call bootstrap.bat

rem Most libraries can be static libs
b2 -j%cores% toolset=%msvcver% address-model=64 architecture=x86 link=static threading=multi runtime-link=static variant=debug stage --stagedir=stage/x64
b2 -j%cores% toolset=%msvcver% address-model=32 architecture=x86 link=static threading=multi runtime-link=static variant=debug stage --stagedir=stage/win32
b2 -j%cores% toolset=%msvcver% address-model=64 architecture=x86 link=static threading=multi runtime-link=static variant=release stage --stagedir=stage/x64
b2 -j%cores% toolset=%msvcver% address-model=32 architecture=x86 link=static threading=multi runtime-link=static variant=release stage --stagedir=stage/win32

and the following property sheet that I put to the same directory as BOOST and added to the project via View->Other Windows->Property Mangeer:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <PropSheetPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))</PropSheetPath>
  </PropertyGroup>
  <PropertyGroup />
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(PropSheetPath)boost_1_66_0\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(PropSheetPath)boost_1_66_0\stage\$(Platform)\lib\</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

4 Responses to How to compile BOOST with MS Visual Studio 2010-2017

  1. superadmin says:

    to build all the DLLs I used the following command
    b2.exe –toolset=msvc-11.0 link=shared

  2. superadmin says:

    You can force Boost to use the DLLs by defining BOOST_ALL_DYN_LINK – either in your C++ preprocessor settings or by a #define in your stdafx.h pre-compiled header, e.g.:

    #define BOOST_ALL_DYN_LINK

    see http://stackoverflow.com/questions/2520234/how-to-link-to-dynamic-boost-libs

    boost\config\user.hpp:

    // BOOST_ALL_DYN_LINK: Forces all libraries that have separate source,
    // to be linked as dll’s rather than static libraries on Microsoft Windows
    // (this macro is used to turn on __declspec(dllimport) modifiers, so that
    // the compiler knows which symbols to look for in a dll rather than in a
    // static library). Note that there may be some libraries that can only
    // be statically linked (Boost.Test for example) and others which may only
    // be dynamically linked (Boost.Threads for example), in these cases this
    // macro has no effect.
    // #define BOOST_ALL_DYN_LINK

  3. Jeff says:

    This was very helpful.

    To update it for Visual Studio 2022, I believe the msvcver variable should be set to “msvc-14.3”.

Leave a Reply

Your email address will not be published. Required fields are marked *