Boost 学习之旅
1.在windows上安装Boost
下载地址:[Boost 1.67.0](https://www.boost.org/users/history/version_1_67_0.html)
下载**boost_1_67_0.7z**或**boost_1_67_0.zip** 在合适的目录解压缩即可。
2.Boost文件目录结构
boost_1_67_0\ - 根目录
index.html - www.boost.org 指南
boost\ - 所有boost的头文件
lib\ - 编译好的lib库文件
libs\ - 测试,实例,Cpp源文件
status\ - 更多的测试
tools\ - 工具集
more\ - 政策文件
doc\ - boost文档
(1)$BOOST_ROOT 指的目录即为 ….\boost_1_67_0\此目录
(2)在IDE的include添加 \boost_1_67_0\根目录
而后可以使用
#include <boost/whatever.hpp>
//或
#include "boost/whatever.hpp"
3.Header-Only Libraries (仅有头文件的库)
大部分的Boost库,都是以头文件hpp给出,包含一些模板和inline函数。
一些增强型的库需要编译:
- Boost.Chrono
- Boost.Context
- Boost.Filesystem
- Boost.GraphParallel
- Boost.IOStreams
- Boost.Locale
- Boost.Log (see build documentation)
- 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 如果需要使用到to_string/from_string或者序列化或者使用 VisualC++ 6.x 或者Borland 是菜可能用到该二进制文件。
- Boost.Graph 如果需要解析 GraphViz files也需要二进制的
- Boost.Math for the TR1 and C99 cmath functions.
- Boost.Random 使用 random_device.
- Boost.Test 只使用头文件或者单独的编译。
-Boost.Exception provides non-intrusive implementation of exception_ptr for 32-bit _MSC_VER==1310 and _MSC_VER==1400 which requires a separately-compiled binary. This is enabled by #define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR.
4.使用Boost编译一个简单的程序
#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) << " ");
return 0;
}
5.使用Boost lib库
从源码编译boost
在boost_1_67_0目录下打开命令行
bootstrap // 生成Boost.Build 即 b2
.\b2
安装 Boost.Build
Boost.Build 是基于文本的系统,用于开发、测试和安装软件
1. 进入目录 tools\build\
2. 运行bootstrap.bat
3. 运行
b2 install --prefix=PREFIX
PREFIX 指向你要安装的目录
4.将PREFIX添加到系统环境变量
Boost.Build将它生成的所有中间文件放在构建目录中
在默认情况下, Boost.Build将创建一个bin.v2/子目录中。
将当前目录更改为Boost根目录并调用b2
b2 --build-dir=build-directory toolset=toolset-name --build-type=complete stage
6.使用Boost lib库
#include <boost/regex.hpp>
#include <iostream>
#include <string>
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;
}
}
编译时提示链接错误,在属性配置link中添加lib库目录