`
happmaoo
  • 浏览: 4334007 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

boost 下载,安装,试用

阅读更多


1下载

官方网站: www.boost.org 按照连接到sourceforge指定地方下载

小窍门: 一般国内的ftp上都会有,所以不一定需要浪费时间和金钱去外网上下载。

比如,我download boost_1_33_1.zip 就是用sf搜到了中科大的一个ftp直接下载的。

2安装

我目前只是在window机器上使用了一下。

解压后,

需要设置两个地方。

第一,执行tools\build\jam_src 下的build.bat,会生成\bin.ntx86\bjam.exe,将bjam.exe拷贝到你的boost库的根目录下,在命令行下执行之。过程有点长,plz wait! 另外,因为它默认对多种编译器都同时进行了配置,所以如果你没有装一些编译器的话,会看到屏幕上会打印出很多系统找不到指定目录的信息,没关系。比如,我只装了VC6,所以会打印很多找不到VC7的信息。

第二,设置编译器查找库文件路径的地方,比如VC6中, tools->option->directory, 加入你的boost库根路径就可以了

3 试用

from: dozb的程序人生

字符串→数值

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

数值→字符串
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
int main()
{
using std::string;
const double d = 123.12;
string s = boost::lexical_cast<string>(d);
std::cout<<s<<std::endl;
return 0;
}

异常
如果转换失败,则会有异常bad_lexical_cast抛出。该异常类是标准异常类bad_cast的子类。

#include <boost/lexical_cast.hpp>
#include <iostream>
int main()
{
using std::cout;
using std::endl;
int i;
try{
i = boost::lexical_cast<int>("abcd");
}
catch(boost::bad_lexical_cast& e)
{
cout<<e.what()<<endl;
return 1;
}
cout<<i<<endl;
return 0;
}
显然“abcd”并不能转换为一个int类型的数值,于是抛出异常,捕捉后输出“bad lexical cast: source type value could not be interpreted as target”这样的信息。

注意事项
lexical_cast依赖于字符流std::stringstream(会自动引入头文件<sstream>[4]),其原理相当简单:把源类型读入到字符流中,再写到目标类型中,就大功告成。例如

int d = boost::lexical_cast<int>("123");
就相当于
int d;
std::stringstream s;
s<<"123";
s>>d;
既然是使用了字符流,当然就有些随之而来的问题,需要特别指出[5]。
由于Visual C++ 6的本地化(locale)部分实现有问题,因此如果使用了非默认的locale,可能会莫名其妙地抛出异常。当然,一般情况下我们并不需要去改变默认的locale,所以问题不是很大。

输入数据必须“完整”地转换,否则抛出bad_lexical_cast异常。例如

int i = boost::lexical_cast<int>("123.123"); // this will throw

便会抛出异常。因为“123.123”只能“部分”地转换为123,不能“完整”地转换为123.123。

浮点数的精度问题。


std::string s = boost::lexical_cast<std::string>(123.1234567);

以上语句预想的结果是得到“123.1234567”,但是实际上我们只会得到“123.123”,因为默认情况下std::stringstream的精度是6(这是C语言程序库中的“前辈”printf留下的传统)。这可以说是boost::lexical_cast的一个bug。怎么办呢?权宜之计,可以这么做:打开头文件<boost/lexical_cast.hpp>,注意对照修改[6]:
#include <boost/limits.hpp>
//...
template<typename Target, typename Source>
Target lexical_cast(Source arg) {
//...
Target result;
interpreter.precision(std::numeric_limits<Source>::digits10);
if( !(interpreter << arg) ||
!(interpreter >> result) ||
!(interpreter >> std::ws).eof())
//...
}
即可得到正确结果

4 说明

做到以上几步,恭喜您,大部分Boost库就可以用了。

为什么不是全部?首先,目前还没有一个能完全符合C++标准的编译器,所以Boost库中的组件或多或少不可用,详细信息请看Boost网站上“编译器支持情况(Compiler Status)”一文。另外,有些库需要Build相应的lib或dll文件。不过这样的库很少,主要是由于平台相关性的原因,如处理正则表达式的regex库、支持python语言的python库等,而建构库的过程相当烦琐,需要使用Jam工具(可以简单提一下:在tools/build/jam_src/builds目录下有三个文件win32-borlandc.mk、win32-gcc.mk、win32-visualc.mk,分别是适用于Windows平台下的Borland C++ Compiler、GNU C++和Visual C++的mak文件。如果在Unix平台,则应使用tools/build/Makefile。用命令行工具make或nmake来做出Jam执行文件,然后再用Jam来建构库,详细内容可见Boost.Build文档)。我个人的建议是,不用急着去建构lib或dll。真的需要使用这些库时,再make随库提供的mak文件即可。虽然Boost.Jam也许是Boost库未来发展的方向,不过毕竟绝大部分库都无须建构,可以直接使用。

5. regex库的安装,试用

先看看regex自带的文档对于MIcrosoft VC 编译器安装的描述

Microsoft Visual C++

You need version 6 of MSVC to build this library. If you are using VC5 then you may want to look at one of the previous releases of this library

Open up a command prompt, which has the necessary MSVC environment variables defined (for example by using the batch file Vcvars32.bat installed by the Visual Studio installation), and change to the <boost></boost>\libs\regex\build directory.

Select the correct makefile - vc6.mak for "vanilla" Visual C++ 6 or vc6-stlport.mak if you are using STLPort.

Invoke the makefile like this:

nmake -fvc6.mak

You will now have a collection of lib and dll files in a "vc6" subdirectory, to install these into your development system use:

nmake -fvc6.mak install

The lib files will be copied to your <vc6></vc6>\lib directory and the dll files to <vc6></vc6>\bin, where <vc6></vc6>is the root of your Visual C++ 6 installation.

You can delete all the temporary files created during the build (excluding lib and dll files) using:

nmake -fvc6.mak clean 

Finally when you use regex++ it is only necessary for you to add the <boost></boost>root directory to your list of include directories for that project. It is not necessary for you to manually add a .lib file to the project; the headers will automatically select the correct .lib file for your build mode and tell the linker to include it.

Note that if you want to dynamically link to the regex library when using the dynamic C++ runtime, define BOOST_REGEX_DYN_LINK when building your project.

If you want to add the source directly to your project then define BOOST_REGEX_NO_LIB to disable automatic library selection.

There are several important caveats to remember when using boost.regex with Microsoft's Compiler:

* There have been some reports of compiler-optimization bugs affecting this library, (particularly with VC6 versions prior to service patch 5) the workaround is to build the library using /Oityb1 rather than /O2. That is to use all optimization settings except /Oa. This problem is reported to affect some standard library code as well (in fact I'm not sure if the problem is with the regex code or the underlying standard library), so it's probably worthwhile applying this workaround in normal practice in any case. * If you have replaced the C++ standard library that comes with VC6, then when you build the library you must ensure that the environment variables "INCLUDE" and "LIB" have been updated to reflect the include and library paths for the new library - see vcvars32.bat (part of your Visual Studio installation) for more details. * If you are building with the full STLPort v4.x, then use the vc6-stlport.mak file provided and set the environment variable STLPORT_PATH to point to the location of your STLPort installation (Note that the full STLPort libraries appear not to support single-thread static builds). * If you are building your application with /Zc:wchar_t then you will need to modify the makefile to add /Zc:wchar_t before building the library.

先做一点试试,我的平台还是windows 2003+ VC6.0

step1:找到文件vcvars32.bat ,备用

step2: 打开一个cmd窗口,并把路径切换到我的boost库目录下的 \libs\regex\build 子目录,找到vc6.mak文件

step3: 运行 'nmake -fvc6.mak ' ,错误提示: Error MSVCDIR 变量没有指定! 解决办法:在vcvars32.bat中找到

类似"set MSVCDir=C:\PROGRA~1\MICROS~2\VC98" 的一行,拷贝等号后面的路径。用UE打开vc6.mak文件

在前面点的位置 定义怎么一行"MSVCDIR = C:\PROGRA~1\MICROS~2\VC98"。 把这个新的mak文件另存为vc6_new.mak

step 4: 运行 'nmake -fvc6_new.mak ' , 然后便会产生所需的库文件等

step5: 运行 'nmake -fvc6_new.mak install' ,就会自动将regex库文件拷贝到 MSVCDIR 指定的目录下的相关目录下(如\bin,\libs等)

这样就基本搞定了。关于什么regex++,我还没有试过。

试用

现在找到regex程序小试一下:

实现一个简单的split功能

#include <list>
#include <iostream>
#include <boost/regex.hpp>
using namespace std;

unsigned tokenise(std::list<std::string>& l, std::string& s)
{
return boost::regex_split(std::back_inserter(l), s);
}

int main()
{
string s = "i am a boy!";
list<string> l;
unsigned result = tokenise(l, s);
cout << result << " tokens found" << endl;

while(l.size())
{
s = *(l.begin());
l.pop_front();
cout << s << endl;
}

return 0;
}
运行成功!

分享到:
评论

相关推荐

    boost_下载,安装,试用

    boost_下载,安装,试用

    boost安装文件

    boost的安装文件1.51版本的,适用64位机器。大家可以试用下,boost出来的也很齐全

    内存加速和优化软件RamBoostMasterV6.1.0.8146绿色汉化版

    RAM Boost Master美貌与实用并重,界面做得不错,而内存整理能力相当出色,或去你已经使用过许多内存优化整理的软件,例如:RamCleaner,完美内存整理,不过今天这个RAM Boost Master值得再次试用一下。跟往常的...

    Ccode_boost_withOCP.plecs

    基于plecs的双闭环PID仿真,闭环思路也试用与simulink,

    json.hpp:使用Boost.Spirit的小型C ++ JSON解析库

    我的主要目标只是在实际用例中试用Spirit,并能够尽可能轻松地将JSON解析为boost::variant 。 用法 解析JSON: # include # include int main () { using boost::get; std::string json_string{ " { \" names...

    eBoostr V1.1 Build399破解版

    从官网上下载的原版eboostr,基本上是免费的,没有功能限制,但是每次启动都提示你要注册,你可以选择继续试用,但是每次重启都只能使用4个小时,要想继续用,就得再重新启动。 由于网上基本上没有注册码,另外...

    IIS服务器SSI(rewrite重写)插件的帮助资料

    下载并安装某个版本的ISAPI_Rewrite.msi,依照安装向导的说明操作。安装过程是自动的,并有自我描述。ISAPI_Rewrite在自动安装过程中必须重启IIS。下列服务必须被重启:IISADMIN、W3SVC。不需要手工把任何ISAPI筛选...

    eBoostr(闪存变内存) 绿色破解版4.5.0.575

    Vista有一项新增的ReadyBoost功能,可以将闪存的容量当磁盘缓存用,现在XP的用户有福了,用eboostr这个软件也可以达到Readyboost的效果。 eBoostr(闪存变内存),依靠其高速随机访问能力,即U盘的随机访问数据延时...

    Telerik_JustMock_2010.2.810_Trial

    最新版:Try the richest ... Boost your applications and experience dramatic productivity gains across the whole development stack: from the data layer, to user interface, to functional and unit testing.

    Booost for Depop-crx插件

    尝试BooSt 7天免费试用 - 无需信用卡 - 19.99英镑/ Mo! 质量关注和取消关注 使用Booost for Depop自动关注用户和增益粉丝。 Booost还可以在按7501限制时取消关注用户。 - “我在8小时内获得了1000名粉丝!” 刷新...

Global site tag (gtag.js) - Google Analytics