boost的編譯和使用,通過蒐集資料和總結,記錄成文。感謝文後所列參考資料的做者。html
地址:http://sourceforge.net/projects/boost/files/boost/1.56.0/python
能夠選擇 boost_1_56_0.7z 下載。ios
解壓後,使用VS2013編譯。首先打開「VS2013 開發人員命令提示」,cd 到boost解壓後的根目錄:E:\XXX\boost_1_56_0,執行bootstrap.bat。會在boost根目錄生成 b2.exe 、bjam.exe 、project-config.jam 、bootstrap.log四個文件。正則表達式
其中,b2.exe 、bjam.exe 這兩個exe做用是同樣的,bjam.exe 是老版本,b2是bjam的升級版本。bootstrap
1. bjam命令參數分析windows
咱們以文章中的命令來分析一下各個參數的做用:多線程
b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="D:\boost_1_56_0\" link=static runtime-link=static threading=multi debug release
函數
(1)stage/install:工具
stage表示只生成庫(dll和lib),install還會生成包含頭文件的include目錄。本人推薦使用stage,由於install生成的這個include目錄實際就是boost安裝包解壓縮後的boost目錄(只比include目錄多幾個非hpp文件,都很小),因此能夠直接使用,並且不一樣的IDE均可以使用同一套頭文件,這樣既節省編譯時間,也節省硬盤空間。測試
(2)toolset:
指定編譯器,可選的如borland、gcc、msvc(VC6)、msvc-9.0(VS2008)等。
vs2008 : msvc-9.0,vs2010 : msvc-10.0,
VS20十二、VS2013:msvc-12.0
(3)without/with:
選擇不編譯/編譯哪些庫。由於python、mpi等庫我都用不着,因此排除之。還有wave、graph、math、regex、test、program_options、serialization、signals這幾個庫編出的靜態lib都很是大,因此不須要的也能夠without掉。這能夠根據各人須要進行選擇,默認是所有編譯。可是須要注意,若是選擇編譯python的話,是須要python語言支持的,應該到python官方主頁http://www.python.org/下載安裝。
查看boost包含庫的命令是bjam --show-libraries。
(4)stagedir/prefix:
stage時使用stagedir,install時使用prefix,表示編譯生成文件的路徑。推薦給不一樣的IDE指定不一樣的目錄,如VS2008對應的是E:\SDK\boost\bin\vc9,VC6對應的是E:\SDK\boost\bin\vc6,不然都生成到一個目錄下面,難以管理。若是使用了install參數,那麼還將生成頭文件目錄,vc9對應的就是E:\SDK\boost\bin\vc9\include\boost-1_46\boost,vc6相似(光這路徑都這樣累贅,仍是使用stage好)。
(5)build-dir:
編譯生成的中間文件的路徑。這個本人這裏沒用到,默認就在根目錄(E:\SDK\boost)下,目錄名爲bin.v2,等編譯完成後可將這個目錄所有刪除(沒用了),因此不須要去設置。
(6)link:
生成動態連接庫/靜態連接庫。生成動態連接庫需使用shared方式,生成靜態連接庫需使用static方式。通常boost庫可能都是以static方式編譯,由於最終發佈程序帶着boost的dll感受會比較累贅。
(7)runtime-link:
動態/靜態連接C/C++運行時庫。一樣有shared和static兩種方式,這樣runtime-link和link一共能夠產生4種組合方式,各人能夠根據本身的須要選擇編譯。
(8)threading:
單/多線程編譯。通常都寫多線程程序,固然要指定multi方式了;若是須要編寫單線程程序,那麼還須要編譯單線程庫,可使用single方式。
(9)debug/release:
編譯debug/release版本。通常都是程序的debug版本對應庫的debug版本,因此兩個都編譯。
2. 編譯boost
編譯boost的命令比較複雜,尤爲是 link, runtime-link 這兩個選項的功能分不太清楚,他們共有4種相互組合,這些相互組合各有什麼含義呢?
因此首先作個實驗,僅編譯date_time庫,觀察一下這兩個選項的做用。
分別使用下面的命令行編譯,
b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=static runtime-link=static threading=multi debug release b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=static runtime-link=shared threading=multi debug release b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=shared runtime-link=shared threading=multi debug release b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=shared runtime-link=static threading=multi debug release b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2" (爲避免將前面的結果覆蓋,配置另外一目錄vc12_2存放)
b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2" --build-type=complete(爲避免將前面的結果覆蓋,配置另外一目錄vc12_3存放)
所獲得的結果以下表所示:
序號 | link | runtime-link | 生成物 | 備註 |
1 | static | static | libboost_date_time-vc120-mt-sgd-1_56.lib libboost_date_time-vc120-mt-s-1_56.lib |
|
2 | static | shared | libboost_date_time-vc120-mt-gd-1_56.lib libboost_date_time-vc120-mt-1_56.lib |
與5結果相同 |
3 | shared | shared | boost_date_time-vc120-mt-gd-1_56.dll boost_date_time-vc120-mt-gd-1_56.lib boost_date_time-vc120-mt-1_56.dll boost_date_time-vc120-mt-1_56.lib |
|
4 | shared | static | 報錯,沒法編譯 | |
5 | 使用缺省 | 使用缺省 | libboost_date_time-vc120-mt-gd-1_56.lib libboost_date_time-vc120-mt-1_56.lib |
與2結果相同 而且在省略debug release時,debug release版本都編譯 |
6 | 使用--build-type=complete | boost_date_time-vc120-mt-gd-1_56.dll boost_date_time-vc120-mt-gd-1_56.lib boost_date_time-vc120-mt-1_56.dll boost_date_time-vc120-mt-1_56.lib
libboost_date_time-vc120-mt-sgd-1_56.lib libboost_date_time-vc120-mt-s-1_56.lib
libboost_date_time-vc120-mt-gd-1_56.lib libboost_date_time-vc120-mt-1_56.lib
libboost_date_time-vc120-s-1_56.lib libboost_date_time-vc120-sgd-1_56.lib |
--build-type=complete時,能夠看到link,runtime-link的 3種組合下debug, release的多線程版本都生成出來了, 除此以外,還生成了link=static,runtime-link=static的debug, release的單線程版本 |
從上面的結果能夠看到,link和runtime-link的缺省配置是 link=static runtime-link=shared,因此咱們可使用 (b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2")命令行來編譯boost。
另外,咱們還能夠分析一下 boost 庫的命名特色:【2】
(1)以「lib」開頭的是「link=static」版本(靜態連接庫版本,沒有dll),而直接以「boost」開頭的是「link=shared」版本(動態連接庫版本,包含lib和dll)。
(2)全部的庫都含有"boost"前綴。
(3)緊隨其後的是boost庫名稱(好比date_time庫)。
(4)而後是編譯器的版本,與庫名稱之間以"-"而不是下劃線"_"分隔(好比 -vc120)。
(5)有「mt」的爲「threading=multi」版本,沒有的則是「threading=single」版本。
(6)有「s」的爲「runtime-link=static」版本,沒有的則是「runtime-link=shared」版本。
(7)有「gd」的爲debug版本,沒有的則是release版本。
(8)全部的庫都含有boost庫的版本號結尾(好比1_56,其中的"."如下劃線"_"代替)
3. link, runtime-link 組合分析
文章【2】給出了link,runtime-link的具體做用分析。
假設一個庫A依賴於庫B,咱們本身的程序client依賴於庫A,即:
那麼,link指的是client->A,runtime-link指的是A -> B
配置 |
連接過程 |
運行時須要的文件 |
link=static runtime-link=static |
client經過A.a (A.lib)靜態包含A; A經過B.a (B.lib)靜態包含B; 不關 .so .dll的事 |
client |
link=static runtime-link=shared |
client經過A.a (A.lib)靜態包含A; 在運行時,client要動態調用B.so (B.dll) |
client B.so (B.dll) |
link=shared runtime-link=shared |
client會包含A.a (A.lib); A會包含 B.a (B.lib); 但都只保存動態庫的真正實現的stub,運行時經過stub去動態加載 A.so (A.dll), B.so (B.dll) 中的實現 |
client A.so (A.dll) B.so (B.dll) |
link=shared runtime-link=static |
client會包含A.a (A.lib),但只包含真正實現的stub; A經過B.a (B.lib)靜態包含B; 運行時,client會動態調用A.so (A.dll) |
client A.so (A.dll)
|
包含頭文件的Include路徑:E:\eCode\boost_1_56_0
包含庫文件的連接路徑:E:\eCode\boost_1_56_0\bin\vc12\lib
(1)能夠設置爲僅用於當前project:
選中當前project->Properties->Configuration Properties->C/C++->General: Additional Include Directories: 設置 E:\eCode\boost_1_56_0
選中當前project->Properties->Configuration Properties->Linker->General: Additional LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib
(2)可設置爲僅用於當前Solution:
選中當前project->Properties->Configuration Properties->VC++ Directories:
Include Directories: 設置 E:\eCode\boost_1_56_0
LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib
(3)可設置爲OS當前用戶下的VC++環境(當前用戶下VC++所建立的全部Solution)
在某個已打開的工程下,切換到Property Manager 選項卡,而後而後展開當前工程的properties配置,打開Microsoft.Cpp.Win32.User
選擇Common Properties->VC++ Directories:
Include Directories: 設置 E:\eCode\boost_1_56_0
LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib
這樣設置的僅在Win32編譯選項下起做用,x64編譯選項須要另外配置x64的properties sheet。
(4)可設置爲OS全部用戶下的VC++環境
能夠編輯 Microsoft.Cpp.Default.props 、Microsoft.Cpp.props 。這裏就不介紹了。
使用文章【3】中date_time計時函數。建立一個Win32 console 工程,而後copy下面代碼
//#define BOOST_DATE_TIME_SOURCE #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace std; using namespace boost::gregorian; using namespace boost::posix_time; /************************************************************************ 建立微秒級的計時器 ************************************************************************/ template <class T = microsec_clock> class MyTimer { private: ptime m_startTime; public: MyTimer() { Restart(); } void Restart() { m_startTime = T::local_time(); } void Elapsed() { cout << T::local_time() - m_startTime << endl; } }; int main() { MyTimer<microsec_clock> t; for(int i = 0; i < 100; ++i) { cout << "hello" << endl; } t.Elapsed(); }
注意開頭的宏 「#define BOOST_DATE_TIME_SOURCE」 注掉了。若啓用這個宏定義,則默認由編譯器從新編譯嵌入的頭文件;若不啓用這個宏定義,則表示使用系統已編譯好的date_time庫。
(1)禁用#define BOOST_DATE_TIME_SOURCE 宏,而後將 libboost_date_time-vc120-mt-gd-1_56.lib 從 E:\eCode\boost_1_56_0\bin\vc12\lib 中移除,編譯debug版的程序時,提示鏈接錯誤,缺乏libboost_date_time-vc120-mt-gd-1_56.lib。
(2)啓用#define BOOST_DATE_TIME_SOURCE 宏,編譯debug版的程序時,可發現即便在缺乏 libboost_date_time-vc120-mt-gd-1_56.lib的狀況下,也能成功編譯。
【1】Boost下載安裝編譯配置使用指南(含Windows、Linux以及ARM Linux)(http://www.cnblogs.com/wondering/archive/2009/05/21/boost_setup.html)
【2】link 和 runtime-link,搭配shared 和 static(http://blog.csdn.net/yasi_xi/article/details/8660549)
【3】計時函數(二)(http://www.cnblogs.com/jerry19880126/archive/2013/02/20/2919718.html)
【4】官方文檔Getting Started on Windows(http://www.boost.org/doc/libs/1_56_0/more/getting_started/windows.html)
【5】bjam使用(http://blog.chinaunix.net/uid-22301538-id-3158997.html)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Boost庫是一個可移植、提供源代碼的C++庫,做爲標準庫的後備,是C++標準化進程的開發引擎之一。 Boost庫由C++標準委員會庫工做組成員發起,其中有些內容有望成爲下一代C++標準庫內容。在C++社區中影響甚大,是徹徹底底的「準」標準庫。Boost因爲其對跨平臺的強調,對標準C++的強調,與編寫平臺無關。大部分boost庫功能的使用只需包括相應頭文件便可,少數(如正則表達式庫,文件系統庫等)須要連接庫。但Boost中也有不少是實驗性質的東西,在實際的開發中實用須要謹慎。
#編譯bjam:
首先從SourceForge上下載Boost庫的壓縮包,此時的最新版爲1.57.0,下載地址爲http://nchc.dl.sourceforge.net/project/boost/boost/1.57.0/boost_1_57_0.7z;下載後,解壓縮7z包。而後以管理員方式在命令提示符下運行bootstrap.bat批處理文件,運行後會在當前目錄中出現bjam.exe文件。
#編譯boost庫
32位編譯: #從開始菜單啓動Visual Studio 2013的vs2013 命令行,進入boost所在目錄,運行bootstrap.bat 編譯命令:
bjam.exe stage --toolset=msvc-12.0 --without-graph --without-graph_parallel --without-math --without-mpi --without-serialization --without-wave --without-test --without-program_options --without-serialization --without-signals --stagedir="vc12_x86" link=static runtime-link=shared threading=multi debug release
#toolset:指定編譯器,可選的如borland、gcc、msvc(VC6)、msvc-10.0(VS20010) #vs2008 : msvc-9.0,vs2010 : msvc-10.0, VS20十二、VS2013是msvc-12.0
#stagedir:表示編譯生成文件的路徑。
#build-dir:編譯生成的中間文件的路徑。這個本人這裏沒用到,默認就在根目錄(D:\boost\boost_1_57_0)下,目錄名爲bin.v2(刪掉),等編譯完成後可將這個目錄所有刪除(沒用了),因此不須要去設置。
#without/with:選擇不編譯/編譯哪些庫。
#address-model:要有address-model=64屬性,若是沒有這個屬性的話,會默認生成32位的平臺庫,加入這個選項才能生成64位的DLL。
#threading:單/多線程編譯。通常都寫多線程程序,固然要指定multi方式了;若是須要編寫單線程程序,那麼還須要編譯單線程庫,可使用single方式。
#靜態庫版link=shared,動態庫link=shared #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版本,因此兩個都編譯。
#64位編譯: 從開始菜單啓動Visual Studio 2013的vs2013 x64兼容工具命令行,而後轉到boost根文件夾,運行bootstrap.bat生成x64版的bjam.exe。 在編譯命令中加入address-model=64屬性
#還有人總結windows下boost庫的命名特色: link=static runtime-link=static 獲得 libboostxxxxx.lib link=shared runtime-link=shared 獲得 boostxxxx.lib 和 boostxxxx.dll 由以上的文件夾層次結構基本就能夠得出結論:
1、以「lib」開頭的是「link-static」版本的,而直接以「boost」開頭的是「link-shared」版本的。
2、有「d」的爲debug版本,沒有的則是release版本。
3、有「s」的爲「runtime-link-static」版本,沒有的則是「runtime-link-shared」版本。
4、有「mt」的爲「threading-multi」版本,沒有的則是「threading-single」版本。
#設定vs2013環境。
(在項目-->右鍵屬性-->C/C++)
附加包含目錄:如:F:/boost_1.57_0
連接器:附加庫目錄:(編譯生成文件的路徑)如:F:/boost_1.57_0/stage/bin
附加依賴項:(項目所需編譯庫)
若是編譯成Debug則包含:libboost_regex-vc120-mt-gd-1_57.lib(舉例)
若是編譯成Release則包含:libboost_regex-vc120-mt-1_57.lib
或者添加#pragma comment(lib, "libboost_regex-vc120-mt-gd-1_57.lib")附加連接庫
#define BOOST_LIB_DIAGNOSTIC
它可讓VC在編譯時的output窗口中輸出程序具體連接了哪些boost庫以及連接順序。
測試一下:
#include "stdafx.h" #include <boost/regex.hpp> #include <boost/asio.hpp> #include <iostream> #pragma comment(lib, "libboost_date_time-vc120-mt-gd-1_57.lib") #pragma comment(lib, "libboost_system-vc120-mt-gd-1_57.lib") int _tmain(int argc, _TCHAR* argv[]) { boost::regex reg("[0-9]+");//lib庫在項目附加依賴項中添加了 std::cout << boost::regex_match("123", reg) << std::endl; boost::asio::io_service io; system("PAUSE"); return 0; }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上面都是網友的,由於我要使用的BOOST:ASIO靜態庫,故輸入的爲
一、生成靜態庫
bjam stage --with-system --with-thread --with-date_time --with-regex --with-serialization --stagedir="D:\Boost\boost_ssc" link=static runtime-link=static
threading=multi debug release
bjam stage --with-system --with-thread --with-date_time --with-regex --with-serialization --stagedir="D:\Boost\boost_ssd" link=static runtime-link=static
threading=multi debug release
二、而後在Realese配置對話框中,連接器選項->常規->附加庫目錄選擇到D:\Boost\boost_ssc目錄;C++/C選選項中也得選中,看上圖;
這樣編譯出來的.exe程序就能正常執行了;