C++ 如何在 Visual Studio 2010 中使用 Boost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2629421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:09:22  来源:igfitidea点击:

How to use Boost in Visual Studio 2010

c++visual-studio-2010boost

提问by OlimilOops

What is a good step by step explanation on how to use the Boost library in an empty project in Visual Studio?

关于如何在 Visual Studio 的空项目中使用 Boost 库的分步说明是什么?

回答by KTC

While Nate's answeris pretty good already, I'm going to expand on it more specifically for Visual Studio 2010 as requested, and include information on compiling in the various optional components which requires external libraries.

虽然Nate 的回答已经很好,但我将根据要求更具体地针对 Visual Studio 2010 对其进行扩展,并包括有关在需要外部库的各种可选组件中进行编译的信息。

If you are using headers only libraries, then all you need to do is to unarchive the boost download and set up the environment variables. The instruction below set the environment variables for Visual Studio only, and not across the system as a whole. Note you only have to do it once.

如果您只使用头文件库,那么您需要做的就是解压缩 boost 下载并设置环境变量。下面的说明仅为 Visual Studio 设置环境变量,而不是整个系统。请注意,您只需执行一次。

  1. Unarchive the latest version of boost (1.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_0).
  2. Create a new empty project in Visual Studio.
  3. Open the Property Manager and expand one of the configuration for the platform of your choice.
  4. Select & right click Microsoft.Cpp.<Platform>.user, and select Propertiesto open the Property Page for edit.
  5. Select VC++ Directorieson the left.
  6. Edit the Include Directoriessection to include the path to your boost source files.
  7. Repeat steps 3 - 6 for different platform of your choice if needed.
  1. 将最新版本的 boost(编写时为 1.47.0)解压缩到您选择的目录中(例如C:\boost_1_47_0)。
  2. 在 Visual Studio 中创建一个新的空项目。
  3. 打开 Property Manager 并展开您选择的平台的配置之一。
  4. 选择并右键单击Microsoft.Cpp.<Platform>.user,然后选择Properties打开属性页进行编辑。
  5. 选择VC++ Directories左侧。
  6. 编辑该Include Directories部分以包含 boost 源文件的路径。
  7. 如果需要,为您选择的不同平台重复步骤 3 - 6。

If you want to use the part of boost that require building, but none of the features that requires external dependencies, then building it is fairly simple.

如果你想使用 boost 需要构建的部分,但没有一个需要外部依赖的特性,那么构建它是相当简单的。

  1. Unarchive the latest version of boost (1.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_0).
  2. Start the Visual Studio Command Prompt for the platform of your choice and navigate to where boost is.
  3. Run: bootstrap.batto build b2.exe (previously named bjam).
  4. Run b2:

    • Win32:b2 --toolset=msvc-10.0 --build-type=complete stage;
    • x64:b2 --toolset=msvc-10.0 --build-type=complete architecture=x86 address-model=64 stage
  1. 将最新版本的 boost(编写时为 1.47.0)解压缩到您选择的目录中(例如C:\boost_1_47_0)。
  2. 为您选择的平台启动 Visual Studio 命令提示符并导航到 boost 所在的位置。
  3. 运行:bootstrap.bat构建 b2.exe(以前命名为 bjam)。
  4. 运行 b2:

    • Win32:b2 --toolset=msvc-10.0 --build-type=complete stage;
    • x64:b2 --toolset=msvc-10.0 --build-type=complete architecture=x86 address-model=64 stage

Go for a walk / watch a movie or 2 / ....

