使用boost

1. 获得Boost

进入Boost的网站(http://www.boost.org/) 下载boost_1_62_0.zip

2. 解压Boost

解压 boost_1_62_0.zip ,比如解压到 D:\Program Files\boost\boost_1_62_0

3. 仅需头文件的库

许多人会问:“我该如何安装Boost库?” 实际上,常使用的boost库函数是不需要安装的。

无需安装的库函数如下:

  • Boost.Chrono
  • Boost.Context
  • Boost.Filesystem
  • Boost.GraphParallel
  • Boost.IOStreams
  • Boost.Locale
  • Boost.MPI
  • Boost.ProgramOptions
  • Boost.Python
  • Boost.Regex
  • Boost.Serialization
  • Boost.Signals
  • Boost.System
  • Boost.Thread
  • Boost.Timer
  • Boost.Wave

需要安装的库函数如下:

  • Boost.DateTime
  • Boost.Graph
  • Boost.Random
  • Boost.Exception


4. 用Boost跑一个程序

我们用无需安装的库函数来运行一个程序,code如下,命名为: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) << " " ); } 

用 Visual Studio IDE 来运行

  • 新建一个项目 New > Project
  • 选择win32控制台 Visual C++ > Win32
  • 建立一个名为 “example” 的项目
  • 在项目属性 Properties 中添加包含目录 Configuration Properties > C/C++ > General > Additional Include Directories,例如D:\Program Files\boost\boost_1_62_0
  • 更改配置 将Configuration Properties > C/C++ > Precompiled Headers从* Use Precompiled Header (/Yu)* 改为* Not Using Precompiled Headers*。
  • 将写好的example.cpp添加到项目的源文件中
  • 最后build example,再bulid solution

OK了,按Ctrl+F5运行程序,在命令行中输入

1 2 3

那么应该输出

3 6 9

5. 使用需要安装的Boost库函数

Boost少数需要编译的库函数在windows下安装十分方便。首先,进入命令行模式,可以依次
Ctrl+R > cmd

在命令行中依次输入下面2行,从而将文件目录转到boost所在文件夹

D:

cd D:\Program Files\boost\boost_1_62_0 

然后再依次输入下面2行进行安装。注意:安装完先别关闭窗口

bootstrap

.\b2

安装完后会窗口有如下的信息:

 
 
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
    D:\Program Files\boost\boost_1_62_0
The following directory should be added to linker library paths:
    D:\Program Files\boost\boost_1_62_0\stage\lib

其中的两个路径后面需要依次添加到项目的包含目录库目录中,故请先别关闭窗口。

6. 使用需要安装的Boost库函数跑一个程序

将刚才的example.cpp中的内容换成如下程序:

#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; } } 

此外,新建一个名为jayne.txt的文档放在桌面,文档内容如下:

To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.

用 Visual Studio IDE 来运行

  • 进入example的项目属性 Properties
  • 添加库目录Configuration Properties > Linker > Additional Library Directories,例如D:\Program Files\boost\boost_1_62_0\stage\lib (根据安装显示的结果来添加,请见上文 第5节 )
  • 最后build example,再bulid solution

运行这个程序

在命令行窗口中输入:

[你的程序目录]\example.exe < [你的文件目录]\jayne.txt 

例如,我的是:

D:\VC_TEST\Boost\example\exmaple\Debug\example.exe < C:\Users\Administrator\Desktop\jayne.txt 

程序将输出:

Will Success Spoil Rock Hunter?

7. 在Clion中使用boost

 boost路径如上文讲的,在安装后会再cmd中显示

在项目中的CMakeLists.txt文件中添加

#添加头文件搜索路径
include_directories(D:\\local\\boost_1_65_0_beta1_gcc\\boost_1_65_0)

#添加库文件搜索路径 link_directories(D:\\local\\boost_1_65_0_beta1_gcc\\boost_1_65_0\\libs)

8. 参考网站

  1. http://www.boost.org/doc/libs/1_62_0/more/getting_started/windows.html#zip
  2. https://www.jianshu.com/p/004c99828af2
  3. https://blog.csdn.net/mhw828/article/details/77472421

 

猜你喜欢

转载自www.cnblogs.com/aoyihuashao/p/9092011.html