笔者写本文时,最新版的Boost库是boost_1_58_0,到这个版本,Boost已经很完备了,是C++标准库的很好的补充和加强。本文和大家一起看一下如何在VS2013中使用Boost库。
1. 下载并“安装”Boost库
首先,在Boost项目首页 http://www.boost.org/ 找到下载页面,下载其中的boost_1_58_0文件,解压后放到易找的一个硬盘根目录下,我解压后放置的目录结构是:
D:\boost\boost_1_58_0\
boost_1_58_0这个目录就是boost库的主目录($BOOST_ROOT),它的详细结构如下:
boost_1_58_0\ .................The “boost root directory”
index.htm .........A copy of www.boost.org starts here
boost\ .........................All Boost Header files
lib\ .....................precompiled library binaries
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. bjam, quickbook, bcp
more\ ..........................Policy documents, etc.
doc\ ...............A subset of all Boost library docs
那么,如何在VC2013的项目中使用boost库呢?说白了,就是让VC2013的项目在编程连接项目的时候,能找到引用的boost库文件。
思路很明显,那就先新建一个C++项目吧
2. 新建并设置boost项目
打开VS2013,新建Visual C++项目,选择其中的 Win32 Console Application (Win32控制台应用程序),建立完成后,右击右侧项目管理器里的项目,打开项目属性对话框。
项目属性对话框里,左侧选择 VC++ Directories,把刚刚的 D:\boost\boost_1_58_0\ 添加到 Include Directories中。
像上面,设置完boost库的位置后,项目中就可以直接引用boost库文件了,如
- #include <boost/lambda/lambda.hpp>
在刚刚的项目中,我们添加一个 C++ File (.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) << " " );
- }