一:安裝MinGW
1、下載下傳MinGW
2、安裝MinGW,在系統變量中添加一個MingG_home, 值為MinGW的路徑,并在 “PATH” 中添加 %MinG_home%\bin;
3、測試:在cmd下,輸入gcc -v,會顯示版本資訊
二 :編譯安裝boost
1、下載下傳并解壓boost,進入解壓目錄後執行 bootstarp.bat gcc
2、編譯完後生成bjam.exe後執行如下指令
D:\boost\boost_1_62_0>bjam install --toolset=gcc --prefix="d:\program\boost" link=static runtime-link=shared runtime-link=static threading=multi debug release >log.txt
三codeblock配置
1、在Codeblocks中使用Boost.定義全局變量boost,注意base,include和lib的填寫,
設定 Code::Blocks中的全局變量boost
Setting->Global Variables
i. 單擊New按鈕,建立boost全局變量
ii. 設定 base: D:\program\boost\
iii.設定include:D:\program\boost\include\boost-1_62
iv.設定lib: D:\program\boost\lib\
如圖所示:
2、設定編譯器,
Setting->Compiler...
設定Search directories
Compiler 标簽Add一項内容 $(#boost.include)
Linker 标簽Add一項内容 $(#boost.lib)
四、測試
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void hello()
{
cout <<"Hello world, I'm athread!"<< endl;
}
int main()
{
boost::thread thread(&hello);
thread.join();
return 0;
}
附 編譯參數含義
我在用GCC編譯時用的指令是 bjam install –toolset=gcc –without-graph –without-graph_parallel –without-mpi –without-serialization –without-wave –prefix="D:\boost_1_47_0\VC9" link=static runtime-link=shared runtime-link=static threading=multi debug release
參數含義具體含義如下:
install/stage: install會生成包含頭檔案的include目錄,stage表示隻生成庫(dll和lib)無Include目錄(可以使用原本就有的booost_1_47目錄下的boost目錄)。我這裡面用install指令,生成的include隻有幾十M不會占用太大空間。
toolset編譯器選項可以是gcc、msvc、borland……
without表示不安裝某些庫(因為我在學習Python是以我這裡安裝了python庫,安裝Python庫需要首先安裝Python解釋器,您如果不需要的話可以添加–with-python來取消安裝python庫)
prefix/stagedir 前面使用install時用prefix,用stage時用stagedir,表示編譯生成檔案的路徑,我這裡是在D:\boost_1_47下的GCC目錄,您可以根據自己的偏好更好,同樣配置C::B和VS時的也要相應地更改。
build-dir 編譯過程中間檔案存放目錄,預設是bjam目錄下的bin.v2,等編譯完成後便可删掉該檔案(我用兩個編譯器編譯後生成的中間檔案有1G多)
threading 一般是多線程是以multi(如果您不寫多線程程式的話這裡可以不用設定)
link 生成動态連結庫/靜态連結庫。生成動态連結庫需使用shared方式,生成靜态連結庫需使用static方式。一般boost庫可能都是以static方式編譯,因為最終釋出程式帶着boost的dll感覺會比較累贅。
runtime-link 動态/靜态連結C/C++運作時庫。同樣有shared和static兩種方式,這樣runtime-link和link一共可以産生4種組合方式,各人可以根據自己的需要選擇編譯。一般link隻選static的話,隻需要編譯2種組合即可,即link=static runtime-link=shared和link=static runtime-link=static,本人一般就編這兩種組合。
debug/release 編譯debug/release版本。一般都是程式的debug版本對應庫的debug版本,是以兩個都編譯。