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

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

  1. tar zxvf boost_1_58_0.tar.gz

2.设置编译器和所选库

先进入解压缩后的目录:

  1. cd boost_1_58_0

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

  1. ./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"

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

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

3.编译boost

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

  1. ./b2 toolset=gcc

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

  1. ...failed updating 60 targets...
  2. ...skipped 21 targets...
  3. ...updated 663 targets...

4.安装boost

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

  1. ./b2 install --prefix=/usr

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

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

  1. ...failed updating 60 targets...
  2. ...skipped 21 targets...
  3. ...updated 11593 targets...

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

  1. ldconfig

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

5.boost使用测试

以boost_thread为例,测试刚安装完的boost库是否能正确使用,测试代码如下:

  1. #include <boost/thread/thread.hpp> //包含boost头文件
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. volatile bool isRuning = true;
  6. void func1()
  7. {
  8. static int cnt1 = 0;
  9. while(isRuning)
  10. {
  11. cout << "func1:" << cnt1++ << endl;
  12. sleep(1);
  13. }
  14. }
  15. void func2()
  16. {
  17. static int cnt2 = 0;
  18. while(isRuning)
  19. {
  20. cout << "\tfunc2:" << cnt2++ << endl;
  21. sleep(2);
  22. }
  23. }
  24. int main()
  25. {
  26. boost::thread thread1(&func1);
  27. boost::thread thread2(&func2);
  28. system("read");
  29. isRuning = false;
  30. thread2.join();
  31. thread1.join();
  32. cout << "exit" << endl;
  33. return 0;
  34. }

在编译程序时,需要加入对boost_thread库的引用:

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

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

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

  1. func1: func2:00
  2. func1:1
  3. func2:1
  4. func1:2
  5. func1:3
  6. func2:2
  7. func1:4
  8. func1:5
  9. func2:3
  10. func1:6
  11. func1:7
  12. func2:4
  13. func1:8
  14. func1:9
  15. func2:5
  16. func1:10
  17. func1:11
  18. func2:6
  19. func1:12
  20. func1:13
  21. func2:7
  22. func1:14
  23. func1:15
  24. func2:8
  25. func1:16
  26. 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工作环境中安装包命令后加上国内源速度*15

    example: pip install -r requests.txt -r https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pyp ...

  2. cocos图片的选择以及压缩

    我们在使用cocos在windows平台下,运行速度很快很流畅,很强大,可是当我们打包成apk文件,在手机上运行的时候,流畅度很可能降低,甚至还有间歇性内存彪高. 游戏内存优化我们一般可以从这么3个方 ...

  3. Lvs Dr 模式配置

    1.Dr 安装 ipvsadm # yum -y install ipvsadm # lsmod | grep ip_vs    #检查ipvs模块是否加载进系统.把ipvs模块加载进系统,需要我们执 ...

  4. jmeter学习随笔

    1.jmeter配置环境是需要注意jdk版本,不同的jdk版本可支持运行不同版本的jmeter,对应关系如下图 2.HTTP请求和HTTP默认请求的区别 若一个项目中会多次调用相同的接口域名及端口号, ...

  5. How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views

    How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views  Ellen S ...

  6. 学习java的阶段性理解(其它语言也一样)

    打算从今天开始学java啊,待会滚去找资料了.现在谈一下学习java阶段性的理解.由于现在对java真的啥也不知道啊,不过还是要瞎鸡儿写点自己的看法,以下看法应该也使适用于其它语言: 第一阶段,入门级 ...

  7. android textiew自定义ClickableSpan无效问题

    我们有个需求,需要将一段文本中的url跳转,替换成跳转我们app的某个页面.然后就开始搞,先自定义clickspan,在设置LinkMovementMethoid, 但是不管怎么搞就是不生效. 这是我 ...

  8. 评测parser的好坏

    1.在dependency parsing中一般是用 LAS UAS 来衡量 简要说来UAS是知道是边对了(也就是它依赖的节点找对了)就算对,而LAS在前者的基础上要求更加严格,还要求边的Label也 ...

  9. JPA、Hibernate框架、通用mapper之间的关系及通用mapper的具体实现

    JPA是描述对象-关系表的映射关系,将运行期实体对象持久化到数据库中,提出以面向对象方式操作数据库的思想. Hibernate框架核心思想是ORM-实现自动的关系映射.缺点:由于关联操作提出Hql语法 ...

  10. 锚点的animate使用过程中定位不准确的问题小记

    源码: $('html, body, .S').animate({ scrollTop: $('.a1').offset().top - 133}, { duration: 1500, easing: ...