您的位置:首页 > 其它

关于Boost库在VS2010下的编译使用

2013-08-07 15:38 627 查看
一、下载

首先从 boost官方主页http://www.boost.org下 载最新版boost安装包(目前最新版是1.43.0)。因为boost一部分类是需要编译成库才能使用的,所以我们还需要准备好boost专用的编译辅 助工具bjam。网上很多人都提倡直接使用boost安装包中附带的bjam源码来编译出bjam,但是之前需要修改若干配置脚本才能编译成功。个人认为 真没什么必要,费这劲毫无意义。boost官方网站在提供boost安装包下载链接的同时也提供与该版本安装包对应的bjam的下载,只有200多KB,
可以一同下载下来。

二、安装

将boost安装包解压至本地目录,如:E:\SDK\boost_1_43_0,然后将bjam.exe 拷贝到该目录下(bjam必须与boost-build.jam在同级目录)。

三、编译

接下来就是最重要的编译步骤了。需要打开命令提示 符(cmd.exe)窗口并执行bjam,可以使用--help参数来查看命令帮助。这里详细讲解一下 bjam的命令行参数,因为它非常重要。首先,它涉及到编程环境的搭建,你需要根据自己今后具体的使用环境来选择合适的命令行参数;其次,它影响到你的硬 盘空间,完全编译的话据说在3G以上,如果你同时拥有2个以上的IDE(如VC6和VC9共存)而且都要用到boost,那么占用多少硬盘就自己算吧…… 虽说如今大家的硬盘空间都不成问题,但就像本人一样崇尚合理利用资源不习惯铺张浪费提倡节俭的童子应该大有人在。综合以上两点因素,本人使用的bjam命
令如下:

bjam stage --toolset=msvc-10.0 --without-python --stagedir="E:\SDK\boost_1_43_0\vc10" link=shared runtime-link=shared threading=multi debug release

下面详细解释一下每个参数的含义,请务必仔细看完:

stage/install:stage 表示只生成库(dll和lib),install还会生成包含头文件的include目录。本人推荐使用 stage,因为install生成的这个include目录实际就是boost安装包解压缩后的boost目录(E:\SDK \boost_1_43_0\boost,只比include目录多几个非hpp文件,都很小),所以可以直接使用,而且不同的IDE都可以使用同一套头 文件,这样既节省编译时间,也节省硬盘空间。

toolset:指定编译器,可选的如borland、gcc、msvc(VC6)、msvc-10.0(VS2010)等。

without/with:选择不编译/编译哪些库。本人不需要编译python库,所以排除之,可以根据各人需要选择,默认是全部编译。 但是需要注意,如果选择编译python的话,是需要python语言支持的,应该到python官方主页http://www.python.org下 载安装。

stagedir/prefix:stage时使用stagedir,install时使用prefix,表示编译生成文件的路径。推荐给 不同的IDE指 定不同的目录,如VS2008对应的是E:\SDK\boost_1_43_0\vc10\lib,VC6对应的是E:\SDK \boost_1_43_0 \vc6\lib,否则都生成到一个目录下面,难以管理。如果使用了install参数,那么还将生成头文件目录,vc9对应的就是E:\SDK \boost_1_43_0\vc10\include\boost-1_43_0\boost,vc6类似(光这路径都这样累赘,还是使用stage
好)。

build-dir:编译生成的中间文件的路径。这个本人这里没用到,默认就在根目录(E:\SDK\boost_1_43_0)下,目录 名为bin.v2,等编译完成后可将这个目录全部删除(没用了),所以不需要去设置。

link:生成动态链接库/静态链接库。生成动态链接库需使用shared方式,生成静态链接库需使用static方式。这里需要注意的 是,static 方式下,最终生成的很多静态链接库大小都在几兆、几十兆,甚至接近百兆。这么大的库我们一般是不会采用静态链接方式的,所以这些库不推荐以static方 式编译(without掉);如果已经编译了赶快删,肯定没用,否则将占用近1G的硬盘空间。以下是巨型库黑名单:wave、graph、math、 regex、test、program_options、serialization、signals。

