1. 下载boost安装包并解压缩

http://www.boost.org/下载boost的安装包,以boost_1_58_0.tar.gz为例 
下载完成后进行解压缩:

tar zxvf boost_1_58_0.tar.gz

2.设置编译器和所选库

先进入解压缩后的目录:

cd boost_1_58_0

然后运行bootstrap.sh脚本并设置相关参数:

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

--with-libraries指定编译哪些boost库,all的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可,可指定的库有以下几种:

库名 说明
atomic  
chrono  
context  
coroutine  
date_time  
exception  
filesystem  
graph 图组件和算法
graph_parallel  
iostreams  
locale  
log  
math  
mpi 用模板实现的元编程框架
program_options  
python 把C++类和函数映射到Python之中
random  
regex 正则表达式库
serialization  
signals  
system  
test  
thread 可移植的C++多线程库
timer  
wave  

--with-toolset指定编译时使用哪种编译器,Linux下使用gcc即可,如果系统中安装了多个版本的gcc,在这里可以指定gcc的版本,比如--with-toolset=gcc-4.4

./b2 install --build-type=complete --layout=versioned --with-thread --prefix="/usr/local"

命令执行完成后看到显示如下即为成功:

Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.6
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam... Bootstrapping is done. To build, run: ./b2 To adjust configuration, edit 'project-config.jam'.
Further information: - Command line help:
./b2 --help - Getting started guide:
http://www.boost.org/more/getting_started/unix-variants.html - Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html

3.编译boost

执行以下命令开始进行boost的编译:

./b2 toolset=gcc

编译的时间大概要10多分钟,耐心等待,结束后会有以下提示:

...failed updating 60 targets...
...skipped 21 targets...
...updated 663 targets...

4.安装boost

最后执行以下命令开始安装boost:

./b2 install --prefix=/usr

--prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。这里把安装目录指定为--prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。

安装完毕后会有以下提示:

...failed updating 60 targets...
...skipped 21 targets...
...updated 11593 targets...

最后需要注意,如果安装后想马上使用boost库进行编译,还需要执行一下这个命令:

ldconfig

更新一下系统的动态链接库

5.boost使用测试

以boost_thread为例,测试刚安装完的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_thread库的引用:

g++ main.cpp -g -o main -lboost_thread

如果boost库的安装位置不是在系统目录下,则还需要在编译时加上-I和-L指定boost头文件和库文件的位置

编译成功后运行程序,利用boost实现的多线程任务正确运行:

func1:  func2:00

func1:1
func2:1
func1:2
func1:3
func2:2
func1:4
func1:5
func2:3
func1:6
func1:7
func2:4
func1:8
func1:9
func2:5
func1:10
func1:11
func2:6
func1:12
func1:13
func2:7
func1:14
func1:15
func2:8
func1:16 exit

[转]Linux编译和安装boost库的更多相关文章

  1. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...

  2. linux下编译安装boost库

    linux下编译安装boost库 linux下编译安装boost库 1.下载并解压boost 1.58 源代码 下载 解压 2.运行bootstrap.sh 3.使用b2进行构建 构建成功的提示 4. ...

  3. 【转】Centos下编译升级安装Boost

    https://www.xingchenw.cn/article/191 Centos下编译升级安装Boost 首先在官网现在相应的包 https://www.boost.org/users/down ...

  4. 安装Boost库

    获取方式 官网下载合适版本:https://www.boost.org/ 此处用的是boost_1_75_0版本 开发环境 推荐使用GCC 7.x.x或以上编译器 安装Boost库 此处采用简易安装, ...

  5. VS2010下安装boost库

    在我们的C++项目中安装boost库,下面以VS2010版本作为例子,其它版本的设置也差不多. 一.编译生成boost库 1.下载最新的boost(本人下载的是boost_1_56_0).boost官 ...

  6. 树莓派上安装boost库

    一.安装boost库 sudo apt-get install libboost-dev aptitude search boost 二.编写测试代码 #include <iostream> ...

  7. 在RedHat 7.2中安装boost库

    在RedHat 7.2中安装boost库 环境,其它版本类似 Redhat7.2 64bit boost 1.64.0 步骤 去 boost官网 下载想要版本的.tar.gz,如下图 解压tar -v ...

  8. C++: Mac上安装Boost库并使用CLion开发

    1.下载安装Boost库 官网下载最新版本1.65.0:http://www.boost.org/users/history/version_1_65_0.html 选择UNIX版本: 下载后解压cd ...

  9. ubuntu 下安装boost库

    ubuntu下安装boost库,,在网上试了一些其他人推荐的libboost-dev 但是会缺少,编译程序会报错: /usr/bin/ld: cannot find -lboost_serializa ...

随机推荐

  1. python之django母板页面

    其实就是利用{% block xxx %}   {% endblock %}的方式定义一个块,相当于占位.存放在某个html中,比如base.html 然后在需要实现这些块的文件中,使用继承{% ex ...

  2. Verilog中的reg一定会被综合成寄存器么

    对应于实际的数字电路中,如果该程序块描述的是时序逻辑,则该寄存器变量对应为寄存器:如果该程序块描述的是组合逻辑,该寄存器变量对应为硬件逻辑:如果该程序块描述的是不完全组合逻辑,那么该寄存器变量也可以对 ...

  3. 转载 * jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小

    由jQuery实现上下.左右动态改变左右.上下两个div的大小,需要自己引入jquery1.8.0.min.js包 可用于页面布局. //============================ind ...

  4. epoll+socket实现 socket并发 linux服务器

    /* 实现功能:通过epoll, 处理多个socket * 监听一个端口,监听到有链接时,添加到epoll_event * xs */ #include <stdio.h> #includ ...

  5. Linux中各个文件的作用

    1.bin: 存放的是执行的常用指令 2.boot: 启动系统的核心文件 3.dev: Linux将设备映射成文件,而dev中放的就是这些设备文件 4.etc: 各种配置文件 5.home: 用户的主 ...

  6. 关于toLocaleDateString的坑

    https://segmentfault.com/a/1190000009391790

  7. 牛客网PAT乙级(Basic Level)真题-数字分类 (20)

    题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4 ...

  8. kafka工作原理介绍

    两张图读懂kafka应用: Kafka 中的术语   broker:中间的kafka cluster,存储消息,是由多个server组成的集群.  topic:kafka给消息提供的分类方式.brok ...

  9. Linq语句的认识

    LINQ语句的使用小结: 1.将数组看做一张表来查询的情况: from d in countyIsCityLevel where d.Equals(AreaCode) select d   2.只查询 ...

  10. Vue小技巧-懒加载

    Vue懒加载包括图片懒加载与路由懒加载 1.图片懒加载: 首先安装 vue-lazyload包 然后导入并加载事先下载好的加载图片 import VueLazyLoad from 'vue-lazyl ...