天天看點

vs2010使用boost庫

今天抽時間學習了一下boost庫,用c++ 做算法,自己再去造輪子實在是浪費時間,學習boost未來工作能直接上手。

比如caffe就直接使用了boost庫, 這裡邊常用的 對于時間操作,字元串操作,檔案操作,智能指針等,熟練使用這些能大大的加速算法的開發時間。

一、參考資料:

   1、 http://download.csdn.net/detail/nuoshueihe/5344610    boost程式庫完全開發指南--深入C++标準庫    書,寫的還不錯,但是不如看英文文檔更直接

2、http://www.boost.org/  boost 官網,在這裡下載下傳Version 1.60.0,以及其文檔

3、http://blog.csdn.net/kangroger/article/details/39393769  參考編譯文檔

二、編譯過程:

(1)首先下載下傳源代碼:http://softlayer-dal.dl.sourceforge.net/project/boost/boost/1.60.0/boost_1_60_0.zip

解壓到某個目錄,我解壓到了D盤根目錄:D:\boost_1_60_0

(2)生成bjam.exe可執行檔案

用VS2010指令行

vs2010使用boost庫

進入到到目錄D:\boost_1_56_0,運作booststrap.bat得到:

vs2010使用boost庫

這時在目錄D:\boost_1_56_0生成了b2.exe、bjam.exe、project-config.jam檔案。

(3)用bjam.exe編譯

運作指令bjam stage  --toolset=msvc-10.0 --build-type=complete --stagedir="D:\boost_1_56_0\bin\vc10"  link=static runtime-link=shared threading=multi debug release

參考部落格中:

運作指令bjam stage --without-Python --toolset=msvc-10.0 --build-type=complete --stagedir="D:\boost_1_56_0\bin\vc10"  link=static runtime-link=shared threading=multi debug release

 --without-Python 我電腦上提示沒有Python,是以把這個庫去掉。

bjam可以參考http://blog.chinaunix.net/uid-22301538-id-3158997.html

stage表示隻生成庫(dll和lib),用install的話還會生成包含頭檔案的include目錄。

toolset指定編譯器,VS2010用msvc-10.0。

without/with表示不編譯/編譯哪些庫。

stagedir,當使用stage時用stagedir,使用install用prefix,表示編譯生成檔案的路徑。路徑的命名最好和編譯器相關,編譯管理。

link指定生成動态連結庫或靜态連結庫。生成動态連結庫需使用shared方式,生成靜态連結庫需使用static方式。

runtime-link,動态/靜态連結C/C++運作時庫。有shared和static兩種方式,這樣runtime-link和link一共可以産生4種組合方式。

threading,單/多線程編譯。

debug/release,編譯debug/release版本。一般都是程式的debug版本對應庫的debug版本,是以兩個都編譯。

差不多需要一小時,編譯完成(中間會有警告)。

編譯好後,在根目錄會有個bin.v2檔案夾,是編譯過程中的臨時檔案夾,很大,可以手動删除。

vs2010使用boost庫

(4)在VS中使用Boost庫

建立工程後需要把Boost庫包含到工程中,右鍵選擇屬性,在VC++目錄的“包含目錄”中添加Boost的根目錄,在“庫目錄”添加剛剛編譯生成的位置再加上路徑lib。

vs2010使用boost庫

之後包好頭檔案即可使用,例如一個多線程的測試:

  1. #include "stdafx.h"
  2. #include "boost/thread.hpp"
  3. #include <iostream>
  4. using namespace std;
  5. void thread_func()
  6. {
  7. cout << "This is my first boost,thread function test" << endl;
  8. }
  9. int _tmain( int argc, _TCHAR* argv[])
  10. {
  11. boost::function< void()> func(thread_func);
  12. boost:: thread t(func);
  13. t.join();
  14. system( "puase");
  15. return ;
  16. }

測試 可以運作,安裝成功

繼續閱讀