编译 boost 1.85.0
2025-07-01 20:55:10

第一步,下载源码:https://archives.boost.io/release/
解压源码后双击运行bootstrap.bat,它会自动检测本机可用的编译器,并编译出b2.exe文件,后面的编译都需要靠它。

#编译(stage)

boost 中大多数库都是仅头文件的,不需要编译,直接包含到项目中就能用。
当然也并不是所有库都是仅头文件的,可以使用--show-libraries选项查看当前版本中所有需要编译才能使用的库。
1.85.0 版中的库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
>b2 --show-libraries
The following libraries require building:
- atomic
- charconv
- chrono
- cobalt
- container
- context
- contract
- coroutine
- date_time
- exception
- fiber
- filesystem
- graph
- graph_parallel
- headers
- iostreams
- json
- locale
- log
- math
- mpi
- nowide
- program_options
- python
- random
- regex
- serialization
- stacktrace
- system
- test
- thread
- timer
- type_erasure
- url
- wave

一些主要的编译选项:

选项 默认值 可选值 说明
–build-dir ./bin.v2 编译产生的临时文件位置
–stagedir ./stage 存放编译后库文件的位置
–toolset 指定工具链,不写时自动查找。支持参数可以查看bootstrap.bat文件
link static|shared static、shared 编译静态库或动态库
runtime-link shared static、shared 静态或动态链接 C++ 运行时库
address-model 32|64 32、64 编译成 32 位版本还是 64 位版本
threading multi single、multi 要编译的库是单线程还是多线程
variant debug|release debug、release 编译 debug 版或 release 版
–with 仅编译哪些库,其他的排除
–without 不编译哪些库

一般编译搞清楚这几个选项就够了,更多选项可以使用b2 --help查看。
一个例子:

1
b2 --toolset=msvc-14.1 --with-program_options link=static runtime-link=static

这条配置仅编译program_options静态库,并且静态链接运行时库。

#安装(install)

b2 分为编译安装两种方式,对应的选项就是stageinstall,默认值是stage

1
b2 ... install

如果指定了install,则会使用--prefix--includedir--libdir这几个位置选项。
一般用不到,就不细说了。

#使用 bcp

我们用 boost 时基本只是用到其中的小部分,但是打包时不清楚哪些头文件是没有用到的,所以会将所有头文件都拷贝到项目中。
bcp 就是解决这个问题的,可以用它将仅用到的头文件提取出来。

编译:

1
b2 tools\bcp

注意,1.85 版中的 bcp 源码有错误,无法编译。到 https://github.com/boostorg/bcp/tree/develop 下载一份最新的代码覆盖到tools\bcp目录下再编译。


如果你用的是最新的 VS2022 也会无法编译,因为当前的 b2 版本不支持最新的编译器vc144,要使用低版本的编译器编译。


使用:

1
2
mkdir output
dist\bin\bcp filesystem.hpp output

这样就能将filesystem.hpp及其相关的文件提取到output目录,然后把头文件拷贝到项目中即可。

#相关阅读

Getting Started on Windows
windows 编译C++ boost库(超详细)