如何在Visual Studio的空项目中使用Boost库的详细步骤说明是什么?
如何在Visual Studio的空项目中使用Boost库的详细步骤说明是什么?
尽管Nate's answer已经很好了,但我会更加详细地解释Visual Studio 2010的相关内容,并包括编译需要外部库的各种可选组件的信息。
如果您使用的是仅有头文件的库,那么您只需要解压boost下载文件并设置环境变量即可。以下说明仅为Visual Studio设置环境变量,而不是整个系统。请注意,您只需要执行一次。
C:\boost_1_47_0
)。Microsoft.Cpp.<Platform>.user
,然后选择属性
以打开属性页进行编辑。VC++目录
。包含目录
部分以包括指向您的boost源文件的路径。如果您想要使用部分需要构建的boost,但没有使用任何需要外部依赖项的功能,则构建它非常简单。
C:\boost_1_47_0
)。bootstrap.bat
以构建b2.exe(之前称为bjam)。运行b2:
b2 --toolset=msvc-10.0 --build-type=complete stage
; b2 --toolset=msvc-10.0 --build-type=complete architecture=x86 address-model=64 stage
去散步/看一两部电影……
库目录
部分,包括指向boost库输出的路径。(默认示例和说明的路径是C:\boost_1_47_0\stage\lib
。如果希望同时使用x86和x64,请先重命名和移动目录(例如到<BOOST_PATH>\lib\x86
和<BOOST_PATH>\lib\x64
)。Boost.IOStreams Bzip2 过滤器:
C:\bzip2-1.0.6
)。-sBZIP2_SOURCE="C:\bzip2-1.0.6"
。Boost.IOStreams Zlib 过滤器:
C:\zlib-1.2.5
)。-sZLIB_SOURCE="C:\zlib-1.2.5"
。Boost.MPI
<BOOST_PATH>
中的文件 project-config.jam
(该目录是运行 bootstrap 后的结果)。添加一行代码 using mpi ;
(注意 ';' 前的空格)。Boost.Python
要完全构建库的32位版本,需要32位Python,同样,64位版本也是如此。如果您出于这种原因安装了多个版本,则需要告诉b2在哪里找到特定版本,并在何时使用哪个版本。一种方法是编辑目录<BOOST_PATH>
中由运行引导程序产生的文件project-config.jam
。添加以下两行,根据您的Python安装路径和版本进行调整(请注意分号前的空格)。
using python:2.6:C:\\Python\\Python26\\python;
using python:2.6:C:\\Python\\Python26-x64\\python :: <address-model> 64;
请注意,这种明确的Python规范目前会导致MPI构建失败。因此,如果您还要构建MPI,则需要进行一些具有和不具有规范的单独构建以构建所有内容。
按照上面的第二组说明构建boost。
Boost.Regex ICU支持
C:\icu4c-4_8
)。<ICU_PATH>\source\allinone
中打开Visual Studio解决方案。-sICU_PATH="C:\icu4c-4_8"
。boost error: msvc initialization: parameter 'version' inconsistent
,你可以只使用 --toolset=msvc
而不需要指定版本号。 - J3soon虽然Boost网站上的指南很有帮助,但这里提供一个经过简化的版本,同时还能建立x64库。
将Boost头文件安装在 C:\Boost\include\boost-(version)
下,并将32位库安装在 C:\Boost\lib\i386
中。请注意,默认库的位置是 C:\Boost\lib
,但如果您打算为多个架构构建,则应将它们放在 i386
目录下。
bootstrap
运行:b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\i386 install
toolset=msvc-11.0
toolset=msvc-10.0
toolset=msvc-14.1
将C:\Boost\include\boost-(version)
添加到您的包含路径中。
C:\Boost\lib\i386
添加到您的库路径中。这将在C:\Boost\include\boost-(version)
下安装Boost头文件,而在C:\Boost\lib\x64
下安装64位库。请注意,库的默认位置为C:\Boost\lib
,但如果您计划为多个架构构建,则需要将它们放在一个x64
目录下。
bootstrap
b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
toolset=msvc-11.0
toolset=msvc-10.0
C:\Boost\include\boost-(version)
添加到您的包含路径中。C:\Boost\lib\x64
添加到您的lib路径中。您还可以尝试将-j%NUMBER_OF_PROCESSORS%作为参数,它将使用您所有的核心。在我的四核处理器上,使事情变得超级快。
<?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>
该文件包含一个用户宏,用于指定Boost目录的位置(在本例中为D:\boost_1_53_0),以及两个其他参数:IncludePath和LibraryPath。语句#include <boost/thread.hpp>
将在相应目录(在本例中为D:\boost_1_53_0\boost\thread.hpp)中找到thread.hpp。'stage\lib\'目录可能会根据安装地址而改变。
这个boost.props文件可以位于D:\boost_1_53_0\
目录中。
你需要Boost的哪些部分?许多东西是TR1的一部分,这是与Visual Studio一起提供的,因此你可以简单地说:
#include <tr1/memory>
using std::tr1::shared_ptr;
根据James的说法,这个方法在C++0x中也应该有效:
#include <memory>
using std::shared_ptr;
std
命名空间,就像它们在C++0x标准中一样。(我认为它们可能也在std::tr1
命名空间中,以保持向后兼容性)。 - James McNellis这个帖子已经存在一段时间了,我想补充一些关于如何在特定硬件上尽可能快地构建Boost的内容。
如果您有4或6核,分别使用-j5或-j7。当然不是标准构建也不是-j2,除非您确实有双核。
我的主要工作站是Sandy Bridge Extreme,配备默认时钟的3930K(6核),而我的旧备份盒子是2600k(4核),趋势是我使用N + 1构建进程获得最佳的Boost编译时间,其中N是物理核心数。 N + 2达到收益递减点,时间会增加。
注:启用超线程技术,32GB RAM DDR3,三星840 EVO SSD。
-j7 on 6-core (2 minutes and 51 seconds) (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在6核心上(3分2秒)(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在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
配置
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)
我注意到64位构建需要稍微多一点时间,我需要对这些进行相同的比较并更新。
从以下网址下载boost: http://www.boost.org/users/download/ 例如,通过svn下载。
之后: cmd -> 进入boost目录(“D:\ boostTrunk”-您检出或下载和提取包的位置): 命令: bootstrap
我们在(“D:\ boostTrunk”)中创建了bjam.exe 之后: 命令: bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static (这将需要一些时间〜20分钟。)
之后: 打开Visual Studio 2010 -> 创建空项目 -> 转到项目属性 -> 设置:
粘贴此代码并检查是否正常工作?
#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;
}
这是一个关于IT技术的视频资源链接。请点击链接观看。还有一点需要注意:如果您想缩短编译时间,可以添加标志
-j2
同时运行两个并行构建。这可能会将编译时间缩短到只看一个电影的时间;)。
以下是我如何使用Boost的方法:
这样您就能够成功构建您的项目而不会出现任何错误!