Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148745
  • 博文数量: 54
  • 博客积分: 1732
  • 博客等级: 上尉
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-23 23:29
文章分类

全部博文(54)

文章存档

2011年(3)

2010年(26)

2009年(25)

分类: C/C++

2010-01-30 21:03:17

首先当然是下载boost源码:

将下载下来的文件解压:
$ tar -xf boost_1_41_0.tar.bz2
大部分的boost文件都只有头文件,不需要安装,因此将上面解压所得放入指定位置即可:
$ sudo cp -r boost_1_41_0/boost /usr/local/include
此时已经可以使用boost中的大部分了
$ vi my1stboost.cc
输入以下示意程序

#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) << " ");

    std::cout << std::endl;

    return 0;
    
}
编译运行:
$ c++ -Wall -Werror  my1stboost.cc
$ echo 1 2 3 | ./a.out
3 6 9

编译其他库:
首先回到boost库解压后的文件夹,之后运行:
$ ./bootstrap.sh
$ sudo ./bjam install
上述步骤默认安装在/usr/local中,如果需要指定安装文件夹,可以
$ ./bootstrap.sh --prefix=installation/destination
安装完毕后,做一个简单的测试:
$ vi regex.cc
输入以下程序:
#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;
    }
    std::cout << std::endl;

    return 0;
}

保存退出后
$ c++ -I /usr/local/include/ regex.cc -o regex /usr/local/lib/libboost_regex.a
$ ls
a.out  my1stboost.cc  regex  regex.cc
$ ./regex
Subjec: Re: hello        <= 用户输入
Subject: Re: hello       <= 用户输入
hello                    <= 程序输出

上述编译的时侯也可以通过-l选项链接:
$ c++ -I /usr/local/include/ -lboost_regex regex.cc -o regex
得到的结果与前文一致
 
本来想为boost库生成用于vim自动补全的tags文件,但是产生出来的文件达到1.5G,只好放弃了


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