[c++ boost][基础篇-1]-windows下编译boost库

1.介绍

强大的C++库,包含了各种编程所需的工具
官网 https://www.boost.org/

2.源码下载

最近几个版本地址 https://www.boost.org/users/history/

3.使用MSVC编译

# 命令行执行
bootstrap.bat

一会儿时间后就会生成编译的所需的b2.exe或者bjam.exe两个都一样的
然后执行查看编译参数

b2 --help
# 下面只展示关键参数
# 可以只编译指定的库
#  --with-<library>        		Build and install the specified <library>. If this option is used, only libraries specified using this #option will be built.
# 指定工具集
# toolset=toolset         		Indicate the toolset to build with.
# 编译debug或者release版本,默认都编译
#  variant=debug|release		Select the build variant
# 编译静态链接库或者动态链接库, 默认静态链接库
# link=static|shared			Whether to build static or shared libraries
# 编译单线程版本还是多线程版本, 默认多线程
# threading=single|multi		Whether to build single or multithreaded binaries
# 运行时链接方式,分静态和动态,也就是VS里的的MT/MD,后面详细介绍
# runtime-link=static|shared	Whether to link to static or shared C and C++runtime.
# 指定编译32位或64位,默认都编译
# address-model=64|32

其中参数toolset可在bootstrap.bat中查看

# bootstrap.bat 41行开始
SET TOOLSET=msvc

IF "%1"=="gcc" SET TOOLSET=gcc
IF "%1"=="vc71" SET TOOLSET=msvc : 7.1
IF "%1"=="vc8" SET TOOLSET=msvc : 8.0
IF "%1"=="vc9" SET TOOLSET=msvc : 9.0
IF "%1"=="vc10" SET TOOLSET=msvc : 10.0
IF "%1"=="vc11" SET TOOLSET=msvc : 11.0
IF "%1"=="vc12" SET TOOLSET=msvc : 12.0
IF "%1"=="vc14" SET TOOLSET=msvc : 14.0
IF "%1"=="vc141" SET TOOLSET=msvc : 14.1

上面列出了支持的所有工具集有gccmsvc,可以看到本教程目前使用的版本boost_1_67_0最高只支持14.1也就是vs 2017

# VS和MSVC对应关系,在后面设置编译参数时需要用到
VS 2017 --> 14.1
VS 2015 --> 14.0
VS 2013 --> 12.0

3.1编译示例

  • 1.有时候为了省时间,只编译date_timefilesystem库(filesystem依赖system),使用vs2015,编译64位库

    b2 toolset=msvc-14.0 --with-date_time --with-filesystem --with-system address-model=64
    

    得到如下库文件,带gd的表示debug库
    stage/lib/libboost_date_time-vc140-mt-gd-x64-1_67.lib
    stage/lib/libboost_date_time-vc140-mt-x64-1_67.lib
    stage/lib/libboost_filesystem-vc140-mt-gd-x64-1_67.lib
    stage/lib/libboost_filesystem-vc140-mt-x64-1_67.lib
    stage/lib/libboost_system-vc140-mt-gd-x64-1_67.lib
    stage/lib/libboost_system-vc140-mt-x64-1_67.lib

  • 2.使用vs2015,只编译date_time库,只编译64release版本

    b2 toolset=msvc-14.0 --with-date_time address-model=64 variant=release
    

    得到如下文件
    libboost_date_time-vc140-mt-x64-1_67.lib >> 770KB

  • 3.使用vs2015,只编译date_time库,只编译64release版本,并且选动态库版本

    b2 toolset=msvc-14.0 --with-date_time address-model=64 variant=release link=shared
    

    得到如下文件,注意观察文件名前面少了lib
    boost_date_time-vc140-mt-x64-1_67.lib >> 30KB
    boost_date_time-vc140-mt-x64-1_67.dll >> 60KB

  • 4.使用vs2017 (注:实际使用vs2015编译,只为了演示不同vs下工具集参数如何设置),只编译date_time库,只编译64release 静态库版本,并且选择MT版本 MT介绍

    b2 toolset=msvc-14.1 --with-date_time address-model=64 variant=release link=static runtime-link=static
    

    得到如下文件
    libboost_date_time-vc140-mt-s-x64-1_67.lib >> 807KB

  • 5.使用vs2017 (注:实际使用vs2015编译,只为了演示不同vs下工具集参数如何设置),只编译date_time库,只编译64release 静态库版本,并且选择MD版本 MD介绍

    b2 toolset=msvc-14.1 --with-date_time address-model=64 variant=release link=static runtime-link=shared
    

    得到如下文件
    libboost_date_time-vc140-mt-x64-1_67.lib >> 770KB

3.2 命名规则

好了,细心的小伙伴发现了,上面每次编译出来的库名称有点不太一样,现在让我们根据库大小来推断一下

  • 上面第2条第3条表明默认使用link=static参数编译,即得到静态库版本,表示程序运行时不需要依赖额外的dll文件
  • 上面第4条第5条表明默认使用runtime-link=static参数编译,即得到MT版本

说了这么多MTMD到底干嘛用的,好了官方告诉你了,
指示多线程模块是否为 DLL。
一句话,我也不太懂,但是你编译的时候所有库的都需要保持一致,要么全为MT要么全部为MD,不然链接的时候会出现各种匹配不上的错误。参考官网

3.3 默认编译

当然了,一般新手并不了解boost一共有多少个需要编译的库,和哪些不需要编译就可以使用的纯头文件的库,所以建议直接全部编译就行,就是有点费时。

# 默认编译 vs2017
b2 toolset=msvc-14.1
# vs2013
b2 toolset=msvc-12.0

上面的命令表示,所有参数缺省,均使用默认参数,关于默认参数上面有介绍

3.3.1编译paddle所需boost库MT版本

没有boost的同学 可以下载 boost_1_67_0.zip 或者 所有版本 保存到lib文件夹下然后解压,并执行

# 
bootstrap.bat
# vs2015 执行
b2 toolset=msvc-14.0 --with-date_time --with-filesystem --with-system --with-regex address-model=64 variant=release link=static runtime-link=static
# vs2017 执行
b2 toolset=msvc-14.1 --with-date_time --with-filesystem --with-system --with-regex address-model=64 variant=release link=static runtime-link=static

出现如下文件表明编译成功
stage/lib/libboost_date_time-vc140-mt-s-x64-1_67.lib
stage/lib/libboost_filesystem-vc140-mt-s-x64-1_67.lib
stage/lib/libboost_regex-vc140-mt-s-x64-1_67.lib
stage/lib/libboost_system-vc140-mt-s-x64-1_67.lib

3.4 静态编译

4.使用gcc编译

未更新

./bootstrap.sh --with-toolset=gcc
./b2
./bootstrap.sh --with-libraries=date_time --with-libraries=filesystem --with-libraries=system --with-libraries=regex --with-toolset=gcc 

./b2 install --prefix='/usr/local/include'

5.使用arm编译

未更新

./bootstrap.sh --with-toolset=gcc architecture=arm
./b2 architecture=arm --CXX_FLAGS=-fPIC --C_FLAGS=-fPIC

6.使用mingw编译

未更新

cd */engine
build.bat gcc

cd boost_1_67_0
bjam "toolset=gcc" link=static runtime-link=static threading=multi  --build-type=complete release
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亡命天涯ba

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值