Boost库学习笔记(一)安装与配置

Boost库学习笔记(一)安装与配置

1. 获取boost

https://www.boost.org/users/history/version_1_79_0.html

任选其一

在这里插入图片描述

boost的目录结构:

在这里插入图片描述

tip:

  1. boost库的根目录库应该设置到环境变量**$BOOST_ROOT** 中,笔者路径为(D:\Software\boost_1_79_0
  2. 编译boost程序时,你需要将boost的头文件的目录添加进来,笔者路径为(D:\Software\boost_1_79_0\boost

仅头文件的库:

大多数的boost库都是header-only的,即头文件已经包含了模板和内联函数,不需要单独编译或者在链接时特殊对待。

当然也有些库需要单独编译才能用:

在这里插入图片描述

还有一少部分是可选单独编译的:

  • Boost.Graph also has a binary component that is only needed if you intend to parse GraphViz files.
  • Boost.Math has binary components for the TR1 and C99 cmath functions.
  • Boost.Random has a binary component which is only needed if you’re using random_device.
  • Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.
  • Boost.Exception provides non-intrusive implementation of exception_ptr for 32-bit _MSC_VER==1310 and _MSC_VER==1400 which requires a separately-compiled binary. This is enabled by #define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR.
  • Boost.System is header-only since Boost 1.69. A stub library is still built for compatibility, but linking to it is no longer necessary

2.Windows安装过程

使用windows的命令提示符用管理员的权限打开,切换到boost的根目录。

bootstrap.bat  # 执行bootstrap.bat的批处理
# 运行完成后会生成b2.exe程序
b2.exe # 开始编译工作

b2.exe程序可以支持的参数有:

  • toolset 编译器类别,可以是gcc,或者是msvc

  • target-os 目标操作系统,可以是Windows,Linux

  • variant 生成类型,可以是 debug(-gd-)或者release (-)

  • threadapi 线程API的实现,可以是win32或者pthread,在windows上编译就用win32

  • link 链接库类型,可以是静态的static,也可以是shared,对应windows上的库就是.lib或者是dll

  • runtime-link 动态还是静态链接C/C++运行时库。同样有shared (-)和static (-s-)两种方式,这样runtime-link和link可以产生4种组合方式,可以根据需要选择编译.

  • prefix 安装路径

  • user-config 用户自定义配置文件,在cross compile的时候可以用来配置编译工具链

  • address-model 生成32位还是64位库文件

  • architecture=x86 cpu架构,一般台式机位x86,嵌入式平台位arm

编译安装64位的Release版的静态链接库可以执行下面的指令:(-mt-s-)

.\b2.exe variant=release threading=multi threadapi=win32 link=static runtime-link=static --prefix=E:\boost_1_77_0 address-model=64 architecture=x86 install -j8

本段内容摘自https://www.cnblogs.com/lylygoing/p/BoostDownload.html

当没有指定参数的时候,默认编译生成在当前目录下。

在这里插入图片描述

从输出可以看出,头文件在D:\Software\boost_1_79_0,链接的库的路径在D:\Software\boost_1_79_9\stage\lib下,其中也会生成bin.v2的目录,大小在2.3G左右,可以直接删除。

3. Linux安装过程

  • 下载
    https://www.boost.org/users/history/version_1_79_0.html
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JdOpAUfy-1659447175784)(imgs/image-20220802152104328.png)]

    tar -xzvf boost_1_79_0.tar.gz #解压
    cd boost_1_79_0 #进入目录
    vim bootstrap.sh #修改prefix为指定目录,笔者为/usr/local/boost1.79
    ./bootstrap.sh
    sudo ./b2 install # 不加sudo仍然生成在本目录
    

4.VS的编译工作准备

  • 创建新项目(VS2022)

    在这里插入图片描述
    在这里插入图片描述

  • 新建一个源文件
    在这里插入图片描述
    在这里插入图片描述

    粘贴以下代码

    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        using namespace boost::lambda;
        typedef std::istream_iterator<int> in;
    
        std::for_each(
            in(std::cin), in(), std::cout << (_1 * 3) << " ");
    }
    
  • 对解决方案进行配置
    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

5.测试运行

每次都会输出你输入值的三倍

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7t1lWTu5-1659447175796)(imgs/image-20220802212612696.png)]

6.要点总结

  1. 安装的时候先执行bootstrap的脚本文件(Windows执行.bat,Linux执行.sh),之后再执行b2的程序进行编译安装,注意参数可以指定。
  2. 使用Boost库主要是附加其头文件目录和库文件目录,用什么工具都要遵循这个原则。
  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Moresweet猫甜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值