您的位置:首页 > 其它

Ubuntu8.04下安装Boost_1_37

2010-10-23 19:44 225 查看
refer to:http://hi.baidu.com/lvgb/blog/item/7e9c9feefd5caf2e2cf53477.html

     注:在安装Boost库之前,应该先安装bzip库和zlib库。否则,某些boost库可能会编译出错。

     Boost里有很多库,覆盖了广泛的领域,从数学库到智能指针,从模板元编程库到预处理器库,从线程到lambda表达式,等等。所有Boost库都具有宽松的许可证,确保库可以被自由使用于商用软件。

      我用Boost的目的是在C++程序中使用正则表达式,这通常有两个选择:Boost或PCRE(Perl Compatible Regular Expressions),据说PCRE的效率更高些,但考虑到Boost已经被提议加入到下一个版本的C++标准中,于是先试用了它。

     Boost的网站地址是:www.boost.org,我从那下载了boost_1_37_0.tar.bz2,将近30M字节,解压后达到260多M,真是个庞大的家伙。

    Boost中的许多东西只需要头文件就行,只要部分功能需要编译,很不幸,regex就需要编译。如果将Boost中的库全部编译的话需要50G的空间,好在我只需要Regex库,只用了62M。

   在编译前,先至解压目录下看看index.html文件,那里有关于编译和安装的详细介绍。

    下面是我的编译安装过程:

   1、配置Makefile

       ./configure --help   可查看configure的可配置选项

   ./configure --show-libraries 可查看Boost实现的库

   ./configure --with-libraries=regex --prefix=/usr/local/boost 这是我的配置,只编译regex,安装到/usr/local/boost下。

    2、编译

       make就行了

    3、安装

       make install

       这样安装后,在/usr/local下生成了boost目录,其中include文件在/usr/local/boost/include/boost-1_37/boost/下,lib文件在/usr/local/boost/lib下。

    4、测试

    写一段小程序:

     vi test.cpp:

#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;

    }

}

   然后编译:

     g++ -o test test.cpp -I /usr/local/boost/include/boost-1_37 -L /usr/local/boost/lib -lboost_regex-gcc42-mt-1_37

      参数-L用于指定Boost库文件的路径,-I用于指定Boost头文件的路径,由于在include语句为#inclue <boost/regex.hpp>,因此-I参数最后不加boost目录。

     运行:./test,告诉我:./test: error while loading shared libraries:
libboost_regex-gcc42-mt-1_37.so.1.37.0: cannot open shared object file:
No such file or directory

     找不到库文件,原来make install不自动执行ldconfig,手工来一遍:

     ldconfig /usr/local/boost/lib

     再运行:./test

                  输入:Subject: Re: ok

                  输出:ok

    说明一切正常,安装成功。

    5、重编译

   
虽然编译成功了,但由于Boost的头文件及库文件不在gcc及g++的默认路径下,每次用-L及-I参数指定太麻烦。要解决这个问题,可以直接把头文件
及库文件复制到/usr/local/include及/usr/local/lib目录下,也可以在使用configure生成makefile文件时
就考虑这个问题。

      make clean   清除上一次的编译结果

      ./configure --with-libraries=regex --prefix=/usr/local/boost
--libdir=/usr/local/lib/boost --includedir=/usr/local/include

      --libdir用于指定库文件的安装路径,--includedir用于指定头文件的安装路径。

    然后: make 再make install

   
检查一下安装路径是否正确:发现/usr/local/lib/boost/下的文件没问题,而/usr/local/include/boost竟然不
存在,安装程序生成的目录是/usr/local/include/boost-1_37/boost,于是只好:

cd /usr/local/include/boost-1_37

   mv boost ..

   cd ..

   rm -rf boost-1_37

   

    为保证以后每次ldconfig都可以加上boost库文件,修改/etc/ld.so.conf文件(或在/etc/ld.so.conf.d目录下增加一个文件),添加以下内容:

         /usr/local/lib/boost

      再运行一下ldconfig就ok了。

    
6、简便的安装方法

     其实用apt-get install libboost-regex-dev就可以直接安装Boost的regex库,只是我更愿意自己动手,感觉更清楚、更自在一些。

     用apt-get安装后,boost的库文件位于/usr/lib下,头文件位于/usr/include/boost下,并且自动ldconfig了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息