- Boost版本:1.79.0
- 编译器:VS2019(Windows)
资源下载和教程
历史版本下载:Boost Version History
Windows教程(1.75.0):Boost Getting Started on Windows - 1.75.0
Unix教程(1.75.0):Boost Getting Started on Unix Variants - 1.75.0
下载源码包并解压↓
安装工具预生成
打开“Developer Command Prompt for VS 2019”↓
进入boost源码目录并执行命令:
1 | d: |
运行完成后,源码目录下新增两个文件:b2.exe
和project-config.jam
b2.exe
是boost的安装工具,在命令行执行.\b2.exe --help
,可以查看使用帮助。
Boost库生成(安装)
使用b2.exe
进行库生成,可以指定以下内容
- 开发工具集(v142/v140/v100/…)
- 输出目录
- 所有或部分模块
- 静态或动态库
- Debug或Release版本
b2命令参数及作用
指定不同的构建目标
stage
/install
stage
只生成dll和lib;install
会生成包含目录include和cmake文件安装到系统中。使用方式及对应的参数如下:
1 | ./b2.exe stage |
--stagedir=<STAGEDIR>
指定输出目录,默认./stage
1 | ./b2.exe install |
--prefix=<PREFIX>
指定安装目录,Windows下默认C:\Boost
--exec-prefix=<EPREFIX>
架构依赖文件安装目录,一般不改,与--prefix
保持一致
--libdir=<LIBDIR>
库目录,默认<EPREFIX>/lib
--includedir=<HDRDIR>
头文件目录,默认<PREFIX>/include
--cmakedir=<CMAKEDIR>
CMake配置文件目录,默认<LIBDIR>/cmake
--no-cmake-config
设置以取消安装CMake配置文件
指定构建模式
--build-type=<type>
<type>
可选值:
minimal
- (默认)尽可能精简地构建。在Windows下,构建静态多线程库,包含debug和release模式,使用shared的运行时。complete
- 完整构建。
b2文档解释:
1 | --build-type=<type> Build the specified pre-defined set of variations of |
指定平台工具集
toolset=<toolset>
<toolset>
可选值:
msvc-14.2
- VS2019msvc-14.0
- VS2015msvc-10.0
- VS2010- … - 其他VS对应的平台工具集
gcc
- MingGW
指定构建的包
--with-<library>
包含xxx
--without-<library>
不包含xxx
不写就以--build-type
指定的构建。有哪些包需要构建可以通过--show-libraries
查看:
1 | PS D:\Boost\boost1.79.0\boost_1_79_0> ./b2.exe --show-libraries |
多线程与单线程版本
threading=single|multi
大部分情况下都用multi
,默认值,生成的库文件带有mt
。
只有很少的情况下使用single
,生成的库文件不带mt
标志。
debug和release
variant=debug|release
不指定则同时生成debug和release版本。二者区别在于:debug版本的库文件名带有gd
,release版本的不带。
link与runtime-link的配置:静态库与动态库,静态运行时与动态运行时
这里涉及到两个选项:link
与runtime-link
,两个选项的可选值都是static
和shared
,若不指定则会自动交叉编译。
两个参数有何区别?
link
指定生成的boost是动态库还是静态库。static
生成静态库,以libboost_
开头,只有*.lib
文件;shared
生成动态库,有*.dll
和*.lib
文件,均以boost_
开头。runtime-link
指定与C++运行时的链接方式。static
表示静态链接,调用系统库时采用静态方式建立连接,在VS工程中的配置为/MTd
或/MT
;shared
表示动态链接,对应/MDd
或/MD
。
link | runtime-link | VS配置 | 文件特点 |
---|---|---|---|
static | static | /MTd, /MT | libboost_ 开头,仅lib 文件,包含s |
static | shared | /MDd, /MD | libboost_ 开头,仅lib 文件,不包含s |
shared | shared | /MDd, /MD | boost_ 开头,dll 与lib 文件,不包含s |
shared | static | 一般不这么用 | 一般不这么用 |
同时指定link=shared runtime-link=static
会警告:
1 | warning: The configuration link=shared, runtime-link=static is disabled |
参数文档说明:
1 | link=static|shared Whether to build static or shared libraries |
指定操作系统
address-model=64|32
不指定则同时生成。
Boost生成库命名特点
1 | <boost库类型标识>_<包名>-<平台工具集版本>-[mt]-[s|gd|sgd]-<操作系统架构>-<版本号>.<扩展名> |
- boost库类型标识:静态库
libboost
,动态库boost
。 - 包名:通过
./b2.exe --show-libraries
可以查看全部。json、locale、math、random等。 - 平台工具集版本:VS2019对应v142。
[mt]
,出现则表示多进程程序,否则是单进程。threading=multi|single
。s
出现表示runtime-link=static
,gd
出现表示debug模式- 空 - 动态运行时链接 + release版本 -
/MD
s
- 静态运行时链接 + release版本 -/MT
gd
- 动态运行时链接 + debug版本 -/MDd
sgd
- 静态运行时链接 + debug版本 -/MTd
- 空 - 动态运行时链接 + release版本 -
- 操作系统架构:x64、x32等。
- 版本号:
1.79.0
对应1_79
- 扩展名:
lib
或dll
示例
全部构建
1 | ./b2.exe stage --stagedir="../build" --build-type=complete toolset=msvc-14.2 |
- 即生成静态库,也生成动态库
- 即生成动态运行时链接,也生成静态运行时链接
- 即生成debug,也生成release
- 即生成64位库,也生成32位库
构建内容如下所示(以json包为例),左侧为静态库,共分为8类,右侧为动态库,共有4类。
细分构建
全部构建完成后执行这一步,只要平台工具集不变,只需进行拷贝操作,速度较快。
(1)static-debug-MDd-x64,libboost_
,mt-gd-x64
1 | ./b2.exe stage --stagedir="../build/static-debug-MDd-x64" --build-type=complete toolset=msvc-14.2 link=static runtime-link=shared address-model=64 variant=debug |
(2)static-debug-MTd-x64,libboost_
,mt-sgd-x64
1 | ./b2.exe stage --stagedir="../build/static-debug-MTd-x64" --build-type=complete toolset=msvc-14.2 link=static runtime-link=static address-model=64 variant=debug |
(3)static-release-MD-x64,libboost_
,mt-x64
1 | ./b2.exe stage --stagedir="../build/static-release-MD-x64" --build-type=complete toolset=msvc-14.2 link=static runtime-link=shared address-model=64 variant=release |
(4)static-release-MT-x64,libboost_
,mt-s-x64
1 | ./b2.exe stage --stagedir="../build/static-release-MT-x64" --build-type=complete toolset=msvc-14.2 link=static runtime-link=static address-model=64 variant=release |
(5)static-debug-MDd-x32,libboost_
,mt-gd-x32
1 | ./b2.exe stage --stagedir="../build/static-debug-MDd-x32" --build-type=complete toolset=msvc-14.2 link=static runtime-link=shared address-model=32 variant=debug |
(6)static-debug-MTd-x32,libboost_
,mt-sgd-x32
1 | ./b2.exe stage --stagedir="../build/static-debug-MTd-x32" --build-type=complete toolset=msvc-14.2 link=static runtime-link=static address-model=32 variant=debug |
(7)static-release-MD-x32,libboost_
,mt-x32
1 | ./b2.exe stage --stagedir="../build/static-release-MD-x32" --build-type=complete toolset=msvc-14.2 link=static runtime-link=shared address-model=32 variant=release |
(8)static-release-MT-x32,libboost_
,mt-s-x32
1 | ./b2.exe stage --stagedir="../build/static-release-MT-x32" --build-type=complete toolset=msvc-14.2 link=static runtime-link=static address-model=32 variant=release |
(9)shared-debug-MDd-x64,boost_
,mt-gd-x64
1 | ./b2.exe stage --stagedir="../build/shared-debug-MDd-x64" --build-type=complete toolset=msvc-14.2 link=shared runtime-link=shared address-model=64 variant=debug |
(10)shared-release-MD-x64,boost_
,mt-x64
1 | ./b2.exe stage --stagedir="../build/shared-release-MD-x64" --build-type=complete toolset=msvc-14.2 link=shared runtime-link=shared address-model=64 variant=release |
(11)shared-debug-MDd-x32,boost_
,mt-gd-x32
1 | ./b2.exe stage --stagedir="../build/shared-debug-MDd-x32" --build-type=complete toolset=msvc-14.2 link=shared runtime-link=shared address-model=32 variant=debug |
(12)shared-release-MD-x32,boost_
,mt-x32
1 | ./b2.exe stage --stagedir="../build/shared-release-MD-x32" --build-type=complete toolset=msvc-14.2 link=shared runtime-link=shared address-model=32 variant=release |
自动构建Powershell脚本
1 | $env:toolset=14.3 |
VS2022(v143)编译Boost1.58遇到的问题
‘cl’不是内部或外部命令,也不是可运行的程序或批处理文件
复现:使用powershell执行b2命令
可能是环境变量中不存在VS2022的工具集路径(安装后未重启)
解决:使用Developer Command Prompt for VS 2022
运行b2
missing argument global-setup
c++ - Boost installation: missing argument global-setup - Stack Overflow
打开b2.exe目录下的project-config.jam
,修改为:
1 | import option ; |
如果是32位就写Hostx86\x86\cl.exe