因为现在手上的老的基类库经常出现丢包,以及从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

boost基础环境搭建的更多相关文章

  1. Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建

    [注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.Spark编译与部署将以CentOS 64位操作系统为基础,主要是考虑到实际应用 ...

  2. EXT 基础环境搭建

    EXT 基础环境搭建使用 Sencha CMD 下载地址 https://www.sencha.com/products/extjs/cmd-download/ Sencha CMD 常用命令 API ...

  3. IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  4. Spark环境搭建(上)——基础环境搭建

    Spark摘说 Spark的环境搭建涉及三个部分,一是linux系统基础环境搭建,二是Hadoop集群安装,三是Spark集群安装.在这里,主要介绍Spark在Centos系统上的准备工作--linu ...

  5. 【1】windows下IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  6. Maven 学习笔记(一) 基础环境搭建

    在Java的世界里,项目的管理与构建,有两大常用工具,一个是Maven,另一个是Gradle,当然,还有一个正在淡出的Ant.Maven 和 Gradle 都是非常出色的工具,排除个人喜好,用哪个工具 ...

  7. (一)Hyperledger Fabric 1.1安装部署-基础环境搭建

    在学习和开发hyperledger fabric的时候遇到了一些坑,现将自己的一些总结和心得整理如下,以期对大家有所帮助.本次使用的宿主机环境:ubuntu,版本:Ubuntu 16.04.3 LTS ...

  8. hadoop3.1.0 window win7 基础环境搭建

    https://blog.csdn.net/wsh596823919/article/details/80774805 hadoop3.1.0 window win7 基础环境搭建 前言:在windo ...

  9. HyperLedger Fabric 1.4 基础环境搭建(7)

    学习了前面几章理论知识后,本章开始介绍实践操作,先介绍Fabric基础环境搭建,采用的操作系统为Centos 7 64位,依次介绍Docker安装.Docker-Compose安装.GO语言环境安装. ...

随机推荐

  1. webpack(4)--module

    Module module的配置如何处理模块. 配置Loader rules 配置模块的读取和解析规则, 通常用来配置loader, 其类型是一个数组, 数组里每一项都描述了如何去处理部分文件. 配置 ...

  2. 什么是 web 开发

    什么是 web 开发     这几天因为工作需要,了解了一下Web development 的技术路线,来源自     en.wikipedia.org/wiki/Web_development    ...

  3. IPv4检验和计算

    IP分组中的检验和仅覆盖首部,而不管数据,首部被划分为16位的段,把所有段相加,结果取反,塞进首部检验和里 在目的主机中,首部划分为16位,相加,结果肯定是16个1,然后取反,结果为0.如下 在目的主 ...

  4. 网络层-IP地址

    以下内容是IPv4 IP地址长度32位,Java里面一个int的长度,总共分为5类IP地址 1:分类编址 A类IP地址0开头:           A类有31个位置可以变化,总数是2^31个, [(0 ...

  5. 9 python 多态与多态类

    1.多态定义 多态指的是一类事物的多种形态 比如动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): @abc.abstractm ...

  6. UI5-文档-4.5-Controllers

    在这个步骤中,我们将文本替换为一个按钮,并在按钮被按下时显示“Hello World”消息.按钮的按下事件的处理是在视图的控制器中实现的. Preview A Say Hello button is ...

  7. webserive学习记录2-cxf框架基础使用

    cxf是一个webservice的框架,类似的还有axis,下面说一下cxf的基本使用. 首先要下载cxf的文件,然后要在项目中引入jar包,当然也可以通过maven进行管理.我用的是最新的3.2.1 ...

  8. 代码报错记录-MAVEN-2

    报错: 编译错误,程序包org.junit找不到 原因: 这个是父项目,报错是在子项目中,子项目使用了父项目的junit包,由于scope是test,导致子项目在编译时找不到junit, 修改: 将父 ...

  9. Haskell语言学习笔记(60)Biapplicative

    Biapplicative class Bifunctor p => Biapplicative p where bipure :: a -> b -> p a b (<< ...

  10. CGBitmapContextCreate函数参数详解 以及在 ios7下变化

    函数原型: CGContextRef CGBitmapContextCreate ( void *data,    size_t width,    size_t height,    size_t ...