由於boost都是使用模板的技術,因此全部代碼都是寫在一個.hpp頭文件中。這樣boost中的大部份內容是不須要編譯生成相應的連接庫,只須要設置下面的包含目錄(或者設置一下環境變量),在源文件中包含相應的頭文件就能夠使用了。少部分庫須要生成連接庫來使用。html
下面介紹完整安裝boost庫的方法:ios
一、首先到boost官網去下載最新的版本的boost庫:bootstrap
http://www.boost.org/windows
二、解壓文件,在命令提示符中打開到boost庫的根目錄下:數組
雙擊bootstrap.bat文件,生成bjam.exe,執行如下命令:post
bjam --toolset=msvc --build-type=complete stage測試
或者直接雙擊bjam.exe.ui
等待程序編譯完成,大約要兩個小時左右,會在boost根目錄下生成bin.v2和stage兩個文件夾,其中bin.v2下是生成的中間文件,大小在2.7G左右,能夠直接刪除。stage下才是生成的dll和lib文件。this
三、打開vs:spa
視圖->屬性管理器->當前項目->Debug|Win32->Microsoft.Cpp.Win32.user雙擊
在彈出的屬性對話框中:
通用屬性->VC++目錄:"包含目錄": boost的根目錄,例: D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0
"庫目錄": stage下的連接庫目錄,例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
通用屬性->連接器->常規:"附加庫目錄":同上面的"庫目錄",例:D:\Visual Stdio 2013\lipeng\boost\boost_1_58_0\stage\lib
至此環境就配置好了,下面測試一下
#include <iostream> #include <vector> #include <iterator> #include <algorithm> #include <boost/timer.hpp> #include <boost/progress.hpp> #include <libs/date_time/src/gregorian/greg_names.hpp> #include <libs/date_time/src/gregorian/date_generators.cpp> #include <libs/date_time/src/gregorian/greg_month.cpp> #include <libs/date_time/src/gregorian/gregorian_types.cpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace boost; int main() { boost::timer t; boost::progress_display pd(100); for (int i = 0; i < 100; ++i) //進度條 { ++pd; } boost::gregorian::date dt(2009, 12, 8); //date_time 庫 assert(dt.year() == 2009); assert(dt.day() == 8); boost::gregorian::date::ymd_type ymd = dt.year_month_day(); std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is " <<dt.day_of_year() <<" days of this year"<< std::endl; std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //轉換爲其餘格式 std::cout << boost::gregorian::to_iso_string(dt) << std::endl; std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl; //對數組排序操做 std::vector<int> test_vc(100); std::vector<int>::iterator beg_it = test_vc.begin(); std::vector<int>::iterator end_it = test_vc.end(); std::srand(std::time(NULL)); std::for_each(beg_it, end_it, [](int& n){n = rand(); }); std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl << std::endl; std::sort(beg_it, end_it, std::greater<int>()); std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl<<std::endl; boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6)); std::cout << t.elapsed() << "s" << std::endl; //程序運行時間 system("pause"); return 0; }
程序正確運行: