现在的位置: 首页 > 综合 > 正文

boost1.55.0安裝及配置

2018年02月20日 ⁄ 综合 ⁄ 共 4140字 ⁄ 字号 评论关闭

Come From:

http://blog.163.com/yungang_z/blog/static/175153133201262445539767/

linux平台下要编译安装除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel 和python-dev,因此在安装前应该先保证这两个库已经安装:

#sudo apt-get install gcc g++ bzip2 libbz2-dev bzip2-libs python-devel

下載boost1.55.0

#tar xvzf boost_1_55_0.tar.gz

进入boost_1_55_0目录:

#cd boost_1_55_0

然后是编译安装,boost源码包中有配置脚本,直接用就可以:

#sh ./bootstrap.sh

Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2 Detecting Python version... 2.6 Detecting Python root... /usr Unicode/ICU support for Boost.Regex?... not found. Generating Boost.Build configuration in project-config.jam... Bootstrapping is done. To build, run: ./b2 To adjust configuration, edit 'project-config.jam'. Further information: - Command line help: ./b2 --help - Getting started guide: http://www.boost.org/more/getting_started/unix-variants.html - Boost.Build documentation: http://www.boost.org/boost-build2/doc/html/index.html

接下来就是编译,重点关注是否编译成功:

#./b2

然后就是漫长的等待,如果最后出现:

The Boost C++ Libraries were successfully built! The following directory should be added to compiler include paths: /home/gang/BAK/boost_1_50_0 The following directory should be added to linker library paths: /home/gang/BAK/boost_1_50_0/stage/lib

表示编译成功,如果没有成功,就需要回查看哪里出现error,再安装相应的库,
最后就是安装:

#./b2 install --prefix=/usr/local

安装后的头文件在/usr/local/include/boost里面,而相应的库在/usr/local/lib/libboost_*

-------------------------------------------------------------------------------------------------------------------------------------------------------

Boost Ubuntu下编译安装boost1.54.0

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.boost.org 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库,只需要包含它的头文件即可,头文件已经包含了模板和inline函数,不需要编译成二进制库文件。
不过下面的这些库必须编译后才能使用。
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文件。

#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文件

#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?

抱歉!评论已关闭.