筆者寫本文時,最新版的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) << " " );
- }