zoukankan      html  css  js  c++  java
  • boost基础环境搭建

    因为现在手上的老的基类库经常出现丢包,以及从ServiceAClient 发送消息到 ServiceBServer时出现消息失败的情况,以及现有的莫名其妙的内存泄露的问题,以及目前还是c++0x,准确地说,是c with class的写法,而且支持windows server。

    所以,现在想拥抱c++11,并解决上述问题。

    选择Boost.Asio,一样是随便找了个库,有网友说库写的不错,就拿来用了,内部原理没看,只知道跨平台,支持c++11.

    1.下载boost软件包
    https://www.boost.org/users/download/

    到下载地址里下载了1.68.0

    然后放到某目录下,tar -zxvf boost1.68.0.tar.gz

    2.安装前配置
    现有环境,centos 64bit 6.10
    进入boost目录后,运行./bootstrap.sh

    3.编译
    执行bootstrap.sh后,会在当前目录下生成b2文件
    在这里执行: ./b2进行编译

    4.安装
    sudo ./b2 install

    不加sudo会报权限错误等问题。

    5.测试
    编写代码

    
    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    int main()
    {
        boost::asio::io_service io;
        boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
        t.wait();
        std::cout << "Hello, world!" << std::endl;
    
        return 0;
    }
    
    

    代码解释:
    Timer.1 - Using a timer synchronously
    Source listing for Timer.1
    This tutorial program introduces asio by showing how to perform a blocking wait on a timer.

    We start by including the necessary header files.

    All of the asio classes can be used by simply including the "asio.hpp" header file.

    #include <iostream>
    #include <boost/asio.hpp>
    

    Since this example uses timers, we need to include the appropriate Boost.Date_Time header file for manipulating times.

    #include <boost/date_time/posix_time/posix_time.hpp>
    

    All programs that use asio need to have at least one io_service object. This class provides access to I/O functionality. We declare an object of this type first thing in the main function.

    int main()
    {
      boost::asio::io_service io;
    

    Next we declare an object of type boost::asio::deadline_timer. The core asio classes that provide I/O functionality (or as in this case timer functionality) always take a reference to an io_service as their first constructor argument. The second argument to the constructor sets the timer to expire 5 seconds from now.

      boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    

    In this simple example we perform a blocking wait on the timer. That is, the call to deadline_timer::wait() will not return until the timer has expired, 5 seconds after it was created (i.e. not from when the wait starts).

    A deadline timer is always in one of two states: "expired" or "not expired". If the deadline_timer::wait() function is called on an expired timer, it will return immediately.

      t.wait();
    

    Finally we print the obligatory "Hello, world!" message to show when the timer has expired.

      std::cout << "Hello, world!" << std::endl;
    
      return 0;
    }
    

    编译运行:
    g++ -Wall -std=c++14 test_xiaoliuzi.cpp -o xtest_xiaoliuzi -lboost_system -lboost_date_time

    需要链接动态链接库:
    如果不链接动态链接库,编译会报错:
    /tmp/ccRacuCr.o: In function boost::system::system_category()': test_xiaoliuzi.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x1): undefined reference to boost::system::detail::system_category_instance'
    /tmp/ccRacuCr.o: In function boost::system::generic_category()': test_xiaoliuzi.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x1): undefined reference to boost::system::detail::generic_category_instance'
    /tmp/ccRacuCr.o: In function boost::asio::detail::posix_event::posix_event()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail11posix_eventC2Ev[_ZN5boost4asio6detail11posix_eventC5Ev]+0x3e): undefined reference to pthread_condattr_setclock'
    /tmp/ccRacuCr.o: In function boost::asio::detail::posix_thread::~posix_thread()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to pthread_detach'
    /tmp/ccRacuCr.o: In function boost::asio::detail::posix_thread::join()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to pthread_join'
    /tmp/ccRacuCr.o: In function boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::basic_deadline_timer(boost::asio::io_context&, boost::posix_time::time_duration const&)': test_xiaoliuzi.cpp:(.text._ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEEC2ERNS0_10io_contextERKNS2_13time_durationE[_ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEEC5ERNS0_10io_contextERKNS2_13time_durationE]+0x33): undefined reference to boost::system::detail::system_category_instance'
    /tmp/ccRacuCr.o: In function boost::asio::basic_deadline_timer<boost::posix_time::ptime, boost::asio::time_traits<boost::posix_time::ptime> >::wait()': test_xiaoliuzi.cpp:(.text._ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEE4waitEv[_ZN5boost4asio20basic_deadline_timerINS_10posix_time5ptimeENS0_11time_traitsIS3_EEE4waitEv]+0x18): undefined reference to boost::system::detail::system_category_instance'
    /tmp/ccRacuCr.o: In function boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::destroy(boost::asio::detail::deadline_timer_service<boost::asio::time_traits<boost::posix_time::ptime> >::implementation_type&)': test_xiaoliuzi.cpp:(.text._ZN5boost4asio6detail22deadline_timer_serviceINS0_11time_traitsINS_10posix_time5ptimeEEEE7destroyERNS7_19implementation_typeE[_ZN5boost4asio6detail22deadline_timer_serviceINS0_11time_traitsINS_10posix_time5ptimeEEEE7destroyERNS7_19implementation_typeE]+0x1b): undefined reference to boost::system::detail::system_category_instance'
    collect2: error: ld returned 1 exit status

    如果只链接-lboost_system, 没有链接-lboost_date_time,编译不会报错,但是运行会报错:

    ./xtest_xiaoliuzi: error while loading shared libraries: libboost_system.so.1.68.0: cannot open shared object file: No such file or directory

    用到的辅助命令:
    为了查看当前的开发机器上是否安装过其他版本的boost,需要用到:
    [xiaoliuzi@ boost_1_68_0]$ sudo /sbin/ldconfig -p | grep boost_thread
    libboost_thread.so.1.68.0 (libc6,x86-64) => /usr/local/lib/libboost_thread.so.1.68.0
    libboost_thread.so (libc6,x86-64) => /usr/local/lib/libboost_thread.so
    编译或者运行出错也有可能会是其他版本同时存在导致的。

    Reference:
    https://www.boost.org/users/download/
    https://www.boost.org/doc/libs/1_68_0/more/getting_started/unix-variants.html#install-boost-build
    https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/tutorial.html
    https://blog.csdn.net/big_bit/article/details/51321251

    转载本Blog文章请注明出处,否则,本作者保留追究其法律责任的权利。 本人转载别人或者copy别人的博客内容的部分,会尽量附上原文出处,仅供学习交流之用,如有侵权,联系立删。
  • 相关阅读:
    oracle11G静默安装步骤
    [INS06101] IP address of localhost could not be determined
    linux tar命令使用详解
    centos 安装 Adobe Flash Player
    yum出错:Error: failure: repodata/filelists.xml.gz from googlechrome: [Errno 256] No more mirrors to try.
    sysbench安装与使用
    /usr/libexec/gconfsanitycheck2 退出状态256
    sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
    Oracle11gR2 for Linux 静默安装
    error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or directory
  • 原文地址:https://www.cnblogs.com/drfxiaoliuzi/p/9566507.html
Copyright © 2011-2022 走看看