C++之Boost使用_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++之Boost使用

C++之Boost使用

 2011/10/14 9:37:59  Goldice  http://jdoc.iteye.com  我要评论(0)
  • 摘要:1.Get&Build&InstallBoostdownloadboostfromhttp://www.boost.org/进入boost目录,使用命令:./bootstrap.sh--prefix=path/to/installation./b2install如此之后:leaveBoostbinariesinthelib/subdirectoryofyourinstallationprefix
  • 标签:使用 c++

1. Get & Build & Install Boost

download boost from?http://www.boost.org/

进入boost目录,使用命令:

./bootstrap.sh --prefix=path/to/installation

./b2 install

如此之后:

leave Boost binaries in the?lib/?subdirectory of your installation prefix. You will also find a copy of the Boost headers in theinclude/?subdirectory of the installation prefix, so you can henceforth use that directory as an?#include?path in place of the Boost root directory.

?

2. Use Boost

1) Header-Only Libraries

Most Boost libraries are?header-only: they consist?entirely of header files?containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.

比如下面这个例子,使用的就是header-only的library.

?

#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) << " " );
}

?

编译:

?

c++ -I path/to/boost_1_47_0 example.cpp -o example

then:

?

echo 1 2 3 | ./example
?

?

?

2)?Separately-Compiled Binary

?

#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++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test  \
-L /home/bin.jinb/usr/local/boost/lib/ -lboost_regex   

?

或者:

g++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test \
 /home/bin.jinb/usr/local/boost/lib/libboost_regex.a 
?

?

?

?

?

发表评论
用户名: 匿名