Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141404
  • 博文数量: 40
  • 博客积分: 1131
  • 博客等级: 少尉
  • 技术积分: 459
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-16 11:50
文章分类
文章存档

2012年(2)

2011年(38)

我的朋友

分类: 嵌入式

2011-08-02 11:25:19

泛型技术和面向对象技术填补了逻辑上的空缺,从代码来讲,一个是竖向累积,一个是横向扩展。

Boost在STL的基础上发展,成为了一个集泛型技术之大成者的库。

hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再将cpp加入到project中进行编译。
而实现代码将直接编译到调用者的obj文件中,不再生成单独的obj.
采用hpp将大幅度减少调用project中的cpp文件数与编译次数,也不用再发布烦人的lib与dll,因此非常适合用来编写公用的开源库。

使用boost(Linux)

最好的资料是package里的index.htm

1, 下载解压到 ~/bin/boost/boost_1_47_0
    目录如下,

boost_1_47_0/ .................The “boost root directory” index.htm ........A copy of starts here boost/ .....................All Boost Header files libs/ .....Tests, .cpps, docs, etc., by library index.html ........Library documentation starts here algorithm/ any/ array/ …more libraries… status/ .........................Boost-wide test suite tools/ ....Utilities, e.g. Boost.Build, quickbook, bcp more/ ..........................Policy documents, etc. doc/ ...............A subset of all Boost library docs

2, The first thing many people whant to know is, “how do I build Boost?”.
    The good news is that often, there’s nothing to build.
   
The only Boost libraries that must be built separately are:
    Filessystem, GraphParallel, IOStreams, MPI, ProgramOptions, Python, Regex, … Thread…

3, Build a Simple Program using Boost with a header-only library

    To keep things simple, let’s start by using a header-only library.
    /* example.cpp – illustrate the Boost */
    #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) << “ ”);
    }

    Now, built it.
    $g++ –I ~/bin/boost/boost_1_47_0 example.cpp –o example

    To test the result type,
    $./example
    $1 2 3
    The result is,
    $3 6 9

4, 全编译,为了使用某些库
    $./bootstrap.sh –help (查看编译选项)
    $./bootstrap.sh --preifx=/home/huang/bin/boost/built (安装路径(必须是绝对路径)/include & /lib, 默认编译器是gcc, user-config.jam)
    $./b2 install (开始编译,并install 库)

    编译成功后,我们就可以使用这个目录来编译应用程序。而不再需要 Boost root directory目录了,
    因为全部的include和lib都已经编译和拷贝到这个目录了。

5, 一个需要库的例子,Regex

    /* 取email的subject, It uses Regex. */
    #include
    #include
    #include

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

    $g++ –I ~/bin/boost/built/inlcude –L ~/bin/boost/built/lib –lboost-regex-gcc43-mt-d Regex.cpp –o Regex

阅读(1537) | 评论(0) | 转发(1) |
0

上一篇:线程

下一篇:正则表达式 - Boost.Regex

给主人留下些什么吧!~~