1. 概述
最近打算學習一下Boost庫,這裡記錄一下Boost庫的安裝的方法,IDE涉及DEV C++和VS 2008。
2. 下載
直接下載連結:參見網頁 http://www.boost.org/users/history/version_1_46_1.html,這裡的連結網速很慢,78MB的東西,下載了10%左右,就定住了,試了三次都是這樣,基本放棄了這個下載連結。
SVN下載連結,檔案儲存在“D:\Boost”下:參見網頁 http://www.boost.org/users/download/,有一定速度,下載使用了49分鐘,傳了115.99MB,版本為1.47,我看了下下載的檔案夾,嚇了我一跳:檔案夾大小501MB,佔用空間3.15GB。趕緊去網上查了下,有的人說對於1.46版本的Boost,如果完全編譯後,可能需要12-15GB的空間,這樣就不奇怪了,雖然還沒編譯就佔了3GB。
3. VS2008 Boost庫編譯(-vc9)
首先,編譯bjam,在命令列下,運行bootstrap.bat -vc9
然後,編譯庫。在命令列下,運行:
bjam stage --toolset=msvc-9.0 --without-graph --without-graph_parallel --without-math --without-mpi --without-python --without-serialization --without-wave --stagedir="D:\Boost\bin\vc9" link=static runtime-link=shared threading=multi debug release
bjam stage --toolset=msvc-9.0 --without-graph --without-graph_parallel --without-math --without-mpi --without-python --without-serialization --without-wave --stagedir="D:\Boost\bin\vc9" link=static runtime-link=static threading=multi debug release
編譯用了50分鐘左右,產生了303MB的檔案。
4. VS2008 Boost庫配置
Tools -> Options -> Projects and Solutions -> VC++ Directories
在Library files中,增加D:\Boost\bin\vc9\lib
在Include files中,增加D:\Boost\
其中,Library的目錄就是前面編譯產生的那些庫檔案儲存到的位置
其中,Include的目錄隨著Boost的不同版本會不同,現在1.47版本只要指定為D:\Boost即使用SVN下載Boost的檔案夾就可以了。
5. VS2008 Boost庫測試
View Code
#include "stdafx.h"
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>using namespace std;
using namespace boost;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"請輸入您的生日,格式\"YYYY-MM-DD\":";
string strBirthday;
cin>>strBirthday;
try
{
gregorian::date birthday( gregorian::from_simple_string(strBirthday) );
gregorian::date today( gregorian::day_clock::local_day() );
gregorian::days days_alive = today - birthday;
if( days_alive < gregorian::days(0) )
{
cout<<"哇,還沒出生就能用電腦了,真厲害"<<endl;
}
else
{
cout<<"您在這個世界上出現了:"<< days_alive.days()<< "天了" << endl;
}
}
catch( gregorian::bad_year& e )
{
cerr<< e.what() << endl;
}
catch( gregorian::bad_day_of_month& e )
{
cerr<< e.what() << endl;
}
catch( gregorian::bad_day_of_year& e )
{
cerr<< e.what() << endl;
}
catch(...)
{
cerr<<"Error!"<<endl;
}
system( "pause" );
return 0;
}
6. DEV C++ Boost庫編譯
首先,設定gcc的環境變數。在我的電腦上點擊右鍵,選擇Properties功能表項目。然後在彈出的對話方塊中選擇Advanced頁,點擊Environment Variables按鈕。之後進行如下設定,選擇PATH,然後點擊Edit按鈕,在最後加上DEV-C++編譯器的路徑,如C:\Program Files\DEV-CPP\Bin,路徑之間用分號分隔。 設定完畢點擊OK按鈕儲存。
然後,編譯bjam,運行bootstrap.bat -gcc
接著,編譯boost庫:
bjam stage --toolset=gcc --without-graph --without-graph_parallel --without-math --without-mpi --without-python --without-serialization --without-wave --stagedir="D:\Boost\bin\gcc" link=static runtime-link=shared threading=multi debug release
bjam stage --toolset=gcc --without-graph --without-graph_parallel --without-math --without-mpi --without-python --without-serialization --without-wave --stagedir="D:\Boost\bin\gcc" link=static runtime-link=static threading=multi debug release
7. DEV C++ Boost庫配置
在Toos->Compiler Options->Directories->C++ Include中,增加D:\Boost
8. DEV C++ Boost庫測試
第一種,只要包含該標頭檔即可,就能使用該標頭檔中的所有函數。
View Code
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
using namespace boost::lambda;
typedef istream_iterator<int> in;
for_each(in(cin), in(), cout << (_1 * 3) << " ");
return 0;
}
第二種,需要建立一個project,而不能只是編譯單個的c++檔案,因為在dev c++中,只有在project中才能設定linker的參數。 建立project後,“Project"-->"Project Options"-->"Parameters"選項卡--->在linker框中添加:"-lboost_regex-mt",告訴linker在連結的時候,連結regex庫。
View Code
#include <boost/regex.hpp>
#pragma comment(lib,"D:\boost\boost_1_42_0\stage\libboost_regex-meg34-mt.lib")
#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[1]<<" "<<matches[2] << std::endl;
}
return 0;
}
9. 參考文獻
Boost下載安裝編譯配置使用指南(含Windows和Linux) http://kb.cnblogs.com/a/1485890/
VS2008中編譯Boost 1.38 http://blog.csdn.net/wrx_2009/archive/2009/06/04/4242841.aspx
VS2008 Team System 安裝Boost庫 http://www.cookbus.com/show-121-1.html
boost在DevC++中的安裝過程 http://blog.csdn.net/suwei19870312/archive/2011/03/13/6246400.aspx