ubuntu12.04+boost_1_54_0

来源:互联网 发布:csdn修改博客域名 编辑:程序博客网 时间:2024/05/16 18:34

http://blog.csdn.net/glt3953/article/details/9960179

手动下载
http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz

解压:

tar --bzip2 -xf ./boost_1_54_0.tar.bz2

开始编译,全部编译耗时太多,所以我仅选择我需要的库:

先用下面的命令查看有多少库可以编译:

./bootstrap.sh --show-libraries
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2

The following Boost libraries have portions that require a separate build
and installation step. Any library not listed here can be used by including
the headers only.

The Boost libraries requiring separate building and installation are:
    - atomic
    - chrono
    - context
    - coroutine
    - date_time
    - exception
    - filesystem
    - graph
    - graph_parallel
    - iostreams
    - locale
    - log
    - math
    - mpi
    - program_options
    - python
    - random
    - regex
    - serialization
    - signals
    - system
    - test
    - thread
    - timer
    - wave

然后就编译我要的库:

./bootstrap.sh --with-libraries=system,filesystem,log,thread
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2
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/boost-build2/doc/html/index.html

然后运行下面的命令完成编译。

./b2

耐心等待。不过因为不是编译所有库。时间会少很多。以后需要再来编译。很快看到结果:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

/usr/src/boost_1_54_0

The following directory should be added to linker library paths:

/usr/src/boost_1_54_0/stage/lib

忘记安装了。再运行./b2 install 命令,默认安装在

/usr/local/lib目录下

头文件在

/usr/local/include/boost目录下

***********************************************************************************************

前期准备:boost中,用到了别的函数库,所以为了使用boost中相应的功能,需要先安装系统中可能缺失的库 


apt-get install mpi-default-dev  #安装mpi库

apt-get install libicu-dev     #支持正则表达式的UNICODE字符集 

apt-get install python-dev     #需要python的话

apt-get install libbz2-dev     #如果编译出现错误:bzlib.h: No such file or directory

上述函数库装好之后,就可以编译boost库了。解压boost_1_49_0.tar.bz2,得到/boost_1_49_0,将当前工作目录切换到此文件夹下。


./bootstrap.sh

生成bjam,上述命令可以带有各种选项,具体可参考帮助文档: ./bootstrap.sh --help。其中--prefix参数,可以指定安装路径,如果不带--prefix参数的话(推荐),默认路径是 /usr/local/include 和 /usr/local/lib,分别存放头文件和各种库。执行完成后,会生成bjam,已经存在的脚本将会被自动备份。注意,boost 1.49会在当前目录下,生成两个文件bjam和b2,这两个是一样的,所以接下来的步骤,可以用这两个中的任意一个来执行。


usingmpi ;  #如果需要MPI功能,需要在 /tools/build/v2/user-config.jam 文件的末尾添加

 接下来就是利用生成的bjam脚本编译源代码了


./b2 -a -sHAVE_ICU=1  #-a参数,代表重新编译,-sHAVE_ICU=1代表支持Unicode/ICU

注意,这里是全部编译。当然也可以选择只编译一部分,选项 --with-<library>  只编译指定的库,如输入--with-regex就只编译regex库了。boost1.49 的完全编译,在笔者Intel® Core™2 Duo CPU T5750 @ 2.00GHz × 2 ,2G DDR2内存的老机子上,使用上述选项,半个小时就差不多了。这个时间是可以承受的。全部编译安装,心理上感觉也舒服些。^_^

bjam的一些常用的参数,列表如下:

--build-dir=<builddir>编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了)--stagedir=<stagedir>存放编译后库文件的路径,默认是stage--build-type=complete

编译所有版本,不然只会编译一小部分版本,确切地说是相当于:

variant=release, threading=multi;link=shared|static;runtime-link=shared

variant=debug|release决定编译什么版本(Debug or Release?)link=static|shared决定使用静态库还是动态库threading=single|multi决定使用单线程还是多线程库runtime-link=static|shared决定是静态还是动态链接C/C++标准库--with-<library>只编译指定的库,如输入--with-regex就只编译regex库了--show-libraries 显示需要编译的库名称

 编译完成后,进行安装,也就是将头文件和生成的库,放到指定的路径(--prefix)下

注意了:/usr/local/include/   /usr/local/lib/   的权限一定要是 最大,不然库的安装不成功。
命令:
sudo chmod 777 -R /usr/local/include/
sudo chmod 777 -R /usr/local/lib/
./b2 install

 至此,如果一切顺利,就完成安装了。写个小程序检验下,来自《Boost程序库完全开发指南——深入C++“准”标准库(修订版)》(罗剑锋著,电子工业出版社2012.5)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <iostream>
#include <boost/timer.hpp>
 
using namespace boost;
using namespace std ;
 
int main()
{
    timer t;
    cout <<"max timespan: "<< t.elapsed_max() / 3600 <<"h"<< endl;
    cout <<"min timespan: "<< t.elapsed_min() <<"s"<< endl;
 
    cout <<"now time elapsed: "<< t.elapsed() << "s"<< endl;
 
    return 1;
}

 程序输出:


max timespan: 0.596523h
min timespan: 1e-06s

n


***************************************************************************************

1.下载 boost-1_52  

http://sourceforge.net/projects/boost/files/boost/1.54.0/boost_1_54_0.tar.gz/download

2. 将文件解压在/usr/local/目录下

3. 进入/usr/local/boost-1_54_0/ 目录, 在terminal中输入

./bootstrap.sh

4.进入/usr/local/boost-1_54_0/ 目录,在terminal中输入

sudo ./b2

5.进入/usr/local/boost-1_54_0/ 目录,在terminal中输入

sudo ./bjam --layout=versioned--build-type=complete--toolset=gccinstall

6.添加环境变量(刚改完要重启或者注销一下来更新刚修改过的环境变量)

两种方法:

(1)修改/etc/profie文件 末尾添加

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

(2)在/etc/profile.d/ 中新建一个shell文件boost.sh

#!/bin/sh

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

测试:
test.cpp

[cpp] view plaincopy
  1. #include <boost/lexical_cast.hpp>  
  2. #include <iostream>  
  3. int main()  
  4. {  
  5. using boost::lexical_cast;  
  6. int a = lexical_cast<int>("123");  
  7. double b = lexical_cast<double>("123.12");  
  8. std::cout<<a<<std::endl;  
  9. std::cout<<b<<std::endl;  
  10. return 0;  
  11. }  


编译:
g++ test.cpp -I$BOOST_INCLUDE -L$BOOST_LIB -o test./test
输出:
123
123.12


原创粉丝点击