Linux 下 boost 库的安装,配置个人环境变量

部分引自: https://blog.csdn.net/this_capslock/article/details/47170313

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的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可

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

 

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

./b2 toolset=gcc

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

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

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

./b2 install --prefix=/home/zhangyw/boost/boostlib

5.添加环境变量

vim ~/.bashrc

文件末尾添加:

# Boost
export BOOST_INCLUDE=/home/zhangyw/boost/boostlib/include/
export BOOST_LIB=/home/zhangyw/boost/boostlib/lib

 

6.boost使用测试

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

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

 

7.编译方法

g++ boostTest.cpp -g -o boostTest -lboost_thread

posted @ 2018-11-06 14:19  宇尉  阅读(8225)  评论(0编辑  收藏  举报