runtime-link:动态/静态链接C/C++运行时库。同样有shared和static两种方式,这样runtime-link 和link一共 可以产生4种组合方式。虽然它和link属性没有直接关系,但我们习惯上,一个工程如果用动态链接那么所有库都用动态链接,如果用静态链接那么所有库都用 静态链接。所以这样其实只需要编译2种组合即可,即link=shared runtime-link=shared和link=static runtime-link=static。

threading:单/多线程编译。一般都写多线程程序,当然要指定multi方式了;如果需要编写单线程程序,那么还需要编译单线程 库,可以使用single方式。

debug/release:编译debug/release版本。一般都是程序的debug版本对应库的debug版本,所以两个都编 译。

本人按以上方式分别编译了静态链接和动态链接两个版本后,整个E:\SDK\boost_1_43_0目录(包括安装包解压缩文件和编译生 成的库文件)只 有250MB。事实上编译完成后安装包解压缩文件除了boost目录之外其他目录和文件已经可以删除了,这样还可以腾出150MB的空间来。不过我又研究 了一下,其实libs这个目录也很有用,它提供了所有Boost类的使用范例,平时可以作为参考;另外doc目录是一个完整的boost使用帮助文档,当 然最好也不要删了。这样剩下的几个目录和文件加起来也就十多兆,索性都给它们留一条生路吧。

呵呵,一个完整而又完美的boost目录就此诞生了。

如果图省事,不想了解这么多,那么有简单的方法,可以使用命令:

bjam --toolset=msvc-10.0 --build-type=complete

直接指定编译器以完全模式编译即可,这样可以满足今后的一切使用场合,但同时带来的后果是:

1、占用3G以上的硬盘空间

2、 占用若干小时的编译时间

3、头文件和库文件存放于C:\Boost(个人非常反感)

4、生成的很多文件可以永远也用不上

四、配置

include目录:E:\SDK\boost_1_43_0

library 目录:E:\SDK\boost_1_43_0\vc10\lib

添加到IDE相应的路径下面即可。

windows下vs2008编译boost库

1.打开Visual Studio 2008 命令提示窗口

2.从命令提示窗口进入D:\boost_1_47_0\tools\build\v2\engine

3.执行 build.bat 会在D:\boost_1_47_0\tools\build\v2 生成bjam.exe 文件.

4.Copy bjam.exe 文件到 D:\boost_1_47_0 下.

5.修改 D:\boost_1_47_0\tools\build\v2\user-config.jam 找到下面的地文

# -------------------

# MSVC configuration.

# -------------------

# Configure msvc (default version, searched for in standard locations and PATH).

# using msvc ;

# Configure specific msvc version (searched for in standard locations and PATH).

# using msvc : 8.0 ;

#在这里添加 vs2008 的配置

using msvc : 9.0 : : /wd4819 /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 ;

#在这里添加 vs2005 的配置

using msvc : 8.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;

6.进入D:\boost_1_47_0 目录

7.执行bjam.exe 编译命令

//下面的命令的各选项的说明:

//prefix 将boost安装到的路径(生成的头文件和库文件都会放到该路径中)。

//重定义以下变量(利用-s设置):

//VC80_ROOT vc2005的安装路径,如果未将vc2005安装到默认位置,你必须指定该项。

//TOOLS 使用的编译工具,vc2005对应的是vc-8_0

//PYTHON_ROOT python的安装目录,如果未将BOOST安装到默认位置,你必须指定该项。

//BUILD 编译结果选项,默认会生成尽可能多的版本,如调试版/发行版,静态库/动态库,单线程/多线程。

bjam 命令说明

Boost.Build V2 (Milestone 12)

Boost.Jam 03.1.16

Project-specific help:

