在Windows上安装Boost

官方文档:Getting Started on Windows

Boost里面有很多库,分为需要编译的和不需要编译的(header file only)。

如果只需要使用不需要编译的库(如Boost.PropertyTree)则只需要从官方网站下载源代码解压缩即可使用。Boost官网上的Downloads->Latest Rease->Version x.xx.x 就是下载链接。解压后放到一个目录,比如D:\boost盘。这里以1.61.0版本为例,则安装目录为D:\boost\boost_1_61_0。在Visual Studio 的项目Properties里面的VC++ Directories->Include Directories 里面添加条目D:\boost\boost_1_61_0即可完成编译。可以发现D:\boost\boost_1_61_0\目录是空的,因为官方发布的source pack里面是没有预编译的。

如果需要使用需要编译的库,如 Boost.FileSystem或者Boost.ProgramOptions,则还需要编译这些库。Boost目录(D:\boost\boost_1_61_0)里面的booststrap.bat脚本就是编译脚本。这里我们参照这篇博文使用Visual Studio 2015来编译boost_1_61_0。

  1. D:\boost目录里面创建脚本build_boost_1_61_0_vs2015.bat,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" x86
    set cores=%NUMBER_OF_PROCESSORS%
    echo Building boost with %cores% cores
    cd boost_1_61_0
    call bootstrap.bat
    rem Most libraries can be static libs
    b2 -j%cores% toolset=msvc-14.0 address-model=64 architecture=x86 link=static threading=multi runtime-link=shared --build-type=minimal stage --stagedir=stage/x64
    b2 -j%cores% toolset=msvc-14.0 address-model=32 architecture=x86 link=static threading=multi runtime-link=shared --build-type=minimal stage --stagedir=stage/win32
    pause

    这里是设置使用Visual Studio (msvc-14.0) toolset编译32位和64位的静态库文件,并且设置了生成的库文件位置为stage目录。更详细的编译选项可以参考官方文档。

  2. 打开cmd,进入D:\boost目录,执行builde_boost_1_61_0_vs2015.bat脚本,等待编译完成。编译完成后回提示 Press any key to continue。编译生成的库文件在stage目录下面。

  3. 为了区分不同编译器生成的库文件,在D:\boost\boost_1_61_0目录下面新建lib64-msvc-14.0目录,表示是Visual Studio 2015编译的。再把stage/x64/*.lib 拷贝到这个新建的目录下面。

因为编译过程比较耗时,可以考虑直接下载Boost Binaries Installer For Windows。下载安装之后,里面直接提供了lib64-msvc-14.0等目录。