去散步/看电影或 2 / ....

  1. Go through steps 2 - 6 from the set of instruction above to set the environment variables.
  2. Edit the Library Directoriessection to include the path to your boost libraries output. (The default for the example and instructions above would be C:\boost_1_47_0\stage\lib. Rename and move the directory first if you want to have x86 & x64 side by side (such as to <BOOST_PATH>\lib\x86& <BOOST_PATH>\lib\x64).
  3. Repeat steps 2 - 6 for different platform of your choice if needed.
  1. 从上面的指令集中执行步骤 2 - 6 以设置环境变量。
  2. 编辑该Library Directories部分以包含 boost 库输出的路径。(以上示例和说明的默认值是C:\boost_1_47_0\stage\lib。如果您希望 x86 和 x64 并排放置(例如 to <BOOST_PATH>\lib\x86& <BOOST_PATH>\lib\x64),请先重命名并移动目录。
  3. 如果需要,为您选择的不同平台重复步骤 2 - 6。

If you want the optional components, then you have more work to do. These are:

如果您需要可选组件,那么您还有更多工作要做。这些是:

  • Boost.IOStreams Bzip2 filters
  • Boost.IOStreams Zlib filters
  • Boost.MPI
  • Boost.Python
  • Boost.Regex ICU support
  • Boost.IOStreams Bzip2 过滤器
  • Boost.IOStreams Zlib 过滤器
  • 增强型MPI
  • Boost.Python
  • Boost.Regex ICU 支持

Boost.IOStreams Bzip2 filters:

Boost.IOStreams Bzip2 过滤器:

  1. Unarchive the latest version of bzip2 library (1.0.6 as of writing) source files into a directory of your choice (e.g. C:\bzip2-1.0.6).
  2. Follow the second set of instructions above to build boost, but add in the option -sBZIP2_SOURCE="C:\bzip2-1.0.6"when running b2 in step 5.
  1. 将最新版本的 bzip2 库(编写时为 1.0.6)源文件解压缩到您选择的目录中(例如C:\bzip2-1.0.6)。
  2. 按照上面的第二组说明构建 boost,但-sBZIP2_SOURCE="C:\bzip2-1.0.6"在第 5 步中运行 b2 时添加选项。

Boost.IOStreams Zlib filters

Boost.IOStreams Zlib 过滤器

  1. Unarchive the latest version of zlib library (1.2.5 as of writing) source files into a directory of your choice (e.g. C:\zlib-1.2.5).
  2. Follow the second set of instructions above to build boost, but add in the option -sZLIB_SOURCE="C:\zlib-1.2.5"when running b2 in step 5.
  1. 将最新版本的 zlib 库(编写时为 1.2.5)源文件解压缩到您选择的目录中(例如C:\zlib-1.2.5)。
  2. 按照上面的第二组说明构建 boost,但-sZLIB_SOURCE="C:\zlib-1.2.5"在第 5 步中运行 b2 时添加选项。

Boost.MPI

增强型MPI

  1. Install a MPI distribution such as Microsoft Compute Cluster Pack.
  2. Follow steps 1 - 3 from the second set of instructions above to build boost.
  3. Edit the file project-config.jamin the directory <BOOST_PATH>that resulted from running bootstrap. Add in a line that read using mpi ;(note the space before the ';').
  4. Follow the rest of the steps from the second set of instructions above to build boost. If auto-detection of the MPI installation fail, then you'll need to look for and modify the appropriate build file to look for MPI in the right place.
  1. 安装 MPI 发行版,例如 Microsoft Compute Cluster Pack。
  2. 按照上述第二组说明中的步骤 1 - 3 来构建提升。
  3. 编辑运行 bootstrap 所产生project-config.jam的目录<BOOST_PATH>中的文件。添加一行内容using mpi ;(注意“;”前的空格)。
  4. 按照上述第二组说明中的其余步骤来构建提升。如果 MPI 安装的自动检测失败,那么您需要查找并修改适当的构建文件以在正确的位置查找 MPI。

Boost.Python

Boost.Python

  1. Install a Python distribution such as ActiveState's ActivePython. Make sure the Python installation is in your PATH.
  2. To completely built the 32-bits version of the library requires 32-bits Python, and similarly for the 64-bits version. If you have multiple versions installed for such reason, you'll need to tell b2 where to find specific version and when to use which one. One way to do that would be to edit the file project-config.jamin the directory <BOOST_PATH>that resulted from running bootstrap. Add in the following two lines adjusting as appropriate for your Python installation paths & versions (note the space before the ';').

    using python : 2.6 : C:\\Python\\Python26\\python ;

    using python : 2.6 : C:\\Python\\Python26-x64\\python : : : <address-model>64 ;

    Do note that such explicit Python specification currently cause MPI build to fail. So you'll need to do some separate building with and without specification to build everything if you're building MPI as well.

  3. Follow the second set of instructions above to build boost.

  1. 安装 Python 发行版,例如 ActiveState 的 ActivePython。确保 Python 安装在您的 PATH 中。
  2. 要完全构建 32 位版本的库需要 32 位 Python,对于 64 位版本也是如此。如果出于这种原因安装了多个版本,则需要告诉 b2 在哪里可以找到特定版本以及何时使用哪个版本。一种方法是编辑由运行引导程序产生project-config.jam的目录<BOOST_PATH>中的文件。添加以下两行,根据您的 Python 安装路径和版本进行适当调整(注意 ';' 之前的空格)。

    using python : 2.6 : C:\\Python\\Python26\\python ;

    using python : 2.6 : C:\\Python\\Python26-x64\\python : : : <address-model>64 ;

    请注意,这种明确的 Python 规范目前会导致 MPI 构建失败。因此,如果您还要构建 MPI,则需要使用和不使用规范进行一些单独的构建以构建所有内容。

  3. 按照上面的第二组说明来构建提升。

Boost.Regex ICU support

Boost.Regex ICU 支持

  1. Unarchive the latest version of ICU4C library (4.8 as of writing) source file into a directory of your choice (e.g. C:\icu4c-4_8).
  2. Open the Visual Studio Solution in <ICU_PATH>\source\allinone.
  3. Build All for both debug & release configuration for the platform of your choice. There can be a problembuilding recent releases of ICU4C with Visual Studio 2010 when the output for both debug & release build are in the same directory (which is the default behaviour). A possible workaround is to do a Build All (of debug build say) and then do a Rebuild all in the 2nd configuration (e.g. release build).
  4. If building for x64, you'll need to be running x64 OS as there's post build steps that involves running some of the 64-bits application that it's building.
  5. Optionally remove the source directory when you're done.
  6. Follow the second set of instructions above to build boost, but add in the option -sICU_PATH="C:\icu4c-4_8"when running b2 in step 5.
  1. 将最新版本的 ICU4C 库(编写时为 4.8)源文件解压缩到您选择的目录中(例如C:\icu4c-4_8)。
  2. <ICU_PATH>\source\allinone.
  3. 为您选择的平台的调试和发布配置构建全部。当调试和发布版本的输出位于同一目录中(这是默认行为)时,使用 Visual Studio 2010 构建最新版本的 ICU4C可能会出现问题。一种可能的解决方法是执行全部构建(例如调试构建),然后在第二配置(例如发布构建)中执行全部重建。
  4. 如果为 x64 构建,则需要运行 x64 操作系统,因为构建后步骤涉及运行它正在构建的一些 64 位应用程序。
  5. 完成后,可以选择删除源目录。
  6. 按照上面的第二组说明构建 boost,但-sICU_PATH="C:\icu4c-4_8"在第 5 步中运行 b2 时添加选项。

回答by Nate

While the instructionson the Boost web site are helpful, here is a condensed version that also builds x64 libraries.

虽然说明书上的升压网站是有帮助的,这里是一个浓缩版,也是建立64个库。

  • You only need to do this if you are using one of the libraries mentioned in section 3of the instructions page. (E.g., to use Boost.Filesystem requires compilation.) If you are not using any of those, just unzip and go.
  • 只有在使用说明页面第 3 节中提到的库之一时,才需要执行此操作。(例如,要使用 Boost.Filesystem 需要编译。)如果您不使用其中任何一个,只需解压缩即可。

Build the 32-bit libraries

构建 32 位库

This installs the Boost header files under C:\Boost\include\boost-(version), and the 32-bit libraries under C:\Boost\lib\i386. Note that the default location for the libraries is C:\Boost\libbut you'll want to put them under an i386directory if you plan to build for multiple architectures.

这将安装 Boost 头文件C:\Boost\include\boost-(version),并安装 32 位库C:\Boost\lib\i386。请注意,库的默认位置是C:\Boost\libi386如果您计划为多个体系结构构建,您需要将它们放在一个目录下。

  1. Unzip Boost into a new directory.
  2. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  3. Run: bootstrap
  4. Run: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\i386 install

    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
    • For Visual Studio 2017, use toolset=msvc-14.1
  5. Add C:\Boost\include\boost-(version)to your include path.

  6. Add C:\Boost\lib\i386to your libs path.
  1. 将 Boost 解压到一个新目录中。
  2. 启动 32 位 MSVC 命令提示符并切换到解压 Boost 的目录。
  3. 跑: bootstrap
  4. 跑: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\i386 install

    • 对于 Visual Studio 2012,请使用 toolset=msvc-11.0
    • 对于 Visual Studio 2010,请使用 toolset=msvc-10.0
    • 对于 Visual Studio 2017,请使用 toolset=msvc-14.1
  5. 添加C:\Boost\include\boost-(version)到您的包含路径。

  6. 添加C:\Boost\lib\i386到您的库路径。

Build the 64-bit libraries

构建 64 位库

This installs the Boost header files under C:\Boost\include\boost-(version), and the 64-bit libraries under C:\Boost\lib\x64. Note that the default location for the libraries is C:\Boost\libbut you'll want to put them under an x64directory if you plan to build for multiple architectures.

这将安装 Boost 头文件C:\Boost\include\boost-(version),并安装 64 位库C:\Boost\lib\x64。请注意,库的默认位置是C:\Boost\libx64如果您计划为多个体系结构构建,您需要将它们放在一个目录下。

  1. Unzip Boost into a new directory.
  2. Start a 64-bit MSVC command prompt and change to the directory where Boost was unzipped.
  3. Run: bootstrap
  4. Run: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
  5. Add C:\Boost\include\boost-(version)to your include path.
  6. Add C:\Boost\lib\x64to your libs path.
  1. 将 Boost 解压到一个新目录中。
  2. 启动 64 位 MSVC 命令提示符并切换到解压 Boost 的目录。
  3. 跑: bootstrap
  4. 跑: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
    • 对于 Visual Studio 2012,请使用 toolset=msvc-11.0
    • 对于 Visual Studio 2010,请使用 toolset=msvc-10.0
  5. 添加C:\Boost\include\boost-(version)到您的包含路径。
  6. 添加C:\Boost\lib\x64到您的库路径。

回答by Aaron Stainback

You can also try -j%NUMBER_OF_PROCESSORS% as an argument it will use all your cores. Makes things super fast on my quad core.

您也可以尝试 -j%NUMBER_OF_PROCESSORS% 作为参数,它将使用您的所有内核。在我的四核上运行速度超快。

回答by Uri

I could recommend the following trick: Create a special boost.propsfile

我可以推荐以下技巧:创建一个特殊boost.props文件

  1. Open the property manager
  2. Right click on your project node, and select 'Add new project property sheet'.
  3. Select a location and name your property sheet (e.g. c:\mystuff\boost.props)
  4. Modify the additional Include and Lib folders to the search path.
  1. 打开物业管理器
  2. 右键单击您的项目节点,然后选择“添加新项目属性表”。
  3. 选择一个位置并命名您的属性表(例如 c:\mystuff\boost.props)
  4. 将附加的 Include 和 Lib 文件夹修改为搜索路径。

This procedure has the value that boost is included only in projects where you want to explicitly include it. When you have a new project that uses boost, do:

此过程的价值在于 boost 仅包含在您想要明确包含它的项目中。当您有一个使用 boost 的新项目时,请执行以下操作:

  1. Open the property manager.
  2. Right click on the project node, and select 'Add existing property sheet'.
  3. Select the boost property sheet.
  1. 打开物业管理器。
  2. 右键单击项目节点,然后选择“添加现有属性表”。
  3. 选择 boost 属性表。

EDIT (following edit from @jim-fred):

编辑(在@jim-fred 编辑之后):

The resulting boost.propsfile looks something like this...

生成的boost.props文件看起来像这样......

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <BOOST_DIR>D:\boost_1_53_0\</BOOST_DIR>
  </PropertyGroup>
  <PropertyGroup>
    <IncludePath>$(BOOST_DIR);$(IncludePath)</IncludePath>
    <LibraryPath>$(BOOST_DIR)stage\lib\;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
</Project>

It contains a user macro for the location of the boost directory (in this case, D:\boost_1_53_0) and two other parameters: IncludePath and LibraryPath. A statement #include <boost/thread.hpp>would find thread.hpp in the appropriate directory (in this case, D:\boost_1_53_0\boost\thread.hpp). The 'stage\lib\' directory may change depending on the directory installed to.

它包含用于 boost 目录位置的用户宏(在本例中为 D:\boost_1_53_0)和另外两个参数:IncludePath 和 LibraryPath。语句#include <boost/thread.hpp>会在适当的目录(在本例中为 D:\boost_1_53_0\boost\thread.hpp)中找到 thread.hpp。'stage\lib\' 目录可能会根据安装到的目录而改变。

This boost.props file could be located in the D:\boost_1_53_0\directory.

这个 boost.props 文件可以位于D:\boost_1_53_0\目录中。

回答by fredoverflow

What parts of Boost do you need? A lot of stuff is part of TR1 which is shipped with Visual Studio, so you could simply say, for example:

您需要 Boost 的哪些部分?很多东西是随 Visual Studio 一起提供的 TR1 的一部分,所以你可以简单地说,例如:

#include <tr1/memory>

using std::tr1::shared_ptr;

According to James, this should also work (in C++0x):

根据 James 的说法,这也应该有效(在 C++0x 中):

#include <memory>

using std::shared_ptr;

回答by codenheim

This thread has been around a while, and I thought I'd add something about HOW to build Boost as fast as possible on your specific hardware.

这个线程已经有一段时间了,我想我会添加一些关于如何在您的特定硬件上尽可能快地构建 Boost 的内容。

If you have a 4 or 6-core use -j5 or -j7 respectively. Certainly not the standard build nor -j2 unless you indeed have dual core.

如果您有 4 核或 6 核,请分别使用 -j5 或 -j7。当然不是标准构建也不是 -j2 除非你确实有双核。

I'm running a Sandy Bridge Extreme with stock clocked 3930K (6-core) on my main station, but have a 2600k (4-core) on older backup box, and the trend is I get the best Boost compile times with N + 1 build processes where N is the number of physical cores. N+2 reaches a point of diminishing returns and the times go up.

我在主站上运行 Sandy Bridge Extreme,主站时钟频率为 3930K(6 核),但在较旧的备份盒上有 2600k(4 核),趋势是我使用 N+ 获得了最佳的 Boost 编译时间1 个构建过程,其中 N 是物理内核的数量。N+2 到达收益递减点,时间会增加。

Notes: Hyperthreading is enabled, 32GB RAM DDR3, Samsung 840 EVO SSD.

注:超线程已启用,32GB RAM DDR3,三星 840 EVO SSD。

-j7 on 6-core (2 minutes and 51 seconds) (Win7 Ultimate x64)(Visual Studio 2013)

-j7 在 6 核上(2 分 51 秒)(Win7 Ultimate x64)(Visual Studio 2013)

PS C:\Boost\boost_1_56_0> measure-command { .\b2 -j7 --build-type=complete msvc stage }

Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 51
Milliseconds      : 128
Ticks             : 1711281830
TotalDays         : 0.0019806502662037
TotalHours        : 0.0475356063888889
TotalMinutes      : 2.85213638333333
TotalSeconds      : 171.128183
TotalMilliseconds : 171128.183

-j6 on 6-core (3 minutes and 2 seconds) (Win7 Ultimate x64)(Visual Studio 2013)

-j6 6 核(3 分 2 秒)(Win7 Ultimate x64)(Visual Studio 2013)

PS C:\Boost\boost_1_56_0> measure-command { .\b2 -j6 --build-type=complete msvc stage }

Days              : 0
Hours             : 0
Minutes           : 3
Seconds           : 2
Milliseconds      : 809
Ticks             : 1828093904
TotalDays         : 0.00211584942592593
TotalHours        : 0.0507803862222222
TotalMinutes      : 3.04682317333333
TotalSeconds      : 182.8093904
TotalMilliseconds : 182809.3904

-j8 on 6-core (3 minutes and 17 seconds) (Win7 Ultimate x64)(Visual Studio 2013)

-j8 在 6 核上(3 分 17 秒)(Win7 Ultimate x64)(Visual Studio 2013)

PS C:\Boost\boost_1_56_0> measure-command { .\b2 -j8 --build-type=complete msvc stage }

Days              : 0
Hours             : 0
Minutes           : 3
Seconds           : 17
Milliseconds      : 652
Ticks             : 1976523915
TotalDays         : 0.00228764342013889
TotalHours        : 0.0549034420833333
TotalMinutes      : 3.294206525
TotalSeconds      : 197.6523915
TotalMilliseconds : 197652.3915

-j7 build on 6-core

-j7 建立在 6 核上

Config

配置

Building the Boost C++ Libraries.


Performing configuration checks

    - 32-bit                   : yes (cached)
    - arm                      : no  (cached)
    - mips1                    : no  (cached)
    - power                    : no  (cached)
    - sparc                    : no  (cached)
    - x86                      : yes (cached)
    - has_icu builds           : no  (cached)
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam
    - zlib                     : no  (cached)
    - iconv (libc)             : no  (cached)
    - iconv (separate)         : no  (cached)
    - icu                      : no  (cached)
    - icu (lib64)              : no  (cached)
    - message-compiler         : yes (cached)
    - compiler-supports-ssse3  : yes (cached)
    - compiler-supports-avx2   : yes (cached)
    - gcc visibility           : no  (cached)
    - long double support      : yes (cached)
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
    - zlib                     : no  (cached)

I note the 64-bit build takes a bit longer, I need to do the same comparison for those and update.

我注意到 64 位构建需要更长的时间,我需要对它们进行相同的比较并进行更新。

回答by andrew

Download boost from: http://www.boost.org/users/download/e.g. by svn

下载 boost 从:http: //www.boost.org/users/download/例如通过 svn

  • Windows -> tortoise (the simplest way)
  • Windows -> 乌龟(最简单的方法)

After that : cmd -> go to boost directory ("D:\boostTrunk" - where You checkout or download and extract package): command : bootstrap

之后: cmd -> 转到 boost 目录(“D:\boostTrunk” - 您签出或下载并解压缩包的位置):命令: bootstrap

we created bjam.exe in ("D:\boostTrunk") After that : command : bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static(It will take some time ~20min.)

我们在 ("D:\boostTrunk") 中创建了 bjam.exe 之后:命令: bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static(这需要一些时间~20分钟。)

After that: Open Visual studio 2010 -> create empty project -> go to project properties -> set:

之后:打开 Visual Studio 2010 -> 创建空项目 -> 转到项目属性 -> 设置:

Project properties VS 2010

项目属性 VS 2010

Paste this code and check if it is working?

粘贴此代码并检查它是否有效?

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/regex.hpp>

using namespace std;

struct Hello 
{
    Hello(){ 
        cout << "Hello constructor" << endl;
    }

    ~Hello(){
        cout << "Hello destructor" << endl;
        cin.get();
    }
};


int main(int argc, char**argv)
{
    //Boost regex, compiled library
    boost::regex regex("^(Hello|Bye) Boost$");
    boost::cmatch helloMatches;
    boost::regex_search("Hello Boost", helloMatches, regex);
    cout << "The word between () is: " << helloMatches[1] << endl;

    //Boost shared pointer, header only library
    boost::shared_ptr<Hello> sharedHello(new Hello);

    return 0;
}

Resources : https://www.youtube.com/watch?v=5AmwIwedTCM

资源:https: //www.youtube.com/watch?v=5AmwIwedTCM

回答by GaMer13

Also a little note: If you want to reduce the compilation-time, you can add the flag

还有一点注意:如果你想减少编译时间,你可以添加标志

-j2

-j2

to run two parallel builds at the same time. This might reduce it to viewing one movie ;)

同时运行两个并行构建。这可能会减少观看一部电影;)

