關于Boost的盛名我就不多費口舌了,它是一個經過千錘百煉、可移植、提供源代碼的C++庫,作為标準庫的後備,是C++标準化程序的發動機之一。 Boost庫由C++标準委員會庫工作組成員發起,其中有些内容有望成為下一代C++标準庫内容。在C++社群中影響甚大,其成員已經有好幾千人了。 Boost庫為我們帶來了最新、最酷、最實用的技術,是不折不扣的“準”标準庫。
筆者寫本文時,最新版的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>
複制代碼 3. 編寫一個簡單的引用boost庫的C++程式
在剛剛的項目中,我們添加一個 C++ File (.cpp),其中輸入如下代碼:
[cpp] view plain copy print ?
- #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) << " " );
- }