windows下boost庫的基本使用方法


因為boost都是使用模板的技術,所以所有代碼都是寫在一個.hpp頭文件中。這樣boost中的大部分內容是不需要編譯生成相應的鏈接庫,只需要設置下面的包含目錄(或者設置一下環境變量),在源文件中包含相應的頭文件就可以使用了。少部分庫需要生成鏈接庫來使用。

下面介紹完整安裝boost庫的方法:

1、首先到boost官網去下載最新的版本的boost庫:

http://www.boost.org/


2、解壓文件,在命令提示符中打開到boost庫的根目錄下:

雙擊bootstrap.bat文件,生成bjam.exe,執行以下命令:

bjam --toolset=msvc --build-type=complete stage

或者直接雙擊bjam.exe.

等待程序編譯完成,大約要兩個小時左右,會在boost根目錄下生成bin.v2和stage兩個文件夾,其中bin.v2下是生成的中間文件,大小在2.7G左右,可以直接刪除。stage下才是生成的dll和lib文件。


3、打開vs:

視圖->屬性管理器->當前項目->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


至此環境就配置好了,下面測試一下:

  1. <span style="font-size:14px;"><pre name="code" class="cpp"><span style="font-family:Courier New;">#include <cstdlib>  
  2.   
  3. #include <iostream>  
  4. #include <vector>  
  5. #include <iterator>  
  6. #include <algorithm>  
  7.   
  8. #include <boost/timer.hpp>  
  9. #include <boost/progress.hpp>  
  10.   
  11. #include <libs/date_time/src/gregorian/greg_names.hpp>  
  12. #include <libs/date_time/src/gregorian/date_generators.cpp>  
  13. #include <libs/date_time/src/gregorian/greg_month.cpp>  
  14. #include <libs/date_time/src/gregorian/gregorian_types.cpp>  
  15.   
  16. #include <boost/date_time/posix_time/posix_time.hpp>  
  17.   
  18. using namespace boost;  
  19.   
  20. int main()  
  21. {  
  22.     boost::timer t;  
  23.   
  24.     boost::progress_display pd(100);  
  25.   
  26.     for (int i = 0; i < 100; ++i) //進度條  
  27.     {  
  28.         ++pd;  
  29.     }  
  30.   
  31.     boost::gregorian::date dt(2009, 12, 8); //date_time 庫  
  32.     assert(dt.year() == 2009);  
  33.     assert(dt.day() == 8);  
  34.     boost::gregorian::date::ymd_type ymd = dt.year_month_day();  
  35.     std::cout<<"\n"<<ymd.year<<"/"<<ymd.month<<"/"<<ymd.day<<" the day is "  
  36.         <<dt.day_of_year() <<" days of this year"<< std::endl;  
  37.   
  38.     std::cout << boost::gregorian::to_iso_extended_string(dt) << std::endl; //轉換為其他格式  
  39.     std::cout << boost::gregorian::to_iso_string(dt) << std::endl;  
  40.     std::cout << boost::gregorian::to_simple_string(dt) << std::endl<<std::endl;  
  41.   
  42.     //對數組排序操作  
  43.     std::vector<int> test_vc(100);  
  44.     std::vector<int>::iterator beg_it = test_vc.begin();  
  45.     std::vector<int>::iterator end_it = test_vc.end();  
  46.     std::srand(std::time(NULL));  
  47.   
  48.     std::for_each(beg_it, end_it, [](int& n){n = rand(); });  
  49.     std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));  
  50.     std::cout << std::endl << std::endl;  
  51.     std::sort(beg_it, end_it, std::greater<int>());  
  52.     std::copy(beg_it, end_it, std::ostream_iterator<int>(std::cout, " "));  
  53.     std::cout << std::endl<<std::endl;  
  54.   
  55.     boost::posix_time::ptime pt(boost::gregorian::date(2005, 2, 6));  
  56.   
  57.     std::cout << t.elapsed() << "s" << std::endl; //程序運行時間  
  58.   
  59.     system("pause");  
  60.   
  61.     return 0;  
  62. }</span></span>  





程序正確運行:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM