Ubuntu20.10中安装boost 1.75库

一、下载Boost 1.75源代码

首先上Boost官网下载Boost 1.75的源代码boost_1_75_0.tar.gz,如下图所示:

Boost 1.75源代码下载

二、编译安装boost 1.75源代码

将从boost_1_75_0.tar.gz下载后的boost 1.75源代码,如下图所示:
boost 1.75源代码

1、解压boost_1_75_0.tar.gz源代码

解压boost_1_75_0.tar.gz源代码

cd 
tar -xvf boost_1_75_0.tar.gz

2、运行bootstrap.sh脚本:

执行脚本bootstrap.sh

./bootstrap.sh 

也可以添加prefix参数:

./bootstrap.sh --with-libraries=all --with-toolset=gcc

·–with-libraries·指定编译哪些·boost·库,all的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可.
也可以添加prefix参数, 自定义生成的头文件和二进制库文件目录:

./booststrap.sh --prefix /usr

则生成的头文件在/usr/lib/include/boost中, 二进制库文件在/usr/lib目录中。

3、接下来,根据提示,执行以下命令开始进行boost的编译:

./b2 toolset=gcc

之后,进行安装指令:

./b2 install

boost 1.75头文件和库文件

三、测试使用boost库

测试boost线程库的源代码如下:

#include <boost/thread/thread.hpp> //包含boost头文件
#include <iostream>
#include <cstdlib>
using namespace std;

volatile bool isRuning = true;

void func1()
{
    static int cnt1 = 0;
    while(isRuning)
    {
        cout << "func1:" << cnt1++ << endl;
        sleep(1);
    }
}

void func2()
{
    static int cnt2 = 0;
    while(isRuning)
    {
        cout << "\tfunc2:" << cnt2++ << endl;
        sleep(2);
    }
}

int main()
{
    boost::thread thread1(&func1);
    boost::thread thread2(&func2);

    system("read");
    isRuning = false;

    thread2.join();
    thread1.join();
    cout << "exit" << endl;
    return 0;
}

为了方便,我把boost 1.75库安装到了/usr目录下,执行命令g++ threadExample01.cpp -o threadExample01 -I/usr/lib -lboost_thread -lpthread运行程序,生成了threadExample01可执行程序,然后执行./threadExample01运行程序,结果如下图所示:
运行boost thread线程示例

参考资料

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值