Linux下BOOST库的安装与使用

本文简介Linux环境下C++非标准库boost的安装与使用。

1.下载boost安装包(http://www.boost.org/)并解压,boost_1_61_0.tar.gz
tar -zxvf boost_1_61_0.tar.gz
2. 执行引导程序,配置安装环境
进入解压后的文件目录
cd boost_1_61_0
查看安装引导程序bootstrap.sh,执行该脚本的附加选项有:
–prefix=PREFIX install Boost into the given PREFIX
在bootstrap.sh脚本中将其默认配置为:PREFIX=/usr/local
也就是说默认安装到/usr/local,这里我们不进行修改,虽然一些安装教程建议将默认路径改为/usr,坚持安装到/usr/local路径下的理由是:能够更好的管理这些自行安装的软件。这里还是解释一下,为什么这么做?
首先,usr是Unix Software Resource的缩写,也就是Unix操作系统软件资源所放置的目录:
/usr/include:c/c++等程序语言的头文件及相关包含文档的目录
/usr/lib:包含各应用软件的函式库、目标档案(object file),以及不被一般使用者惯用的执行档或脚本(script)。
将默认路径改为/usr,安装后会将头文件和库文件放到/usr目录下的include和lib目录下,这样编译程序时不用担心找不到头文件这以及链接时找不到库文件。但是这样的话,会导致头文件(或库文件)和其他头文件(或库文件)‘杂糅’在一起,不便于管理。
如果将其安装到/usr/local目录下,安装完后boost头文件位于/usr/local/include/boost,库文件位于/usr/local/lib。编译时如果找不到头文件,只要加上头文件路径一起编译就好了,如果你嫌麻烦,那就用makefile管理吧。

执行引导脚本bootstrap.sh

[root@localhost boost_1_61_0]# ./bootstrap.sh 
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86/b2
Detecting Python version... 2.4
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
....

3.安装
执行引导程序后,会生成两个用于编译和安装的可执行文件 b2和bjam,这里选择b2进行安装,安装选项Targets and Related Options:

install Install headers and compiled library files to the
======= configured locations (below).

–prefix= Install architecture independent files here.
Default; C:\Boost on Win32
Default; /usr/local on Unix. Linux, etc.

–exec-prefix= Install architecture dependent files here.
Default;

–libdir=

Install library files here.
Default; /lib

–includedir= Install header files here.
Default; /include

stage Build and install only compiled library files to the
===== stage directory.

–stagedir= Install library files here
Default; ./stage认配置,直接安装
[root@localhost boost_1_61_0]# ./b2 install

4.验证使用

/*
 *递归遍历boost头文件所在目录下文件
 */
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>

using namespace std;
using namespace boost;

int main()
{
    string strPath = "/usr/local/include/boost";
    filesystem::recursive_directory_iterator itEnd;
    for(filesystem::recursive_directory_iterator itor( strPath.c_str() ); itor != itEnd ;++itor)
    {
        filesystem::path filePath(*itor);
        cout<<filePath.filename()<<endl; 
        //在本版本的boost中,filename()返回的是path对象,如果想要获得string类型的文件名,需要使用path的方法string();即filePath.filename().string()
    }
    return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值