编译BOOST库(Visual Studio 2010/GCC)

4 篇文章 0 订阅
4 篇文章 0 订阅

1. 

Windows 7 64bit和Visual Studio 2010下安装及使用Boost 1.47.0

 

Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。 Boost库由C++标准委员会库工作组成员发起,在C++社区中影响甚大,其成员已近2000人。本文主要介绍了如何安装及使用Boost 1.47.0。
参考网页:http://tech.ddvip.com/2009-05/1242887529120322.html

    1). 在http://www.boost.org/users/download/下载最新版Boost库。解压后将其复制到指定位置。

    2). 打开Visual Studio Command Prompt,进入解压后的Boost目录。输入bootstrap,等待其生成Boost构建工具bjam。
Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
    3). 在命令行输入bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static。然后等待安装完成。如果要生成64位库,键入命令bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static address-model=64。Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
    4). 安装完成后,屏幕上会提示"The Boost C++ Libraries were successfully built!"。并制定头文件和库文件目录。Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
    5). 新建一个win32控制台程序,命名为Boost_Test。代码后附。

    6). 在Project -> Boost_Test Properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories 中增加头文件路径。Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
    7). 在Project -> Boost_Test Properties -> Configuration Properties -> Linker -> General -> Additional Library Directories 中增加库文件路径。这里我们不用制定需要链接的静态库,Boost会自动加载需要的库文件。Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
    8). 编译,链接,测试结果如下。
Windows <wbr>7 <wbr>64bit和Visual <wbr>Studio <wbr>2010下安装及使用Boost <wbr>1.47.0
Boost_Test.cpp代码
// Boost_Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/regex.hpp>
using namespace std;
struct Hello
{
    Hello()
    {
        cout << "Hello constructor" << endl;
    }

    ~Hello()
    {
        cout << "Hello destructor" << endl;
        cin.get();
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    boost::regex regex("^(Hello|Bye) Boost$");
    boost::cmatch helloMatches;
    boost::regex_search("Hello Boost", helloMatches, regex);
    cout << "The word between () is: " << helloMatches[1] << endl;
    boost::shared_ptr<Hello> sharedHello(new Hello);
    cin.get();
    return 0;
}


2.如何用Visual Studio 2010编译boost库

出处:http://www.cnblogs.com/LinuxHunter/archive/2010/11/28/1890603.html

1.启动Visual Studio 2010命令行提示

2.进入boost根目录,进入其子目录\tools\jam\src

3.输入命令build vc10

  根据不同编译器可输入如下:

   >>BCC5.5/BCB6/BCB2006/CB2009用户输入build borland

   >>VC用户依据其版本输入build vc7vc8vc9vc10

   >>Mingw用户输入build mingw

4.将生成的bjam.exe拷贝到boost根目录

5. 将命令行回退到boost根目录,输入如上命令

bjam --toolset=msvc stage   注:--toolset=borland(对应BCB)   =msvc(对应VC)   =gcc(对应Mingw)

bjam参数

--build-dir=<builddir>
编译的临时文件会放在builddir里(这样比较好管理,编译完就可以把它删除了)

--stagedir=<stagedir>
存放编译后库文件的路径,默认是stage

--build-type=complete
编译所有版本,否则只会编译一小部分版本(确切地说是相当于:variant=release, threading=multi;link=shared|static;runtime-link=shared)

variant=debug|release
决定编译什么版本(Debug or Release?)

link=static|shared
决定使用静态库还是动态库。

threading=single|multi 
决定使用单线程还是多线程库。

runtime-link=static|shared
决定是静态还是动态链接C/C++标准库。

--with-<library>
只编译指定的库,如输入--with-regex就只编译regex库了。

--show-libraries 
显示需要编译的库名称

6.若要用到python库且有多个python版本时添加如下命令参数,路径视情况自定:

"-sPYTHON_ROOT=D:\Python" "-sPYTHON_VERSION=2.6"


3.vs2010设置boost开发环境


<1>. 编译boost类库 

前面一篇文章介绍了如何在linux下建立boost的开发环境,并且编写了一个测试程序,这里将试着在windows下编译boost,同时设置vs2010的开发环境,使用vs来作为开发的ide。

1. 下载bjam.exe

/Files/xuqiang/bjam.rar

2. 编译boost

将下载的bjam.exe放置在boost源代码的目录下,简单的编译的话,直接双击bjam.exe即可,开始编译,这个过程可能持续时间比较长。

3. 编译完成之后,安装boost,下面的命令将boost安装到e:\boost目录下。

bjam.exe --prefix=e:\boost install

之后将boost安装到 e:\boost中,包含相关头文件和lib文件

 

<2>. 设置vs2010的环境 

1. 新建一个vc控制台工程,编写代码

复制代码
#include  < boost / regex.hpp >
#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[ 2 <<  std::endl;
    }
复制代码

2. 设置工程属性 

设置header file路径:


设置lib路径:



4.linux下boost开发环境建立


1. 下载boost c++库(http://www.boost.org/),这里使用的是1.46.1版本的。

2. 在linux下进行简单编译。

2.1 解压该文件,解压完成后目录如下:

 

2.2 在boost的跟目录执行如下命令开始配置:


2.3 上面的过程完成之后,开始编译boost库:


2.4 经过漫长的过程(如果按照山谜案的默认的配置的话,时间真的很长),完成boost的编译。其中可能出现的错误:

1. error: bzlib.h: No such file or directory boost ,解决方法,boost编译时需要依赖libbz2库:sudo apt-get install libbz2-dev

安装boost:

 

3. 测试一下

下面就是用一个简单的例子来体验一下boost的威力。

 


 

 

#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
        using boost::lexical_cast;
        int a = lexical_cast<int> ("123");
        std::cout << a << std::endl;
        return 0;
}

编译程序:

 

运行结果:

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值