环境:ubuntu12.04 32bit,boost1.49
前期准备: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,已经存在的脚本将会被自动备份。注意,boost1.49会在当前目录下,生成两个文件bjam和b2,这两个是一样的,所以接下来的步骤,可以用这两个中的任意一个来执行。
using mpi; #如果需要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™2Duo CPU T5750 @ 2.00GHz × 2 ,2GDDR2内存的老机子上,使用上述选项,半个小时就差不多了。这个时间是可以承受的。全部编译安装,心理上感觉也舒服些。^_^
bjam的一些常用的参数,列表如下:
--build-dir=<builddir> |
编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了) |
--stagedir=<stagedir> |
存放编译后库文件的路径,默认是stage |
--build-type=complete |
编译所有版本,不然只会编译一小部分版本,确切地说是相当于: variant=release,threading=multi;link=shared|static;runtime-link=shared |
variant=debug|release |
决定编译什么版本(Debugor Release?) |
link=static|shared |
决定使用静态库还是动态库 |
threading=single|multi |
决定使用单线程还是多线程库 |
runtime-link=static|shared |
决定是静态还是动态链接C/C++标准库 |
--with-<library> |
只编译指定的库,如输入--with-regex就只编译regex库了 |
--show-libraries |
显示需要编译的库名称 |
编译完成后,进行安装,也就是将头文件和生成的库,放到指定的路径(--prefix)下
?
./b2 install |
至此,如果一切顺利,就完成安装了。写个小程序检验下,来自《Boost程序库完全开发指南——深入C++“准”标准库(修订版)》(罗剑锋著,电子工业出版社2012.5)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include "stdcpp.hpp" #include <boost/timer.hpp>
using namespace boost;
int main() { timert; 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 EXIT_SUCCESS; } |
程序输出:
?
max timespan: 0.596523h
min timespan: 1e-06s
now time elapsed:0s