分享

Boost Ubuntu下编译安装boost1.54.0 | Alex Zhou的程序世界

 昵称17588304 2014-08-19

Boost是一个功能强大、开源、跨平台、免费的c++程序库,被业界称为“准”c++标准库,能让你的c++开发更加简单,下面就开始下载安装Boost吧。
1. 下载Boost
首先去官网下载安装Boost库,我的系统是Ubuntu12.10,这里选择Unix平台,下载最新的库:boost_1_54_0.tar.gz。
解压安装包:
tar -zxvf boost_1_54_0.tar.gz
下面是boost1.54.0的根目录结构:

boost_1_54_0/ ……………..The “boost root directory”
index.htm ………A copy of www. 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

所有的Boost头文件都以.hpp为后缀名,要详细的了解Boost各种库,可以打开libs/index.html文件。

大部分Boost库,只需要包含它的.hpp文件(包含了声明和实现)即可,不需要编译成二进制库文件。
不过下面的这些库必须编译后才能使用。
Boost.Chrono
Boost.Context
Boost.Filesystem
Boost.GraphParallel
Boost.IOStreams
Boost.Locale
Boost.MPI
Boost.ProgramOptions
Boost.Python (see the Boost.Python build documentation before building and installing it)
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.System
Boost.Thread
Boost.Timer
Boost.Wave
下面这些库是可选的,只有你在使用某些特定功能时才需要先编译成二进制库文件,具体哪些功能等碰到了再说,先简单了解就行。
Boost.DateTime
Boost.Graph
Boost.Math
Boost.Random
Boost.Test
Boost.Exception

先来实现一个简单的demo,只需要包含Boost相关头文件即可,不需要二进制库。
创建一个example.cpp文件。

1
2
3
4
5
6
7
8
9
10
11
12
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;
    std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << " ");
    std::cout << std::endl;
}

该程序很简单,它从标准输入中读取一些数字,然后每个数x3后再输出。
编译:(-I表示指定#include头文件的目录,编译时会从该目录去查找)
g++ -I ~/boost/boost_1_54_0/ ./example.cpp -o example
运行:
echo 1 2 3 | ./example
输出:
3 6 9

2. 编译安装Boost
cd boost_1_54_0
编译前需要配置,输入下面的命令:
sudo ./bootstrap.sh
配置后会提示你使用b2(老版本是使用bjam编译)编译,开始编译安装,这里选择完全安装,自定义安装以后再研究,先不浪费时间在这些细节上,输入命令:
sudo ./b2 install
编译安装完成后,会把boost头文件拷贝到/usr/local/include/目录下,库文件在/usr/local/lib/下。
注意:编译安装boost前,得先安装gcc,使用sudo apt-get install build-essential即可。
build-essential依赖于下面这些软件包,所以安装build-essential时,这些软件也会被安装,很方便。
|Depends: libc6-dev
Depends: libc6-dev
Depends: gcc
Depends: g++
Depends: make
Depends: dpkg-dev

使用命令apt-cache depends build-essential可以查看依赖关系。
再来实现一个简单的demo,需要链接二进制库文件。
创建example2.cpp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <boost/regex.hpp>
#include <string>
#include <iostream>
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;  
    }
  }
}

上面的程序使用了boost的正则表达式库,从标准输入中获取符合条件的信息,然后打印出来。创建一个test.txt文件作为输入内容,内容如下:
m: Alex Zhou
Subject: Will Success?

See subject.

如果打印出Will Success,则说明程序运行成功。
编译:
g++ -I/usr/local/include/ example2.cpp -o example2 /usr/local/lib/libboost_regex.a
运行:
./example2 < test.txt
输出:
Will Success?

1

转载请注明来自:Alex Zhou的程序世界,本文链接:http:///boost-2/1214.html


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多