在vs2013中引用boost庫

關於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庫文件了,如
  1. #include <boost/lambda/lambda.hpp>
複製代碼
3. 編寫一個簡單的引用boost庫的C++程序

在剛剛的項目中,我們添加一個 C++ File (.cpp),其中輸入如下代碼:

  1. #include <boost/lambda/lambda.hpp>  
  2. #include <iostream>  
  3. #include <iterator>  
  4. #include <algorithm>  
  5.   
  6. int main()  
  7. {  
  8. using namespace boost::lambda;  
  9. typedef std::istream_iterator<int> in;  
  10.   
  11. std::for_each(  
  12. in(std::cin), in(), std::cout << (_1 * 3) << " " );  
  13. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章