Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。
Boost库由C++标准委员会库工作组成员发起,在C++社区中影响甚大,其成员已近2000人。
Boost库为我们带来了最新、最酷、最实用的技术,是不折不扣的“准”标准库。
(以上文字摘自【开源中国】)
本文记录(介绍)Boost库的编译过程。
操作系统为64位版本Windows8.1 ,编译环境为Visual Studio 2013 (msvc12.0)
一、准备
下载boost代码(此文首次编辑时,Boost的最新版本为1.57.0)
需要下载请访问官网http://www.boost.org/
或者SourceForge页面https://sourceforge.net/projects/boost/
我下载的是boost_1_57_0.zip
下载完之后解压(可以按照个人喜好设置解压路径)
请保证目标位置所在磁盘有足够空间,因为编译后产生了大量的中间文件,最后将占用近2G的空间
二、开始编译
1、生成bjam.exe程序
切换到解压目录,找到bootstrap.bat,(双击)运行,不出意外就会生成bjam.exe(和其他文件)
保险起见,我在bootstrap.bat的内容末尾添加了一行暂停语句
也可以在控制台(命令行)窗口(Win+R调出cmd窗口)中执行,把bat文件拖拽到cmd窗口就可以了
2、设置编译参数
我个人偏好自己写一个bat文件(而不是在cmd窗口输入命令),内容如下
(原本所有命令都在一行,为了方便阅读才拆分为多行显示)
@echo build boost library
bjam
--toolset=msvc-12.0
--prefix=D:\Develop\Boost
--build-type=complete
link=shared
variant=release
threading=multi
runtime-link=shared
install
@pause
其中各词语的含义如下
bjam 是主程序(bjam.exe)
--toolset=msvc-12.0 表示编译工具集是MSVC12.0 (即VS2013)
--prefix=D:\Develop\Boost 表示编译后的安装目录
--build-type=complete 表示尝试完整编译(可以选择只编译其中的一些组件或者忽略某些组件)
link=shared 表示动态链接(使用动态链接库)
variant=release 表示以Release模式编译
thread=multi 表示编译为多线程
runtime-link=shared 表示生成动态链接库(dll)
install 表示安装(类似于CMake的install,就是将对应include、lib、bin等文件拷贝到安装目录)
更多的参数及其设置可以参考相关资料(网络上的相关资料很丰富,找到适合的参考一下就行了)
注释:
cmd打开:bjam install --prefix="E:\boost_1_69_0\vs2015" --toolset=msvc-14.0 address-model=64 --with-system --with-date_time --with-random --with=-regex link=static runtime-link=static threading=multi
参数可以根据下面参考 编译出来的是2015上64位静态库
编译结果
【步骤2 参数解释】
BOOST_LIB_PREFIX + BOOST_LIB_NAME + "-" + BOOST_LIB_TOOLSET + "-" + BOOST_LIB_THREAD_OPT + "-" + BOOST_LIB_RT_OPT + "-" + BOOST_LIB_VERSION
这些定义为:
BOOST_LIB_PREFIX: 静态库为 "lib" (否则无,是用动态链接库)
BOOST_LIB_NAME: 库的基本名称 ( 比方说 boost_regex).
BOOST_LIB_TOOLSET: 编译工具集名称 ( 比如:vc6, vc7, bcb5 )
BOOST_LIB_THREAD_OPT: 多线程为 "-mt" ,否则为空
BOOST_LIB_RT_OPT: 指示使用的运行库的后缀,
组合下面的一个或者更多字符:
s 静态运行库,指的是静态链接到运行时库(不出现表示动态).
g 调试/诊断 runtime (release if not present).
d 调试版本 (不出现表示 release 版 ).
p STLPort 版本.
注:对 vc 来说,gd 总是一起出现
BOOST_LIB_VERSION: Boost 版本, Boost 版本 x.y 表示为 x_y形式.
编译:为了简化boost库的编译,boost库中带了一个用来编译的工具,名字是bjam.exe或者b2.exe.
1:运行boost下的bootstap.bat脚本就会自动生上述的两个编译工具,并且拷贝到boost目录下. 也可以进入tools/build目录下找到类似的脚本或者项目源码来编译.
2: bjam.exe的参数
Feature | Allowed values | Notes |
variant | debug,release |
|
link | shared,static | Determines if Boost.Build creates shared or static libraries |
threading | single,multi | Cause the produced binaries to be thread-safe. This requires proper support in the source code itself. |
address-model | 32,64 | Explicitly request either 32-bit or 64-bit code generation. This typically requires that your compiler is appropriately configured. Please refer to the section called “C++ Compilers” and your compiler documentation in case of problems. |
toolset | (Depends on configuration) | The C++ compiler to use. See the section called “C++ Compilers” for a detailed list. (Vs2008)msvc-8.0 (vs2010)msvc-10.0 |
include | (Arbitrary string) | Additional include paths for C and C++ compilers. |
define | (Arbitrary string) | Additional macro definitions for C and C++ compilers. The string should be either SYMBOL or SYMBOL=VALUE |
cxxflags | (Arbitrary string) | Custom options to pass to the C++ compiler. |
cflags | (Arbitrary string) | Custom options to pass to the C compiler. |
linkflags | (Arbitrary string) | Custom options to pass to the C++ linker. |
runtime-link | shared,static | Determines if shared or static version of C and C++ runtimes should be used. |
--build-dir=<builddir> | 编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了) |
--stagedir=<stagedir> | 存放编译后库文件的路径,默认是stage |
--build-type=complete | 编译所有版本,不然只会编译一小部分版本(确切地说是相当于:variant=release, threading=multi;link=shared|static;runtime-link=shared) |
variant=debug|release | 决定编译什么版本(对应文件中的d 调试版本 不出现表示 release 版) |
link=static|shared | 决定使用静态库还是动态库。(对应文件中的BOOST_LIB_PREFIX ) |
threading=single|multi | 决定使用单线程还是多线程库。(对应文件中的BOOST_LIB_THREAD_OPT) |
runtime-link=static|shared | 决定是静态还是动态链接C/C++标准库。(对应文件中的BOOST_LIB_THREAD_OPT) |
--with-<library> | 只编译指定的库,如输入--with-regex就只编译regex库了。 |
--show-libraries | 显示需要编译的库名称 |
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=static link=static stage
意思是要生静态库,该静态库静态链接C运行时库
生成的文件名字是:libboost_date_time-vc100-mt-sgd-1_48.lib(debug version),libboost_date_time-vc100-mt-s-1_48.lib(release version) 两个文件.
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=shared link=static stage
意思是要生静态库,该静态库动态链接C运行时库
生成的文件名字是:libboost_date_time-vc100-mt-gd-1_48.lib(debug verion),libboost_date_time-vc100-mt-1_48.lib(release version) 两个文件.
bjam.exe --toolset=msvc-10.0 --with-date_time runtimelink=shared link=shared stage
意思是要生动态库,该动态库动态链接C运行时库
生成的文件名字是:boost_date_time-vc100-mt-gd-1_48.lib(debug version),boost_date_time-vc100-mt-1_48.lib(release version) 两个文件.
生成的dll名字是:boost_date_time-vc100-mt-gd-1_48.dll(debug version),boost_date_time-vc100-mt-1_48.dll(release version)
3、编译
执行以上命令(在CMD窗口输入或者编写bat文件双击运行),我设置的是完全编译,耗时约10分钟
在安装目录下有include和lib两个文件夹
在include目录下有几个层次文件夹,我把底层文件夹下的所有文件剪切到include\boost目录下,感觉更“整洁”
注意,必须是\include\boost\双层结构,而不是\include\单层结构,否则无法正常使用
下图是我的boost文件夹下内容布局
然后将lib文件夹下的dll文件剪切出来粘贴到新建的bin目录,余下的lib文件保留
我的lib文件夹如下(左) bin文件夹如下(右)
三、配置编译环境参数
这一步和其他库的配置方法基本上是一样的,无外乎设置include目录、lib目录和bin目录等,此处不再赘述。
四、使用示例
测试程序的源代码
#include <iostream>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
void test_array()
{
cout << "array test" << endl;
array<int, 10> ia = { 1, 2, 3, 4, 5 };
for (size_t i = 0; i<ia.size(); ++i)
{
cout << ia[i] << " ";
}
cout << endl;
}
int main(void)
{
test_array();
system("pause");
return 0;
}
运行结果