目录css
基于版本1.69.0html
获取Boost副本最好的方法是下载boost_1_69_0.7z或boost_1_69_0.zip并解压缩以安装完整的Boost发行版。python
这是Boost目录结构草图:ios
boost_1_69_0 \ ........................ “boost根目录” index.htm ................ 这是www.boost.org的副本 boost \ ......................... 全部Boost 头文件 lib \ .......................... 预编译库二进制文件 libs\ ................. 测试,.cpps,docs等,经过库 index.html ..................... 库文档从这里开始 algorithm\ any \ array \ ...更多库... status\ .........................Boost-wide测试套件 tools\ ........... 实用工具,例如Boost.Build,quickbook,bcp more \ ................................. 政策文件等 doc \ ........................ 全部Boost库文档的子集
重要的是要注意如下几点:c++
C:\Program Files\boost\boost_1_69_0
)有时在boost文档和邮件列表中也称为$BOOST_ROOT
。include
目录中包含boost\
子目录。 在Microsoft Visual Studio
中设置include
路径的具体步骤将在本文档的后面部分中介绍; 若是您使用其余IDE,请参阅产品文档以获取相关说明。.hpp
扩展名,而且位于boost根目录下的boost\
子目录中,所以你的Boost #include
指令将以下所示:#include <boost/whatever.hpp>
或shell
#include "boost/whatever.hpp"
取决于您对使用尖括号的偏好包括。即便是Windows用户也能够(而且出于可移植性的缘由,可能)在#include
指令中使用正斜杠(forward slashes); 你的编译器不关心。bootstrap
doc\
子目录分散注意力; 它只包含Boost文档的子集。若是要查看完整的文件,从 libs\index.html
开始。许多人想要知道的第一件事是,“我如何构建Boost?”好消息是,一般没有什么可构建的。windows
必须单独构建的Boost库是:浏览器
一些库具备可选的单独编译的二进制文件:多线程
to_string/from_string
或序列化功能时才须要,或者若是你的目标是Visual C ++ 6.x
或Borland
。解析GraphViz文件
时才须要它。random_device
时才须要它。_MSC_VER == 1310
和_MSC_VER == 1400
提供了exception_ptr的非侵入式实现,这须要单独编译的二进制文件。这是由#define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR
启用的。为了简单起见,咱们首先使用仅限标头的库。如下程序从标准输入读取整数序列,使用Boost.Lambda
将每一个数字乘以3,并将它们写入标准输出:
#include <boost/lambda/lambda.hpp> #include <iostream> #include <iterator> #include <algorithm> int main() { using namespace boost::lambda; typedef std::istream_iterator<int> in; std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " ); }
将该程序的文本复制到名为example.cpp
的文件中。
Win32控制台应用程序
。example_boost
example_boost
,而后从弹出的菜单中选择“属性”E:\dev\VS2017\boost_1_69_0
要测试您的应用程序,请按F5键并在结果窗口中键入如下内容,而后按Return键:
1 2 3
而后按住控制键并按“Z”,而后按Return键。
从计算机的“开始”菜单中,若是您是Visual Studio 2017用户,请选择
. Visual Studio 2017 > 适用于 VS 2017 的 x64 本机工具命令提示
为Visual Studio编译器调出一个特殊的命令提示符窗口。在该窗口中,将当前目录设置为适合建立一些临时文件的位置,而后键入如下命令,后跟Return键:
cl /EHsc /I E:\dev\VS2017\boost_1_69_0 D:\Users\yaoyu\source\repos\example_boost\example_boost\example_boost.cpp
要测试结果,请键入:
echo 1 2 3 | example_boost
若是您看到源自Boost头文件的编译器警告,请不要惊慌。咱们试图消除它们,但这样作并不老是实用的。错误是另外一回事。若是您在本教程中看到编译错误,请检查以确保您已正确复制示例程序而且已正确识别Boost根目录
。
若是要使用任何单独编译的Boost库,则须要获取库二进制文件。
若是您但愿使用Visual C++从源代码构建,可使用本节中描述的简单构建过程。打开命令提示符并将当前目录更改成Boost根目录(Boost root directory)
。而后,键入如下命令:
bootstrap .\b2
第一个命令准备Boost.Build
系统以供使用。第二个命令调用Boost.Build
来构建单独编译的Boost库。有关容许的选项列表,请参阅Boost.Build文档。
若是您使用的是早期版本的Visual C++或其余供应商的编译器,则须要使用Boost.Build来建立本身的二进制文件。
Boost.Build是一个基于文本的系统,用于开发,测试和安装软件。首先,您须要构建并安装它。步骤以下:
tools\build\
。bootstrap.bat
b2 install --prefix=PREFIX
, 其中PREFIX是您但愿安装Boost.Build的目录PREFIX\bin
添加到PATH
环境变量中。首先,在下表中找到与编译器对应的工具集(Boost.Build文档中始终提供最新列表)。
Toolset Name | Vendor | Notes |
---|---|---|
acc | Hewlett Packard | Only very recent versions are known to work well with Boost |
borland | Borland | |
como | Comeau Computing | Using this toolset may require configuring another toolset to act as its backend. |
darwin | Apple Computer | Apple's version of the GCC toolchain with support for Darwin and MacOS X features such as frameworks. |
gcc | The Gnu Project | Includes support for Cygwin and MinGW compilers. |
hp_cxx | Hewlett Packard | Targeted at the Tru64 operating system. |
intel | Intel | |
msvc | Microsoft | |
sun | Oracle | Only very recent versions are known to work well with Boost. Note that the Oracle/Sun compiler has a large number of options which effect binary compatibility: it is vital that the libraries are built with the same options that your appliction will use. In particular be aware that the default standard library may not work well with Boost, unless you are building for C++11. The particular compiler options you need can be injected with the b2 command line options cxxflags=``and ``linkflags=. For example to build with the Apache standard library in C++03 mode use b2 cxxflags=-library=stdcxx4 linkflags=-library=stdcxx4. |
vacpp | IBM | The VisualAge C++ compiler. |
若是安装了特定编译器的多个版本,则能够将版本号附加到工具集名称,后面加一个连字符,例如intel-9.0或borland-5.4.3。在Windows上,即便您只安装了一个版本(除非您使用的是具备特殊版本检测代码的msvc或gcc工具集),不然auto-linking
会失败。
Boost.Build会将构建时生成的全部中间文件放入构建目录(build directory)中。若是您的Boost根目录是可写的,则此步骤不是必需的:默认状况下,Boost.Build将在您当前的工做目录中为此建立bin.v2/
子目录。
将当前目录更改成Boost根目录
, 并按以下方式调用b2:
b2 --build-dir=build-directory toolset=toolset-name --build-type=complete stage
有关这些和其余调用选项的完整说明,请参阅Boost.Build文档。
例如,您的会话可能以下所示:
C:\WINDOWS> cd /d C:\Program Files\boost\boost_1_69_0 C:\Program Files\boost\boost_1_69_0> b2 ^ More? --build-dir="C:\Documents and Settings\dave\build-boost" ^ More? --build-type=complete msvc stage
请务必阅读关于^, More?
和该行中的引号(“)的说明。
选项 --build-type=complete 会让Boost.Build构建全部支持的库版本。有关如何仅构建特定版本的说明,请在Boost.Build邮件列表中查询。
构建特殊阶段(stage)
目标将Boost库二进制文件放在Boost树的stage\lib\
子目录中。要使用其余目录,请将--stagedir=directory
选项传递给b2。
有关在调用b2时能够传递的其余选项的说明 ,请键入:
b2 --help
特别是,为了限制建设所花费的时间,您可能会对如下内容感兴趣:
--show-libraries
查看库名列表--with-library-name
或--without-library-name
选项选择要构建(或不构建)的库release
或debug
来选择特定的构建版本。在构建Boost库的过程当中,您能够指望在控制台上看到一些消息。这些可能包括
toolset-name.c++ long/path/to/file/being/built
在构建Boost时看到的惟一错误消息(若是有的话)应该与IOStreams
库对zip和bzip2格式的支持有关。若是须要这些特性,请安装libz和libbz2的相关开发包。构建Boost库时的其余错误也值得关注。
若是它看起来像编译系统没法找到你的编译器和/或链接器,考虑设置一个user-config.jam
文件。若是这不是您的问题或user-config.jam
文件不工做,请解决有关将编译器的Boost配置到Boost.Build邮件列表的问题。
为了演示与Boost二进制库的连接,咱们将使用如下简单程序从电子邮件中提取主题行。它使用Boost.Regex
库,它具备单独编译的二进制组件。
#include <boost/regex.hpp> #include <iostream> #include <string> int main() { std::string line; boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" ); while (std::cin) { std::getline(std::cin, line); boost::smatch matches; if (boost::regex_match(line, matches, pat)) std::cout << matches[2] << std::endl; } }
连接有两个主要挑战:
从咱们以前建立的仅头文件example
项目开始:
example
,而后从弹出的菜单中选择“属性”配置属性
> 连接器
> 附加库目录
中,输入Boost二进制文件的路径,例如E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1
。例如,假设您的Boost二进制文件位于E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1
中,咱们能够经过简单地将下面的粗体文本添加到咱们以前使用的命令行,从Visual C++命令行编译和连接上述程序。:
cl /EHsc /I E:\dev\VS2017\boost_1_69_0 example_boost.cpp ^ /link /LIBPATH:E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1
为了配置正确的二进制文件,您须要知道Boost二进制文件是如何命名的。每一个库文件名由一组共同的元素序列组成,这些元素描述了它的构建方式。例如,libboost_regex-vc71-mt-d-x86-1_34.lib
能够分解为如下元素:
lib
boost_regex
-vc71
-mt
-d
Key | Use this library when: | Boost.Build option |
---|---|---|
s | linking statically to the C++ standard library and compiler runtime support libraries. | runtime-link=static |
g | using debug versions of the standard and runtime support libraries. | runtime-debugging=on |
y | using a special debug build of Python. | python-debugging=on |
d | building a debug version of your code.6 | variant=debug |
p | using the STLPort standard library rather than the default one supplied with your compiler. | stdlib=stlport |
例如,若是构建代码的调试版本以用于静态运行时库和STLPort标准库的调试版本,则标记将为:-sgdp
。若是以上都不适用,则省略ABI标记。
-x86
Key | Architecture | Boost.Build option |
---|---|---|
x | x86-32, x86-64 | architecture=x86 |
a | ARM | architecture=arm |
i | IA-64 | architecture=ia64 |
s | Sparc | architecture=sparc |
m | MIPS/SGI | architecture=mips* |
p | RS/6000 & PowerPC | architecture=power |
字母后面的两位数字对地址模型进行编码,以下所示:
Key | Address model | Boost.Build option |
---|---|---|
32 | 32 bit | address-model=32 |
64 | 64 bit | address-model=64 |
-1_34
.lib
为了测试咱们的主题提取,咱们将过滤如下文本文件。将其从浏览器中复制并保存为jayne.txt
:
To: George Shmidlap From: Rita Marlowe Subject: Will Success Spoil Rock Hunter? --- See subject.
如今,在命令提示符窗口中,键入:
path\to\compiled\example < path\to\jayne.txt D:\Users\yaoyu\source\repos\example_boost\example_boost>example_boost.exe < jayne.txt
程序应输出电子邮件主题, “Will Success Spoil Rock Hunter?”
最后介绍了Boost并将其与您的程序集成。当你开始认真地使用Boost时,确定会有一些你但愿咱们覆盖的额外点。有一天,咱们可能会在“入门系列”中找到“第2册”来解决这些问题。在此以前,咱们建议您继续使用如下资源。若是您没法找到所需内容,或者咱们能够采起任何措施使此文档更加清晰,请将其发布到Boost用户的邮件列表中。