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. Xcode Archive打包失败问题

    ionic3项目 完成 模拟器 真机测试均可以打包安装成功  在Archive的时候报错了 错误如下 code signing is required for product type 'Applic ...

  2. 使用EFCore,手动创建SQLLite数据库

    有时候我们需要在代码中动态生成一个sqllite数据库文件,可以按照以下代码完成, static void Main(string[] args) { MyContext context = new ...

  3. python中的lambda表达式

    lambda是python中匿名函数的写法  我们可以在不定义函数名的情况下一边定义并调用这个函数 例子: 普通方法定义函数:def  plus(a,b): return a+b lambda方法  ...

  4. 图片上传js

    var imgURL; function getImgURL(node) { try{ var file = null; if(node.files && node.files[0] ...

  5. leetcode题解 9. Palindrome Number

    9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...

  6. python基础—sys与os库

    python可以用sys库打印环境变量或者查看当前文件的脚本路径,具体代码: import sysprint(sys.path[2])#打印环境变量print(sys.argv)#当前脚本路径 os库 ...

  7. 我的学习目标(目前已初步学习完Java语言基础)

    操作系统.尤其是内存/线程/进程方面 计算机网络协议,重点关注 TCP/UDP/HTTP. 数据结构与算法. 数据库 设计模式,熟练掌握常用的几种设计模式. Java语言基础.熟悉java语言基础,了 ...

  8. MongoDB的安装和使用

    Step1:下载和安装 下载地址:http://dl.mongodb.org/dl/win32/x86_64 安装:一直按照默认指示去安装或者选择自己喜欢的路径安装. Step2:配置环境变量 安装完 ...

  9. datafactory5.6向mysql5.7添加大量测试数据

    1.下载安装datafactory5.6 2.下载安装mysql5.7,并创建数据库guest_test和表sign_event 3.下载安装odbc5.3 4.打开datafactory配置数据源, ...

  10. iOS开发,改变系统铃声音量和静音,并非媒体播放音量

    使用AVSystemController可以改变系统声音,而且是铃声,并非媒体播放的声音. 它可以让iPhone手机静音.但是,AVSystemController存在于私有Celestial框架中. ...