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
boost基础环境搭建的更多相关文章
- Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建
[注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.Spark编译与部署将以CentOS 64位操作系统为基础,主要是考虑到实际应用 ...
- EXT 基础环境搭建
EXT 基础环境搭建使用 Sencha CMD 下载地址 https://www.sencha.com/products/extjs/cmd-download/ Sencha CMD 常用命令 API ...
- IOS开发基础环境搭建
一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包: ...
- Spark环境搭建(上)——基础环境搭建
Spark摘说 Spark的环境搭建涉及三个部分,一是linux系统基础环境搭建,二是Hadoop集群安装,三是Spark集群安装.在这里,主要介绍Spark在Centos系统上的准备工作--linu ...
- 【1】windows下IOS开发基础环境搭建
一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包: ...
- Maven 学习笔记(一) 基础环境搭建
在Java的世界里,项目的管理与构建,有两大常用工具,一个是Maven,另一个是Gradle,当然,还有一个正在淡出的Ant.Maven 和 Gradle 都是非常出色的工具,排除个人喜好,用哪个工具 ...
- (一)Hyperledger Fabric 1.1安装部署-基础环境搭建
在学习和开发hyperledger fabric的时候遇到了一些坑,现将自己的一些总结和心得整理如下,以期对大家有所帮助.本次使用的宿主机环境:ubuntu,版本:Ubuntu 16.04.3 LTS ...
- hadoop3.1.0 window win7 基础环境搭建
https://blog.csdn.net/wsh596823919/article/details/80774805 hadoop3.1.0 window win7 基础环境搭建 前言:在windo ...
- HyperLedger Fabric 1.4 基础环境搭建(7)
学习了前面几章理论知识后,本章开始介绍实践操作,先介绍Fabric基础环境搭建,采用的操作系统为Centos 7 64位,依次介绍Docker安装.Docker-Compose安装.GO语言环境安装. ...
随机推荐
- 洛谷 P1197 [JSOI2008]星球大战
题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道 ...
- Tensorflow图像操作
图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...
- leetcode557
public class Solution { public string ReverseWords(string s) { var list = s.Split(' ').AsEnumerable( ...
- leetcode326
public class Solution { public bool IsPowerOfThree(int n) { && ( % n == ); } } https://leetc ...
- WebRTC内置debug工具,详细参数解读 chrome://webrtc-internals/
为了确保这篇文章所写内容尽可能的准确,我决定请来Philipp Hancke来作为此篇文章的共同作者. 当你想要找到你WebRTC产品中的问题时,webrtc-internals是一个非常棒的工具,因 ...
- SecureCRT结合xmanager远程启动图形化界面程序
我们很多操作都是可以通过命令行的形式来完成,但是由于不支持图形模式,在一些需要图形界面的时候就很麻烦.本次设置可以在secureCRT中直接使用netca\dbca命令,通过xmanager的pass ...
- cacti客户端snmp设置
1. ubuntu : apt-get install snmp snmpd vim /etc/default/snmpd //将此配置文件中127.0.0.1 删掉. /etc/init.d/sn ...
- Linux 的伪终端的基本原理 及其在远程登录(SSH,telnet等)中的应用
本文介绍了linux中伪终端的创建,介绍了终端的回显.行缓存.控制字符等特性,并在此基础上解释和模拟了telnet.SSH开启远程会话的过程. 一.轻量级远程登录 之前制作的一块嵌入式板子,安装了嵌入 ...
- Eclipse中Ctrl+Shift+f快捷键无效的解决方式
某天突然发现idea非常重要的快捷键ctrl+shift+f无效了,网上搜了很多都说是qq快捷键冲突,但是找了下qq快捷键却没有解决,现在给大家一个解决快捷键冲突的思路: 1.查看QQ快捷键--> ...
- Haskell语言学习笔记(33)Exception, Except, ExceptT
Exception class (Typeable e, Show e) => Exception e where toException :: e -> SomeException fr ...