ID #1600

Windows下如何使用BOOST C++库

我采用的是VC8.0和boost_1_35_0。自己重新编译boost当然可以,但是我使用了

http://www.boostpro.com/products/free

提供的安装工具 BoostPro 1.35.0 Installer (192K .exe) 。我强烈建议使用这个工具来在Windows下安装BOOST库和源文件。

1)使用boost_1_35_0_setup.exe这个工具下载boost库,选择你要的包(类型总是Mutilthread和Mutithread Debug),下载后自动安装。我用VC8.0的boost_1_35_0安装在E:\boost。我主要介绍用RegEx和Signals这2个需要编译后才能使用的库,

2)我在VC8.0下建立了一个Console工程,并为工程添加了VC包含目录:E:\boost\boost_1_35_0,和库目录:E:\boost\boost_1_35_0\lib。不需要指定链接哪个库,因为系统会自动查找的。

3)需要注意的是,我不使用动态链接库,因为一堆的警告,让我恐惧。因此我使用静态的连接库,就是名称前有libboost-xxx样式的库。比如,要使用(注意与下面的名称完全一致):

Debug下:

libboost_signals-vc80-mt-gd-1_35.lib

libboost_regex-vc80-mt-gd-1_35.lib

Release下:

libboost_signals-vc80-mt-1_35.lib

libboost_regex-vc80-mt-1_35.lib

而VC的项目属性是:

Debug:多线程调试 DLL (/MDd),不采用Unicode

Release:多线程 DLL (/MD),不采用Unicode

尤其要注意,使用工具下载的时候,总是下载:

Mutilthread和Mutithread Debug

这样的好处是,我们是链接到静态的boost库,所以,不需要任何boost的dll。不要为了贪图小一点尺寸的运行时包而选择使用boost的动态库,起码我看那一堆的警告就不寒而栗。

下面就是个小例子,没任何警告,一切如期:

///////////////////////////////////////////////////////////////////////////////
// main.cpp
//
// 使用BOOST C++标准库
//
//
// 2008-7-10 cheungmine
//
///////////////////////////////////////////////////////////////////////////////
#include <boost/lambda/lambda.hpp>
#include <boost/regex.hpp>

#include <iostream>
#include <cassert>

#include <boost/signals.hpp>

struct print_sum {
  void operator()(int x, int y) const { std::cout << x+y << std::endl; }
};

struct print_product {
  void operator()(int x, int y) const { std::cout << x*y << std::endl; }
};

//
// 主程序
//
int main(int argc, char** argv)
{
    boost::signal2<void, int, int, boost::last_value<void>, std::string> sig;

    sig.connect(print_sum());
    sig.connect(print_product());

    sig(3, 5);

    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;
    }

    return 0;
}


2011-07-01 16:30
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

用vc++穷举windows应用程序密码(续)

用vc++穷举windows应用程序密码(上)

利用VC++编写Windows95的CPL组件

如何在Windows应用程序中实现电子注册功能

用窗口子类化技术改进Windows编辑框控件的功能