boost的编译安装 以及boost regex的使用

来源:互联网 发布:电脑远程连接软件 编辑:程序博客网 时间:2024/05/18 01:04

1.由于这里我需要使用boost regex 的unicode支持 所以首先下载icu

下载地址为:http://download.chinaunix.net/download.php?id=44840&ResourceID=13494

下载解压到/usr/local/src目录 然后

进入icu的source目录 运行

./configure CC=gcc CXX=g++ CFLAGS=-O3 CXXFLAGS=-O3

然后make & make install即可

然后编写测试程序 参照http://blog.csdn.net/liyuwenjing/article/details/6105388

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unicode/utypes.h>#include <unicode/ucol.h>#include <unicode/ustring.h>#include <unicode/coll.h>int main(void){    UErrorCode success;    Collator *myCollator = Collator::createInstance(success);    if(myCollator->compare("abc","ABC")<0)    {        printf("abc is less than ABC\n");    }    else    {        printf("abc is greater than ABC");    }    return 0;}

不过原文的编译选项缺了一些东西

首先gcc main.c -o test -licuio -licui18n -licuuc  (三个选项一个都不能少)

 然后export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

./test即可

2.boost的编译安装

下载并解压到/usr/local/src

然后./bootstrap.sh --with-icu

./b2

./b2 install 

这样的话会默认的prefix=/usr/local


测试boost是否安装正确

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

然后g++ test.cpp -o test  (因为我默认安装在/usr/local 所以不需要链接库)

echo 1 2 3|./test

会输出3 6 9

表示boost安装成功

3.boost regex的使用

boost regex在boost中属于需要独立编译的类 独立编译的类在boost中还包括

Boost.Filesystem
Boost.GraphParallel
Boost.IOStreams
Boost.MPI
Boost.ProgramOptions
Boost.Python (see the Boost.Python build documentation before building and installing it)
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.System
Boost.Thread
Boost.Wave

以下参见http://www.wuzesheng.com/?p=965

下面是具体的步骤:(假设下载完后的,代码解压在了BOOST_ROOT目录)
(1)进入到BOOST_ROOT/libs/regex/build目录
(2)如果要使用静态库,请执行make -fgcc.mak
(3)如果要使用静态库,请执行make -fgcc-shared.mak

执行完上面三步后的,在BOOST_ROOT/libs/regex/build/下会生成一个gcc目录 ,进入该目录 ,可以看到生成了下面四个文件:
(1)libboost_regex-gcc-1_42.a , 这是release版的静态库
(2)libboost_regex-gcc-1_42.so, 这是release版的动态库(共享库)
(3)libboost_regex-gcc-d-1_42.a, 这是debug版的静态库
(4)libboost_regex-gcc-d-1_42.so, 这里debug版的动态库(共享库)

我的版本已经是1.51了  然后我将这四个文件复制到了/usr/local/lib/boost_regex下

创建测试程序 

#include <string>#include <iostream> #include "boost/regex.hpp" int main(int argc, char ** argv){    if (argc != 4)    {           std::cerr < < "Usage: " << argv[0] << " option regex text\n"            << "option: 0 --whole match\n"            << "        1 --sub match\n"            << "        2 --replace match\n";        return 1;    }        boost::regex oRegex(argv[2]);    boost::smatch oResults;    std::string strStr(argv[3]);    std::string strRes;     switch (atoi(argv[1]))    {           case 0:            if(boost::regex_match(strStr, oResults, oRegex))            {                   std::cout << strStr << " matches " << oRegex << "\n";            }               else            {                   std::cout << strStr << " doesn't match " << oRegex << "\n";            }               break;        case 1:            if(boost::regex_search(strStr, oResults, oRegex))            {                std::cout << strStr << " matches " << oRegex << "\n";            }            else            {                std::cout << strStr << " doesn't match " << oRegex << "\n";            }            break;        case 2:            strRes = boost::regex_replace(strStr, oRegex, "$$$$");            std::cout << "strRes=" << strRes << "\n";            break;        default:            std::cerr << "Invalid option: " << argv[1] << "\n";            break;    }}

编译方法如下

g++ main.cpp -L /usr/local/lib/boost_regex/ -lboost_regex-gcc-1_53 -o BoostRegex

export LD_LIBRARY_PATH=/usr/local/lib/boost_regex:$LD_LIBRARY_PATH 

然后分别运行如下

/BoostRegex 0 "http:\/\/www\..*\.com" "http://www.soso.comjfj"

得到http://www.soso.comjfj doesn't match http:\/\/www\..*\.com

./BoostRegex 1 "http:\/\/www\..*\.com" "http://www.soso.comjfj"

得到http://www.soso.comjfj matches http:\/\/www\..*\.com

./BoostRegex 2 "http:\/\/www\..*\.com" "http://www.soso.comjfj"

得到./BoostRegex 2 "http:\/\/www\..*\.com" "http://www.soso.comjfj"

strRes=$$jfj


4.boost的regex使用

未完待续


                                             
0 0