你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu下编译安装boost库

[日期:2013-07-19] 来源:Linux社区  作者:longcpp [字体: ]

环境:Ubuntu 12.04 32bit,boost 1.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,已经存在的脚本将会被自动备份。注意,boost 1.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™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)下

./b2 install

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

Boost程序库完全开发指南——深入C++“准”标准库高清PDF版下载http://www.linuxidc.com/Linux/2013-07/87574.htm 

#include "stdcpp.hpp"

#include <boost/timer.hpp>

 

using namespace boost;

 

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 EXIT_SUCCESS;

}

程序输出:

max timespan: 0.596523h

min timespan: 1e-06s

now time elapsed: 0s

相关阅读:

Ubuntu编译安装boost并在eclipse C/C++中使用 http://www.linuxidc.com/Linux/2011-04/34790.htm

-----------------------------------分割线-----------------------------------

我也在ubuntu下装了boost,不过遇到了一些问题:
www.linuxidc.com @www.linuxidc.com:~/Code$ g++ -lboost_filesystem -lboost_system main.cpp
/tmp/cclYtfBf.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x49): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x53): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x5d): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status

也就是说加了#include<boost/filesystem.hpp>这句之后马上出错,其他都没问题。不知到你有没有遇到这个问题呢?

答:

"boost/filesystem.hpp" 这个文件依赖于boost_system,编译的时候,需要 -lboost_system选项,但是你的编译选项里头已经指定该选项,为什么还会出现链接错误呢。我也纳闷了半天,后来发现,原来是-l选项的顺序问题。在g++的man帮助信息里面,我发现了下面的话:

引用It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

读完这段话,应该就明白出错原因了。所以,解决方案就是用下面的命令来编译:

引用g++ main.cpp -lboost_system -lboost_filesystem

更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

linux
相关资讯       Ubuntu安装boost 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款