Project has jamfile at Jamroot

Usage:

bjam [options] [properties] [install|stage]

Builds and installs Boost.

Targets and Related Options:

install Install headers and compiled library files to the

======= configured locations (below).

--prefix=<PREFIX> Install architecture independent files here.

Default; C:\Boost on Win32

Default; /usr/local on Unix. Linux, etc.

--exec-prefix=<EPREFIX> Install architecture dependent files here.

Default; <PREFIX>

--libdir=<DIR> Install library files here.

Default; <EPREFIX>/lib

--includedir=<HDRDIR> Install header files here.

Default; <PREFIX>/include

stage Build and install only compiled library files

===== to the stage directory.

--stagedir=<STAGEDIR> Install library files here

Default; ./stage

Other Options:

--build-type=<type> Build the specified pre-defined set of variations

of the libraries. Note, that which variants get

built depends on what each library supports.

minimal (default) - Builds the single

"release" version of the libraries. This

release corresponds to specifying:

"release <threading>multi <link>shared

<link>static <runtime-link>shared" as the

Boost.Build variant to build.

complete - Attempts to build all possible

variations.

--build-dir=DIR Build in this location instead of building

within the distribution tree. Recommended!

--show-libraries Displays the list of Boost libraries that require

build and installation steps, then exit.

--layout=<layout> Determines whether to choose library names

and header locations such that multiple

versions of Boost or multiple compilers can

be used on the same system.

versioned (default) - Names of boost

binaries include the Boost version

number and the name and version of the

compiler. Boost headers are installed

in a subdirectory of <HDRDIR> whose

name contains the Boost version

number.

system - Binaries names do not include

the Boost version number or the name

and version number of the compiler.

Boost headers are installed directly

into <HDRDIR>. This option is

intended for system integrators who

are building distribution packages.

--buildid=ID Adds the specified ID to the name of built

libraries. The default is to not add anything.

--help This message.

--with-<library> Build and install the specified <library>

If this option is used, only libraries

specified using this option will be built.

--without-<library> Do not build, stage, or install the specified

<library>. By default, all libraries are built.

Properties:

toolset=toolset Indicates the toolset to build with.

variant=debug|release Select the build variant

link=static|shared Whether to build static or shared libraries

threading=single|multi Whether to build single or multithreaded binaries

runtime-link=static|shared

Whether to link to static or shared C and C++ runtime.

Configuration help:

Configuration file at F:\Develop\boost_1_37_0 C++\boost_1_37_0\tools\build\v2

user-config.jam

This file is used to configure your Boost.Build installation. You can modify

this file in place, or you can place it in a permanent location so that it

does not get overwritten should you get a new version of Boost.Build. See:

http://boost.org/boost-build2/doc/html/bbv2/reference.html#bbv2.reference.init

for documentation about possible permanent locations.

General command line usage:

bjam [options] [properties] [targets]

Options, properties and targets can be specified in any order.

Important Options:

* --clean Remove targets instead of building

* -a Rebuild everything

* -n Don't execute the commands, only print them

* -d+2 Show commands as they are executed

* -d0 Supress all informational messages

* -q Stop at first error

* --debug-configuration Diagnose configuration

* --debug-building Report which targets are built with what properties

* --debug-generator Diagnose generator search/execution

Further Help:

The following options can be used to obtain additional documentation.

* --help-options Print more obscure command line options.

* --help-internal Boost.Build implementation details.

* --help-doc-options Implementation details doc formatting.

8.我的编译命令,编译所有版本.

bjam --toolset=msvc-9.0 --prefix=D:\boost_1_47_0\BoostLibAndDll --build-type=complete install

等待编译完成.

9.设置开发环境

打开VS2008 选择 工具->选项->vc++目录

设置包含文件目录D:\boost_1_47_0\boost

设置引用文件目录:D:\boost_1_47_0\BoostlibAndDll\lib

完成.可以使用了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: