c++ boost库安装与测试

1、从官网http://www.boost.org/users/download/下载最新版本的boost,如boost_1_65_0

2、解压tar xzvf boost_1_65_0.tar.gz,

3、安装

cd boost_1_65_0/

./bootstrap.sh
sudo ./b2 install
 4、测试boost,以any类型和uuid为例。
#include <iostream>
#include <list>
#include <vector>
#include <boost/any.hpp>
#include <sys/time.h>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <uuid/uuid.h>

#include "test_stl.h"

using namespace std;
typedef std::list<boost::any> list_any;

//关键部分:可以存放任意类型的对象
void fill_list(list_any& la)
{
    la.push_back(10);//存放常数
    la.push_back( std::string("dyunze") );//存放字符串对象;注意la.push_back(“dyunze”)错误,因为会被当错字符串数组
}

//根据类型进行显示
void show_list(list_any& la)
{
    list_any::iterator it;
    boost::any anyone;

    for( it = la.begin(); it != la.end(); it++ )
    {
        anyone = *it;

        if( anyone.type() == typeid(int) )
            std::cout<<boost::any_cast<int>(*it)<<std::endl;
        else if( anyone.type() == typeid(std::string) )
            std::cout<<boost::any_cast<std::string>(*it).c_str()<<std::endl;
    }
}

// boost版本异常慢
void test_uuid_perf()
{
    // boost::uuids::uuid a_uuid;
    vector<string> myvertor;
    uuid_t uu;
    char uuid_str[37];
    
    struct timeval start, stop, diff;
    gettimeofday(&start, 0);   //开始计时
    for (int i=0;i<100000;i++)
    {
        // a_uuid = boost::uuids::random_generator()();
        // myvertor.emplace_back(boost::uuids::to_string(a_uuid));
        uuid_generate(uu);
    
        uuid_unparse_lower(uu, uuid_str);
        string str1(uuid_str);
        myvertor.emplace_back(str1);

    }
    cout << myvertor[10] << endl;
    gettimeofday(&stop, 0);   //结束计时
    timeval_subtract(&diff, &start, &stop);
    printf("总计用时:%d秒%d微秒\n",diff.tv_sec, diff.tv_usec);  // boost版本,10万个uuid 17秒多; uuid版本, 10万个0.5秒,35倍
    printf("完成");
}

int test_boost()
{
    list_any la;
    fill_list(la);
    show_list(la);

    test_uuid_perf();
    return 0;
}

注:boost本身大部分工具库为header-only库,一小部分专用的需要链接。从下也可知:

[zjh@hs-10-20-30-193 cpp_demo]$ cd /usr/local/lib
[zjh@hs-10-20-30-193 lib]$ ll | grep boost
-rw-r--r-- 1 root root 2602 Oct 1 16:11 libboost_atomic.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_atomic.so -> libboost_atomic.so.1.65.0
-rwxr-xr-x 1 root root 8392 Oct 1 16:11 libboost_atomic.so.1.65.0
-rw-r--r-- 1 root root 110550 Oct 1 16:11 libboost_chrono.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_chrono.so -> libboost_chrono.so.1.65.0
-rwxr-xr-x 1 root root 36432 Oct 1 16:11 libboost_chrono.so.1.65.0
-rw-r--r-- 1 root root 166074 Oct 1 16:11 libboost_container.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_container.so -> libboost_container.so.1.65.0
-rwxr-xr-x 1 root root 118224 Oct 1 16:11 libboost_container.so.1.65.0
-rw-r--r-- 1 root root 74878 Oct 1 16:11 libboost_context.a
lrwxrwxrwx 1 root root 26 Oct 1 16:11 libboost_context.so -> libboost_context.so.1.65.0
-rwxr-xr-x 1 root root 55336 Oct 1 16:11 libboost_context.so.1.65.0
-rw-r--r-- 1 root root 97300 Oct 1 16:11 libboost_coroutine.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_coroutine.so -> libboost_coroutine.so.1.65.0
-rwxr-xr-x 1 root root 62192 Oct 1 16:11 libboost_coroutine.so.1.65.0
-rw-r--r-- 1 root root 148634 Oct 1 16:11 libboost_date_time.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_date_time.so -> libboost_date_time.so.1.65.0
-rwxr-xr-x 1 root root 94416 Oct 1 16:11 libboost_date_time.so.1.65.0
-rw-r--r-- 1 root root 1662 Oct 1 16:11 libboost_exception.a
-rw-r--r-- 1 root root 245588 Oct 1 16:11 libboost_filesystem.a
lrwxrwxrwx 1 root root 29 Oct 1 16:11 libboost_filesystem.so -> libboost_filesystem.so.1.65.0
-rwxr-xr-x 1 root root 127128 Oct 1 16:11 libboost_filesystem.so.1.65.0
-rw-r--r-- 1 root root 773386 Oct 1 16:11 libboost_graph.a
lrwxrwxrwx 1 root root 24 Oct 1 16:11 libboost_graph.so -> libboost_graph.so.1.65.0
-rwxr-xr-x 1 root root 450264 Oct 1 16:11 libboost_graph.so.1.65.0
-rw-r--r-- 1 root root 340650 Oct 1 16:11 libboost_iostreams.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_iostreams.so -> libboost_iostreams.so.1.65.0
-rwxr-xr-x 1 root root 152304 Oct 1 16:11 libboost_iostreams.so.1.65.0
-rw-r--r-- 1 root root 2899498 Oct 1 16:11 libboost_locale.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_locale.so -> libboost_locale.so.1.65.0
-rwxr-xr-x 1 root root 1161984 Oct 1 16:11 libboost_locale.so.1.65.0
-rw-r--r-- 1 root root 3029602 Oct 1 16:11 libboost_log.a
-rw-r--r-- 1 root root 2435950 Oct 1 16:11 libboost_log_setup.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_log_setup.so -> libboost_log_setup.so.1.65.0
-rwxr-xr-x 1 root root 1107160 Oct 1 16:11 libboost_log_setup.so.1.65.0
lrwxrwxrwx 1 root root 22 Oct 1 16:11 libboost_log.so -> libboost_log.so.1.65.0
-rwxr-xr-x 1 root root 1146984 Oct 1 16:11 libboost_log.so.1.65.0
-rw-r--r-- 1 root root 659100 Oct 1 16:11 libboost_math_c99.a
-rw-r--r-- 1 root root 553156 Oct 1 16:11 libboost_math_c99f.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_math_c99f.so -> libboost_math_c99f.so.1.65.0
-rwxr-xr-x 1 root root 83384 Oct 1 16:11 libboost_math_c99f.so.1.65.0
-rw-r--r-- 1 root root 578542 Oct 1 16:11 libboost_math_c99l.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_math_c99l.so -> libboost_math_c99l.so.1.65.0
-rwxr-xr-x 1 root root 83824 Oct 1 16:11 libboost_math_c99l.so.1.65.0
lrwxrwxrwx 1 root root 27 Oct 1 16:11 libboost_math_c99.so -> libboost_math_c99.so.1.65.0
-rwxr-xr-x 1 root root 103384 Oct 1 16:11 libboost_math_c99.so.1.65.0
-rw-r--r-- 1 root root 2933306 Oct 1 16:11 libboost_math_tr1.a
-rw-r--r-- 1 root root 2923974 Oct 1 16:11 libboost_math_tr1f.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_math_tr1f.so -> libboost_math_tr1f.so.1.65.0
-rwxr-xr-x 1 root root 456008 Oct 1 16:11 libboost_math_tr1f.so.1.65.0
-rw-r--r-- 1 root root 2901944 Oct 1 16:11 libboost_math_tr1l.a
lrwxrwxrwx 1 root root 28 Oct 1 16:11 libboost_math_tr1l.so -> libboost_math_tr1l.so.1.65.0
-rwxr-xr-x 1 root root 414672 Oct 1 16:11 libboost_math_tr1l.so.1.65.0
lrwxrwxrwx 1 root root 27 Oct 1 16:11 libboost_math_tr1.so -> libboost_math_tr1.so.1.65.0
-rwxr-xr-x 1 root root 428872 Oct 1 16:11 libboost_math_tr1.so.1.65.0
-rw-r--r-- 1 root root 167818 Oct 1 16:11 libboost_prg_exec_monitor.a
lrwxrwxrwx 1 root root 35 Oct 1 16:11 libboost_prg_exec_monitor.so -> libboost_prg_exec_monitor.so.1.65.0
-rwxr-xr-x 1 root root 94432 Oct 1 16:11 libboost_prg_exec_monitor.so.1.65.0
-rw-r--r-- 1 root root 1320724 Oct 1 16:11 libboost_program_options.a
lrwxrwxrwx 1 root root 34 Oct 1 16:11 libboost_program_options.so -> libboost_program_options.so.1.65.0
-rwxr-xr-x 1 root root 591024 Oct 1 16:11 libboost_program_options.so.1.65.0
-rw-r--r-- 1 root root 612306 Oct 1 16:11 libboost_python.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_python.so -> libboost_python.so.1.65.0
-rwxr-xr-x 1 root root 369992 Oct 1 16:11 libboost_python.so.1.65.0
-rw-r--r-- 1 root root 39266 Oct 1 16:11 libboost_random.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_random.so -> libboost_random.so.1.65.0
-rwxr-xr-x 1 root root 31776 Oct 1 16:11 libboost_random.so.1.65.0
-rw-r--r-- 1 root root 2961168 Oct 1 16:11 libboost_regex.a
lrwxrwxrwx 1 root root 24 Oct 1 16:11 libboost_regex.so -> libboost_regex.so.1.65.0
-rwxr-xr-x 1 root root 1420512 Oct 1 16:11 libboost_regex.so.1.65.0
-rw-r--r-- 1 root root 1114070 Oct 1 16:11 libboost_serialization.a
lrwxrwxrwx 1 root root 32 Oct 1 16:11 libboost_serialization.so -> libboost_serialization.so.1.65.0
-rwxr-xr-x 1 root root 440448 Oct 1 16:11 libboost_serialization.so.1.65.0
-rw-r--r-- 1 root root 208580 Oct 1 16:11 libboost_signals.a
lrwxrwxrwx 1 root root 26 Oct 1 16:11 libboost_signals.so -> libboost_signals.so.1.65.0
-rwxr-xr-x 1 root root 119336 Oct 1 16:11 libboost_signals.so.1.65.0
-rw-r--r-- 1 root root 59616 Oct 1 16:11 libboost_stacktrace_addr2line.a
lrwxrwxrwx 1 root root 39 Oct 1 16:11 libboost_stacktrace_addr2line.so -> libboost_stacktrace_addr2line.so.1.65.0
-rwxr-xr-x 1 root root 49224 Oct 1 16:11 libboost_stacktrace_addr2line.so.1.65.0
-rw-r--r-- 1 root root 39630 Oct 1 16:11 libboost_stacktrace_basic.a
lrwxrwxrwx 1 root root 35 Oct 1 16:11 libboost_stacktrace_basic.so -> libboost_stacktrace_basic.so.1.65.0
-rwxr-xr-x 1 root root 35824 Oct 1 16:11 libboost_stacktrace_basic.so.1.65.0
-rw-r--r-- 1 root root 2946 Oct 1 16:11 libboost_stacktrace_noop.a
lrwxrwxrwx 1 root root 34 Oct 1 16:11 libboost_stacktrace_noop.so -> libboost_stacktrace_noop.so.1.65.0
-rwxr-xr-x 1 root root 8496 Oct 1 16:11 libboost_stacktrace_noop.so.1.65.0
-rw-r--r-- 1 root root 62116 Oct 1 16:11 libboost_system.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_system.so -> libboost_system.so.1.65.0
-rwxr-xr-x 1 root root 20944 Oct 1 16:11 libboost_system.so.1.65.0
-rw-r--r-- 1 root root 2463292 Oct 1 16:11 libboost_test_exec_monitor.a
-rw-r--r-- 1 root root 297182 Oct 1 16:11 libboost_thread.a
lrwxrwxrwx 1 root root 25 Oct 1 16:11 libboost_thread.so -> libboost_thread.so.1.65.0
-rwxr-xr-x 1 root root 187920 Oct 1 16:11 libboost_thread.so.1.65.0
-rw-r--r-- 1 root root 22672 Oct 1 16:11 libboost_timer.a
lrwxrwxrwx 1 root root 24 Oct 1 16:11 libboost_timer.so -> libboost_timer.so.1.65.0
-rwxr-xr-x 1 root root 24960 Oct 1 16:11 libboost_timer.so.1.65.0
-rw-r--r-- 1 root root 112536 Oct 1 16:11 libboost_type_erasure.a
lrwxrwxrwx 1 root root 31 Oct 1 16:11 libboost_type_erasure.so -> libboost_type_erasure.so.1.65.0
-rwxr-xr-x 1 root root 73568 Oct 1 16:11 libboost_type_erasure.so.1.65.0
-rw-r--r-- 1 root root 2433124 Oct 1 16:11 libboost_unit_test_framework.a
lrwxrwxrwx 1 root root 38 Oct 1 16:11 libboost_unit_test_framework.so -> libboost_unit_test_framework.so.1.65.0
-rwxr-xr-x 1 root root 1052944 Oct 1 16:11 libboost_unit_test_framework.so.1.65.0
-rw-r--r-- 1 root root 3857126 Oct 1 16:11 libboost_wave.a
lrwxrwxrwx 1 root root 23 Oct 1 16:11 libboost_wave.so -> libboost_wave.so.1.65.0
-rwxr-xr-x 1 root root 1879896 Oct 1 16:11 libboost_wave.so.1.65.0
-rw-r--r-- 1 root root 752452 Oct 1 16:11 libboost_wserialization.a
lrwxrwxrwx 1 root root 33 Oct 1 16:11 libboost_wserialization.so -> libboost_wserialization.so.1.65.0
-rwxr-xr-x 1 root root 312616 Oct 1 16:11 libboost_wserialization.so.1.65.0

posted @ 2023-10-01 23:34  zhjh256  阅读(137)  评论(0编辑  收藏  举报