Windows下Boost库安装方法

原文地址为: Windows下Boost库安装方法

1. 概述

    最近打算学习一下Boost库,这里记录一下Boost库的安装的方法,IDE涉及DEV C++和VS 2008。

2. 下载

    直接下载链接:参见网页 http://www.boost.org/users/history/version_1_46_1.html,这里的链接网速很慢,78MB的东西,下载了10%左右,就定住了,试了三次都是这样,基本放弃了这个下载链接。
    SVN下载链接,文件保存在“D:\Boost”下:参见网页 http://www.boost.org/users/download/,有一定速度,下载使用了49分钟,传了115.99MB,版本为1.47,我看了下下载的文件夹,吓了我一跳:文件夹大小501MB,占用空间3.15GB。赶紧去网上查了下,有的人说对于1.46版本的Boost,如果完全编译后,可能需要12-15GB的空间,这样就不奇怪了,虽然还没编译就占了3GB。

3. VS2008 Boost库编译(-vc9)

    首先,编译bjam,在命令行下,运行bootstrap.bat -vc9
    然后,编译库。在命令行下,运行:        

    bjam stage  -- toolset = msvc - 9.0   -- without - graph  -- without - graph_parallel  -- without - math  -- without - mpi  -- without - python  -- without - serialization  -- without - wave  -- stagedir = " D:\Boost\bin\vc9 "  link = static  runtime - link = shared threading = multi debug release
   bjam stage 
-- toolset = msvc - 9.0   -- without - graph  -- without - graph_parallel  -- without - math  -- without - mpi  -- without - python  -- without - serialization  -- without - wave  -- stagedir = " D:\Boost\bin\vc9 "  link = static  runtime - link = static  threading = multi debug release

    编译用了50分钟左右,产生了303MB的文件。

 4. VS2008 Boost库配置    

     Tools -> Options -> Projects and Solutions -> VC++ Directories
     在Library files中,增加D:\Boost\bin\vc9\lib
     在Include files中,增加D:\Boost\
     其中,Library的目录就是前面编译产生的那些库文件保存到的位置
   其中,Include的目录随着Boost的不同版本会不同,现在1.47版本只要指定为D:\Boost即使用SVN下载Boost的文件夹就可以了。

5. VS2008 Boost库测试   

961ddebeb323a10fe0623af514929fc1.jpe View Code
#include  " stdafx.h "
#include 
< iostream >
#include 
< boost / date_time / gregorian / gregorian.hpp >
using   namespace  std;
using   namespace  boost;

int  _tmain( int  argc, _TCHAR *  argv[])
{
    cout
<< " 请输入您的生日,格式\"YYYY-MM-DD\": " ;
    
string  strBirthday;
    cin
>> strBirthday;
    
try
    {
        gregorian::date birthday( gregorian::from_simple_string(strBirthday) );
        gregorian::date today( gregorian::day_clock::local_day() );
        gregorian::days days_alive 
=  today  -  birthday;
        
if ( days_alive  <  gregorian::days( 0 ) )
        {
            cout
<< " 哇,还没出生就能用电脑了,真厉害 " << endl;
        }
        
else
        {
            cout
<< " 您在这个世界上出现了: " <<  days_alive.days() <<   " 天了 "   <<  endl;
        }
    }
    
catch ( gregorian::bad_year &  e )
    {
        cerr
<<  e.what()  <<  endl;
    }
    
catch ( gregorian::bad_day_of_month &  e )
    {
        cerr
<<  e.what()  <<  endl;
    }
    
catch ( gregorian::bad_day_of_year &  e )
    {
        cerr
<<  e.what()  <<  endl;
    }
    
catch (...)
    {
        cerr
<< " Error! " << endl;
    }
    system( 
" pause "  );
    
return   0 ;
}

 6. DEV C++ Boost库编译

     首先,设置gcc的环境变量。在我的电脑上点击右键,选择Properties菜单项。然后在弹出的对话框中选择Advanced页,点击Environment Variables按钮。之后进行如下设置,选择PATH,然后点击Edit按钮,在最后加上DEV-C++编译器的路径,如C:\Program Files\DEV-CPP\Bin,路径之间用分号分隔。 设置完毕点击OK按钮保存。
     然后,编译bjam,运行bootstrap.bat -gcc
     接着,编译boost库:  

    bjam stage  -- toolset = gcc  -- without - graph  -- without - graph_parallel  -- without - math  -- without - mpi  -- without - python  -- without - serialization  -- without - wave  -- stagedir = " D:\Boost\bin\gcc "  link = static  runtime - link = shared threading = multi debug release
    bjam stage 
-- toolset = gcc  -- without - graph  -- without - graph_parallel  -- without - math  -- without - mpi  -- without - python  -- without - serialization  -- without - wave  -- stagedir = " D:\Boost\bin\gcc "  link = static  runtime - link = static  threading = multi debug release

7. DEV C++ Boost库配置

     在Toos->Compiler Options->Directories->C++ Include中,增加D:\Boost
8. DEV C++ Boost库测试

    第一种,只要包含该头文件即可,就能使用该头文件中的所有函数。

961ddebeb323a10fe0623af514929fc1.jpe View Code

#include <boost/lambda/lambda.hpp>  
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
    using namespace boost::lambda;
    typedef istream_iterator<int> in;   
    for_each(in(cin), in(), cout << (_1 * 3) << " ");
    return 0;
}

   第二种,需要建立一个project,而不能只是编译单个的c++文件,因为在dev c++中,只有在project中才能设置linker的参数。 建立project后,“Project"-->"Project Options"-->"Parameters"选项卡--->在linker框中添加:"-lboost_regex-mt",告诉linker在链接的时候,链接regex库。 

961ddebeb323a10fe0623af514929fc1.jpe View Code
#include  < boost / regex.hpp >
#pragma  comment(lib,"D:\boost\boost_1_42_0\stage\libboost_regex-meg34-mt.lib")
#include 
< iostream >
#include 
< string >
int  main()
{
    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[ 1 ] << "   " << matches[ 2 <<  std::endl;
    }
    
return   0 ;
}

9. 参考文献

   Boost下载安装编译配置使用指南(含Windows和Linux)  http://kb.cnblogs.com/a/1485890/ 
   VS2008中编译Boost 1.38  http://blog.csdn.net/wrx_2009/archive/2009/06/04/4242841.aspx 
   VS2008 Team System 安装Boost库  http://www.cookbus.com/show-121-1.html 
   boost在DevC++中的安装过程  http://blog.csdn.net/suwei19870312/archive/2011/03/13/6246400.aspx


转载请注明本文地址: Windows下Boost库安装方法

猜你喜欢

转载自blog.csdn.net/dearbaba_8520/article/details/82555668