Visual Studio 2010 下 Boost.Regex 库的使用笔记

原文链接:http://www.cnblogs.com/PerfectString/archive/2010/08/30/Demo_Boost_Regex.html

最近玩了一把 Boost 中的 Regex 库,发现网上很多文章写的晦涩难懂,更让人生气的是按照其指导是做不出来的,综合几篇文章和自己的实际使用的总结,写个笔记备忘。

按照我自己的老习惯来,先直捣黄龙,能够运行实例再说,把东西做出来之后我们再来进一步的了解相关知识点。 开始吧

1,下载 Boost Release 1.44.0 的文件包,下载的位置:
然后解压缩到自己设定的位置,我这里设定就是 C:\Boost ,名称越简洁越好,以免发生意外。

2,设定 msvc 的版本跟编译器信息

可能很多人不愿意跟我一样使用这样的目录,为了方便起见,这里用 $BOOST$ 代表你的 Boost 的安装或者解压缩路径,比如在我的机器下,$BOOST$ 就等价于 C:\Boost 。

在 $BOOST$\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 ;

#添加 Visual Studio 2010 的配置

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

如果是 Visual Studio 2005 就是 8.0 ,Visual Studio 2008 就是 9.0 ,这个应该没有疑问的。偷懒就直接粘贴过去吧,这个记下来也没啥用。

3,生成 bjam
$BOOST$ 目录下面,有个文件 bootstrap.bat 的批处理文件,双击或者在命令行提示符窗口中打开,稍等片刻,就会在该目录下生成 bjam.exe 以及它的 log 文件。

4,编译 Boost::Regex,注意,这里只编译  Boost::Regex 库。
到 $BOOST$ 目录下,键入 bjam --toolset=msvc --with-regex stage ,或者直接复制粘贴吧,免得麻烦。
编译生成的库文件会被在目录 $BOOST$\stage\lib 中。
比如,在我的机器上就是:
C:\BOOST\STAGE\LIB
    libboost_regex-vc100-mt-1_44.lib
    libboost_regex-vc100-mt-gd-1_44.lib
    libboost_regex-vc100-mt-gd.lib
    libboost_regex-vc100-mt.lib

同时还会生成 bin.v2 这个临时目录,可以查看一下结构,硬盘空间紧张的话,这个目录可以删除掉的,我的目录结构如下:
C:\BOOST\BIN.V2
└─libs
    └─regex
        └─build
            └─msvc-10.0
                ├─debug
                │  ├─link-static
                │  │  └─threading-multi
                │  └─threading-multi
                └─release
                    └─link-static
                        └─threading-multi

5,好了,既然成功的编译成功了,下面就来运行实例。我采用的是《Beyond the C++ Standard Library: An Introduction to Boost》这本书中的实例,简单,小巧。能测试成功,就代表你这边文章没白看。我附上这本书的下载链接吧。

6,打开 Visual Studio 2010 ,接着应该在读这篇文章的各位应该驾轻就熟了。
File->New->Project->Win32 Console Application ,name 的地方取个项目名称了,我就叫:RegexDemo 好了
接着next,在Additional options 的地方,选择Empty project,我们自己弄,不要什么预编译头文件(Precompiled header)了,然后 Finish 。

7,新建一个cpp文件,把下面的代码复制到文件中,我也是直接复制原书中的例子,但是加了几行代码,不然的话,编译成功后,运行了只能看到:“Press any key to continue.....” >_< ! 太无趣了。

接下来设置 include 目录和 lib 目录,Visual Studio 2010 的设置方法有点不一样,需要在你的工程上右键属性才可以。

 #include <iostream>
 #include <cassert>
 #include <string>
 #include <boost/regex.hpp>
 
  using namespace std;
 
  int main()
 { 
     // 3 digits, a word, any character, 2 digits or "N/A", 
     // a space, then the first word again 
      boost::regex reg("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");   
     std::string correct="123Hello N/A Hello"; 
     std::string incorrect="123Hello 12 hello";  
 
     /* 原来的代码
     assert(boost::regex_match(correct,reg)==true); 
     assert(boost::regex_match(incorrect,reg)==false);
     */
 
     // 新添加几行玩一下。
      bool b1 = boost::regex_match(correct,reg);
     bool b2 = boost::regex_match(incorrect,reg);
     if(b1)
     {
         cout << "correct!!!" << endl;
     }
     cout << "=======================================" << endl;
     if(!b2)
     {
         cout << "incorrect!!!" << endl;
     }
 
     return 0;
 }

   
   

F7 编译,结果自然是:========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== Ctrl + F5 直接运行,看到结果了吧。至于例子不解释了,自己看书。因为这个设计到正则表达式的知识,不在本文讨论范围。

说好了的,我们运行完实例后,开始相关知识点拓展,不要急躁哈。*^_^*

 

以下还没有完全编辑好=============================================================

1.完全编译安装: bjam --toolset=msvc install 完成后会生成一个bin.v2编译时的临时目录,手动删除。生成另一个目录C:\boost,里面为所有的头文件和库文件。头文件目录为boost_1_34_1\boost目录复制过去的。

2.只编译相应的库文件 bjam --toolset=msvc stage 完成后同样会生成bin.v2临时目录。另一个目录为stage文件,里面有对应的库文件。

3.查看需要编译才能使用的库列表 bjam --show-libraries

4.编译特定的库,如只编译regex bjam --toolset=msvc --with-regex stage 生成的库文件在stage目录中。

5.不编译某个库,如不编译regex bjam --toolset=msvc --without-regex stage 生成的库文件在stage目录中。

6.编译特定的库,如只编译regex,生成debug,多线程,共享连接版本,并保存在stage。 bjam --toolset=msvc --with-regex stage debug threading=multi link=shared

7.生成 mt-sgd 的静态库(runtime-link-static) bjam "-sTOOLS=vc-8_0" --with-thread install debug release runtime-link=static

8.编译regex库。 bjam --toolset=msvc --with-regex stage debug release threading=multi threading=single link=shared link=static runtime-link=shared runtime-link=static

boost的安装方法:

对于DLL版本 bjam --toolset=msvc link=shared runtime-link=shared threading=multi stage debug release install

对于lib版本 bjam --toolset=msvc link=static runtime-link=shared threading=multi stage debug release install

另外,在$BOOST$\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 ;   

然后进入 $BOOST$ 目录,执行bjam.exe 编译命令

//下面的命令的各选项的说明: //prefix    将boost安装到的路径(生成的头文件和库文件都会放到该路径中)。 //重定义以下变量(利用-s设置): //VC80_ROOT vc2005的安装路径,如果未将vc2005安装到默认位置,你必须指定该项。 //TOOLS         使用的编译工具,vc2005对应的是vc-8_0 //PYTHON_ROOT   ython的安装目录,如果未将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 $boost$\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.

编译所有版本:

bjam --toolset=msvc-8.0 --prefix=$lib-and-dll-out-dir$ --build-type=complete install 等待编译完成.

设置开发环境:

打开VS2005 选择 工具->选项->vc++目录 设置包含文件目录$lib-and-dll-out-dir$\include\boost-1_37\boost 设置引用文件目录:$lib-and-dll-out-dir$\lib 完成.可以使用了.

我的命令 编译静态链接库的完全版本 windows: bjam --toolset=msvc link=static runtime-link=shared runtime-link=static threading=multi stage debug release install

linux: bjam --toolset=gcc link=static stage debug release install

吃水别忘挖井人,附上参考资料: http://www.boost.org/doc/libs/1_34_1/libs/regex/doc/install.html http://www.boost.org/boost-build2/doc/html/bbv2/reference/tools.html#bbv2.reference.tools.compiler.msvc http://hi.baidu.com/quant_master/blog/item/9174717adbc5f3e72e73b38c.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值