Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1804189
  • 博文数量: 496
  • 博客积分: 12043
  • 博客等级: 上将
  • 技术积分: 4778
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 14:26
文章分类

全部博文(496)

文章存档

2014年(8)

2013年(4)

2012年(181)

2011年(303)

2010年(3)

分类: C/C++

2011-09-29 17:18:10

Linux上安装使用boost入门指导获得boostboost分布只需要头文件的库使用boost建立一个简单的程序准备使用boost二进制文件库把你的程序链接到boost库

  1.获得boost

  boost_1_46_1.tar.bz

  2解压

  2.boost分布

  boost_1_46_1.........................boost根目录

  boost/.....................................所有boost头文件

  libs/........................................Tests,.cpps,docs等的库文件注意:

  (1)boost根目录(通常是/usr/local/boost_1_46_1)想到$BOOST_ROOT变量中(2)编译程序时如果用到 boost库,需要指定头文件路径-I$BOOST_ROOT(3)因为所有头文件都在boost文件夹下,并且头文件都是hpp后缀, 所#include形如:

  #include

  3.只需要头文件的库绝大多数的boost库都是header-noly的:它们完全由包含模板和inline函数的头文件组成,不需要单独编译和二进制库文件,也不需要链接时特别对待。

  只有下面的boost库必需单独built:

   Boost.FilesystemBoost.GraphParallelBoost.IOStreamsBoost.MPIBoost.ProgramOptionsBoost.Python (see the Boost.Python build documentation before building and installing it)Boost.RegexBoost.SerializationBoost.SignalsBoost.SystemBoost.ThreadBoost.Wave 下面这些单独built是可选(optional)的:

  Boost.DateTime  Boost.Graph  Boost.Math  Boost.Random  Boost.Test4. 使用boost建立简单的程序下面的程序(example.cc)只用到header-only库。它是从标准输入中读入一串整数,使用 Boost.Lambda每个数乘以3后输出。

  view sourceprint?

  #include

  #include

  #include

  #include

  int main(){using namespace boost::lambda;typedef std::istream_iterator in;std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << " " );}

  编译:g++  -I$BOOST_ROOT  example.cc  -o  example运行:echo 1 2 3 | ./example5.准备使用boost二进制库如果你的程序用到需要单独编译的boost库,你需要首先获得这些二进制库文件。

  5.1编译安装所有二进制库文件cd $BOOST_ROOT./bootstrap.sh  --help./bootstrap.sh  --prefix=/usr/local    ##其实默认情况下prefix的值就是/usr/local此时生成了bjam可执行文件,这个东西就 是用来编译boost库的。

  ./bjam  install5.2仅安装指定的二进制库文件下面均使用系统默认的编译器,即Linux上的gcc。

  5.2.1安装Boost.buildBoost.Build是一个用于开发、、安装软件的基于文本的系统。Boost.Build的生成安装步骤:

  (1)cd $BOOST_ROOT/tools/build/v2(2)./bootstrap.sh(3)./bjam  install  --prefix=/usr/local/      ##prefix是Boost.Build安装位置(4)把prefix/bin放到PATH中           ##当然/usr/local/bin已经PATH中了5.2.2调用bjam时不指定toolset则使用系统默认的编译器。如果你 的Linux上装了不同版本的gcc,则使用toolset选项时可以指定版本号:toolset=gcc-4.45.2.3指定build路径,通过 --build-dir=/path选项,不指定时默认在当前路径下创建bin.v2文件夹,把生成的文件放在其内。

  5.2.4调用bjamcd  $BOOST_ROOTbjam  --build-dir=./build-boost  toolset=gcc   stage上面的命令将创建static and shared non-debug multi-threaded variants of the libraries.如果要建立所有的variants,请使用"--build-type=complete"选项。

  所有的boost二进制库文件将放在stage/lib/下,如果你要另外指定路径,请使用“--stagedir=directory"选项。

  注意为节省build时间,你可能需要少build一些库文件:

  查看库文件名称  --show-libraries限制build哪些库  --with-libraryname或者--without- libraryname选择特定的build variant  adding release or debug to the command line友情提示:Boost.Build会生成很多报告输出,如果你能保证建立过程不出错误,你可以禁止这些输出以节省时间。方法:在命令后追 加”>build.log  2>&1"6.把你的程序链接到boost库下面的程序是从邮件中抽取“主题”内容,它用到了Boost.Regex库,这个库是需要单独 编译的。

  view sourceprint?

  #include

  #include

  #include

  int main(){std::string line;boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );while (std::cin){std::getline(std::cin, line);boost::smatch matches;if (boost::regex_match(line, matches, pat))std::cout << matches[2] << std::endl;}

  }

  编译:

  方法A:g++  -I$BOOST_ROOT  example.cc -o example  $BOOST_ROOT/stage/lib/ -lboost_regex.a方法B:g++  -I$BOOST_ROOT  example.cc -o example  -L$BOOST_ROOT/stage/lib/ -lboost_regex当你要使用多个库源于一个路径时使用方法B就省力了(paid off)。注意到方法B中并没有指定.a(静态库)还中.so(动态库),系统将自动地帮你选择使用静态库还是动态库,当然你可以通过选项 “-static"显示地指定。

  6.1库文件的命名方式拿libboost_regex-gcc34-mt-d-1_36来说:

  lib....................................通用前缀 boost_regex.....................库名gcc34...............................编译 时使用的toolset是gcc-3.4mt...................................编译时是支持多线程的d/s/g /y/p..........................ABI tag1_36................................Tag version6.2运行我们的程序首先新建一个文本文件mail.txtview sourceprint?

  To: George ShmidlapFrom: Rita MarloweSubject: Will Success Spoil Rock Hunter?

  ---See subject.

  如果我们的程序链接到了一个共享动态库,我们需要让系统知道到哪儿去加载它。请看我的~/.bashrc文件:

  view sourceprint?

  #boostexport BOOST_ROOT="/usr/local/boost_1_46_1"export LD_LIBRARY_PATH="/usr/local/boost_1_46_1/stage/lib:$LD_LIBRARY_PATH"运行程 序:./example  <  mail.txt应该输出view sourceprint?

Will Success Spoil Rock Hunter?

阅读(926) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~