實踐是最好的辦法。。學習C++,想試試線程,然後打算用boost庫,結果boost庫編譯差點吓到我。。沒看到比較完整的安裝教程。。一直耽擱。今天動手。完成了。方法記錄如下:
1.下載下傳boost
從boost官網( http://www.boost.org )上下載下傳最新的boost版本,現在最新是1.49版本,解壓到自定義目錄(我解壓到了D:/program files,最終的目錄結構是D:\Program Files\boost_1_49_0)
2.編譯安裝
在D:\Program Files\boost_1_49_0的目錄下,有一個bootstrap.bat檔案,直接輕按兩下運作。就會在同目錄生成b2.exe;bjam.exe兩個檔案。
3.設定編譯環境
修改user-config.jam (D:\Program Files\boost_1_49_0\tools\build\v2\user-config.jam) 的MSVC configuration
# MSVC configuration
# Configure msvc (default version, searched for in standard locations and PATH).
# using msvc ;
在上面這段的下面直接添加如下的文字。
1
using msvc:10.0::/wd4819/D_CRT_SECURE_NO_DEPRECATE/D_SCL_SECURE_NO_DEPRECATE/D_SECURE_SCL=0;
儲存關閉。
4.開始編譯
點選開始->所有程式->“Microsoft Visual Studio 2010”,指向“Visual Studio tools(工具)”,然後單擊“Visual Studio 2010 command prompt(指令提示)” 使用cd切換到D:\Program Files\boost_1_49_0目錄。這個就不說了
然後輸入如下的代碼:
2
3
b2 toolset=msvc-10.0architecture=x86 instruction-set=i686 address-model=32link=static
variant=debug,release threading=multi runtime-link=shared--without-python--without-mpi
--without-wave--without-graph--without-math--without-serialization stage
解釋一下指令的意思:
1.toolset:表示編譯器工具,我安裝的是VS2010,是以是msvc-10(如果你是VS2005,可以使用msvc-8.0 VS2008是msvc-9.0)
2.architecture:表示架構,也就是你的CPU架構,x86,x64,因為我安裝的是win7 32位,是以使用了x86的架構
3.instruction-set:表示指令集,依然是8086指令集
4.address-model:表示位址長度為32位
5.link:表示生成動态/靜态連結庫,動态連結庫是shared,靜态連結庫是static,一般都會編譯成靜态庫,因為給出程式的時候打包boost的庫會非常龐大
6.variant:表示生成的Debug或者release版本,一般情況下會兩種版本都會編譯出來的
7.threading:表示單/多線程編譯,一般我們的程式都會用到多線程,是以選擇了multi
8.runtime-link:表示動态/靜态連結C/C++運作時庫(C/C++ Runtime),我們選擇了動态連結
9.without/with:表示不需要編譯/需要編譯哪些庫,一些自己不用的庫可以無需編譯
10.stage/install:stage表示隻生成庫檔案(DLL和Lib),install還會生成包含頭檔案的include目錄,推薦使用stage,因為boost_1_49\boost中就是boost庫完整的頭檔案,是以無需再拷貝一份出來。編譯出來的庫會放在stage檔案夾中
這樣一份完整的boost庫就生成了,剩下就是直接使用到項目中了。
其實編譯的具體指令都是可以自己寫的。如果你需要編譯所有。隻需要使用下面的這行代碼
b2 –toolset=msvc-10.0 –build-type=complete
就可以了。
不出問題的話。就開始編譯了。。登個半個多小時吧。就會完成了。
5.設定vs
打開vs,建立一個工程。然後工程屬性。配置屬性->C/C++ ,附加包含目錄
填上
D:\Program Files\boost_1_49_0;%(AdditionalIncludeDirectories)
這個是最終的結果,你也可以手動添加
在左側選擇連結器->附加庫目錄,填上
D:\Program Files\boost_1_49_0\stage\lib;%(AdditionalLibraryDirectories)
6.測試
在你建立的工程裡輸入如下的代碼。運作成功就說明可以了
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <boost/thread/thread.hpp>
#include <iostream>
voidhello()
{
std::cout<<
"Hello world, I'm a thread!"
<<std::endl;
}
intmain(intargc,char*argv[])
boost::threadthrd(&hello);
thrd.join();
return0;
參考:
<a href="http://www.cppfans.org/1317.html" target="_blank">http://www.cppfans.org/1317.html</a>
<a href="http://www.cnblogs.com/ComputerG/archive/2011/03/10/1979730.html" target="_blank">http://www.cnblogs.com/ComputerG/archive/2011/03/10/1979730.html</a>
<a href="http://www.cppblog.com/shaker/archive/2011/11/30/33583.html" target="_blank">http://www.cppblog.com/shaker/archive/2011/11/30/33583.html</a>