回答by bhavesh

Here is how I was able to use Boost:

以下是我如何使用 Boost:

  1. Download and extract the zip version of Boost libraries.
  2. Run bootstrap.bat file and then run bjam.exe.
  3. Wait for roughly 30 minutes or so.
  4. Create a new project in Visual Studio.
  5. Go to project-->properties-->Linker-->General-->Additional Library Directoriesand add boost/stage/libdirectory to it.
  6. Go to project-->properties-->C/C++-->General-->Additional Include Directoriesand add boostdirectory to it.
  1. 下载并解压 Boost 库的 zip 版本。
  2. 运行 bootstrap.bat 文件,然后运行 ​​bjam.exe。
  3. 等待大约 30 分钟左右。
  4. 在 Visual Studio 中创建一个新项目。
  5. 转到项目--> 属性--> 链接器--> 常规--> 附加库目录并将boost/stage/lib目录添加到其中。
  6. 转到项目--> 属性--> C/C++--> 常规--> 附加包含目录并向其添加boost目录。

You will be able to build your project without any errors !

您将能够在没有任何错误的情况下构建您的项目!

回答by Zoob

The Windows installers located hereworked perfectly for me. I took the following steps:

位于此处的 Windows 安装程序非常适合我。我采取了以下步骤:

  1. Follow the installation wizard until finished.
  2. Run visual studio.
  3. Create a new C++ project
  4. Open project properties (can be found by right-clicking the project name in the solution explorer)
  5. Under "C/C++ > General > Additional Include Directories" add the path where boost root directory. Default for my version was C:\local\boost_1_63_0. The number after "boost" is the version of boost.
  6. In project properties, under "Linker > Additional Library Directories" add the directory for library files. Default for my version was C:\local\boost_1_63_0\lib64-msvc-14.0. The number after "lib" is related to the build target (32 bit or 64 bit in Visual Studio) and the number after "msvc" is related to the version of Visual Studio (14.0 is related to Visual Studio 2015, but I'm using it with the 2017 Visual Studio).
  1. 按照安装向导直到完成。
  2. 运行视觉工作室。
  3. 创建一个新的 C++ 项目
  4. 打开项目属性(可以通过在解决方案资源管理器中右键单击项目名称找到)
  5. 在“C/C++ > General > Additional Include Directories”下添加 boost 根目录所在的路径。我的版本默认为 C:\local\boost_1_63_0。“boost”后面的数字是boost的版本。
  6. 在项目属性中,在“链接器 > 附加库目录”下添加库文件的目录。我的版本默认为 C:\local\boost_1_63_0\lib64-msvc-14.0。“lib”后面的数字与构建目标(Visual Studio 中的 32 位或 64 位)有关,“msvc”后面的数字与 Visual Studio 的版本有关(14.0 与 Visual Studio 2015 相关,但我是将它与 2017 Visual Studio 一起使用)。

Good luck!

祝你好运!