欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++之Boost使用 博客分类: Linux和开源软件 C++BoostLinux 

程序员文章站 2024-03-06 13:59:20
...

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 
 

 

 

 

 

相关标签: C++ Boost Linux