0%

Boost编译

  1. 资源下载和教程
  2. 安装工具预生成
  3. Boost库生成(安装)
    1. b2命令参数及作用
      1. 指定不同的构建目标
      2. 指定构建模式
      3. 指定平台工具集
      4. 指定构建的包
      5. 多线程与单线程版本
      6. debug和release
      7. link与runtime-link的配置:静态库与动态库,静态运行时与动态运行时
      8. 指定操作系统
    2. Boost生成库命名特点
    3. 示例
      1. 全部构建
      2. 细分构建
    4. 自动构建Powershell脚本
  4. VS2022(v143)编译Boost1.58遇到的问题
    1. ‘cl’不是内部或外部命令,也不是可运行的程序或批处理文件
    2. missing argument global-setup
  • Boost版本:1.79.0
  • 编译器:VS2019(Windows)

资源下载和教程

Boost编译与使用 - 知乎 (zhihu.com)

历史版本下载: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

下载源码包并解压↓

image-20220523175341085

安装工具预生成

打开“Developer Command Prompt for VS 2019”↓

image-20220523182031192

进入boost源码目录并执行命令:

1
2
3
d:
cd D:\Boost\boost1.79.0\boost_1_79_0
.\bootstrap.bat

运行完成后,源码目录下新增两个文件:b2.exeproject-config.jam

image-20220523201331399

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
2
3
4
5
6
7
8
9
10
11
12
--build-type=<type>     Build the specified pre-defined set of variations of
the libraries. Note, that which variants get built
depends on what each library supports.

-- minimal -- (default) Builds a minimal set of
variants. On Windows, these are static
multithreaded libraries in debug and release
modes, using shared runtime. On Linux, these are
static and shared multithreaded libraries in
release mode.

-- complete -- Build all possible variations.

指定平台工具集

toolset=<toolset>

<toolset>可选值:

  • msvc-14.2 - VS2019
  • msvc-14.0 - VS2015
  • msvc-10.0 - VS2010
  • … - 其他VS对应的平台工具集
  • gcc - MingGW

指定构建的包

--with-<library>包含xxx

--without-<library>不包含xxx

不写就以--build-type指定的构建。有哪些包需要构建可以通过--show-libraries查看:

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
PS D:\Boost\boost1.79.0\boost_1_79_0> ./b2.exe --show-libraries
The following libraries require building:
- atomic
- chrono
- 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
- wave

多线程与单线程版本

threading=single|multi

大部分情况下都用multi默认值,生成的库文件带有mt

只有很少的情况下使用single,生成的库文件不带mt标志。

debug和release

variant=debug|release

不指定则同时生成debug和release版本。二者区别在于:debug版本的库文件名带有gd,release版本的不带。

link与runtime-link的配置:静态库与动态库,静态运行时与动态运行时

这里涉及到两个选项:linkruntime-link,两个选项的可选值都是staticshared若不指定则会自动交叉编译

两个参数有何区别?

  • link指定生成的boost是动态库还是静态库。static生成静态库,以libboost_开头,只有*.lib文件;shared生成动态库,有*.dll*.lib文件,均以boost_开头。
  • runtime-link指定与C++运行时的链接方式。static表示静态链接,调用系统库时采用静态方式建立连接,在VS工程中的配置为/MTd/MTshared表示动态链接,对应/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_开头,dlllib文件,不包含s
shared static 一般不这么用 一般不这么用

同时指定link=shared runtime-link=static会警告:

1
2
3
warning: The configuration link=shared, runtime-link=static is disabled
warning: by default as being too dangerous to use, and will not be built.
warning: To enable it, use --allow-shared-static.

参数文档说明:

1
2
3
4
link=static|shared      Whether to build static or shared libraries
runtime-link=static|shared
Whether to link to static or shared C and C++
runtime.

指定操作系统

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=staticgd出现表示debug模式
    • 空 - 动态运行时链接 + release版本 - /MD
    • s - 静态运行时链接 + release版本 - /MT
    • gd - 动态运行时链接 + debug版本 - /MDd
    • sgd - 静态运行时链接 + debug版本 - /MTd
  • 操作系统架构:x64、x32等。
  • 版本号:1.79.0对应1_79
  • 扩展名:libdll

示例

全部构建

1
./b2.exe stage --stagedir="../build" --build-type=complete toolset=msvc-14.2
  • 即生成静态库,也生成动态库
  • 即生成动态运行时链接,也生成静态运行时链接
  • 即生成debug,也生成release
  • 即生成64位库,也生成32位库

构建内容如下所示(以json包为例),左侧为静态库,共分为8类,右侧为动态库,共有4类。

image-20220523221544750

细分构建

全部构建完成后执行这一步,只要平台工具集不变,只需进行拷贝操作,速度较快。

(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
2
3
4
5
6
7
8
9
10
11
12
13
$env:toolset=14.3
./b2.exe stage --stagedir="../build/static-debug-MDd-x64" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=shared address-model=64 variant=debug
./b2.exe stage --stagedir="../build/static-debug-MTd-x64" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=static address-model=64 variant=debug
./b2.exe stage --stagedir="../build/static-release-MD-x64" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=shared address-model=64 variant=release
./b2.exe stage --stagedir="../build/static-release-MT-x64" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=static address-model=64 variant=release
./b2.exe stage --stagedir="../build/static-debug-MDd-x32" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=shared address-model=32 variant=debug
./b2.exe stage --stagedir="../build/static-debug-MTd-x32" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=static address-model=32 variant=debug
./b2.exe stage --stagedir="../build/static-release-MD-x32" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=shared address-model=32 variant=release
./b2.exe stage --stagedir="../build/static-release-MT-x32" --build-type=complete toolset=msvc-$env:toolset link=static runtime-link=static address-model=32 variant=release
./b2.exe stage --stagedir="../build/shared-debug-MDd-x64" --build-type=complete toolset=msvc-$env:toolset link=shared runtime-link=shared address-model=64 variant=debug
./b2.exe stage --stagedir="../build/shared-release-MD-x64" --build-type=complete toolset=msvc-$env:toolset link=shared runtime-link=shared address-model=64 variant=release
./b2.exe stage --stagedir="../build/shared-debug-MDd-x32" --build-type=complete toolset=msvc-$env:toolset link=shared runtime-link=shared address-model=32 variant=debug
./b2.exe stage --stagedir="../build/shared-release-MD-x32" --build-type=complete toolset=msvc-$env:toolset link=shared runtime-link=shared address-model=32 variant=release

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
2
3
4
5
import option ; 

using msvc : 14.3 : "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\Hostx64\x64\cl.exe" ;

option.set keep-going : false ;

如果是32位就写Hostx86\x86\cl.exe