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程序就能正常执行了;