`
cloudtech
  • 浏览: 4595181 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Windows下如何使用BOOST C++库

 
阅读更多

Windows下如何使用BOOST C++

cheungmine@gmail.com

2008-7-10

我采用的是VC8.0boost_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库,选择你要的包(类型总是MutilthreadMutithread Debug),下载后自动安装。我用VC8.0boost_1_35_0安装在E:/boost。我主要介绍用RegExSignals2个需要编译后才能使用的库,

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库,所以,不需要任何boostdll。不要为了贪图小一点尺寸的运行时包而选择使用boost的动态库,起码我看那一堆的警告就不寒而栗。

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

///////////////////////////////////////////////////////////////////////////////

// main.cpp

//

// 使用BOOST C++标准库

//

//

// <chsdate w:st="on" year="2008" month="7" day="10" islunardate="false" isrocdate="false">2008-7-10</chsdate> 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;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics