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语言环境安装. ...
随机推荐
- 《Linux内核精髓:精通Linux内核必会的75个绝技》目录
1章 内核入门HACK #1 如何获取Linux内核HACK #2 如何编译Linux内核HACK #3 如何编写内核模块HACK #4 如何使用GitHACK #5 使用checkpatch.pl检 ...
- linux命令--df/ps aux/netstat/hostname/tail
查询文件系统 df -h 查询cpu使用情况 top 进程查看: ps aux | grep haproxy 端口查看: netstat -lntup 主机名查看 hostname 查看文件末尾字符串 ...
- apache跨域图片配置
修改httpd.conf 1 找到 网站目录设置 <Directory "/var/www"> AllowOverride ALL # Allow open acces ...
- HTML5 浏览器接收的常用 content-type
<1> 常见的设置方法 response.setHeader("content-type", 'text/html'); <2> 浏览器接收的常用 cont ...
- 微信公众号自动回复 node
纯属分享记录: app.js var bodyParser = require('body-parser'); require('body-parser-xml')(bodyParser); var ...
- win2008安装IIS
win2008安装IIS http://jingyan.baidu.com/article/fec4bce2398747f2618d8b88.html http://127.0.0.1/ 新建网站,端 ...
- 一个简单例子弄懂什么是javascript函数劫持
javascript函数劫持很简单,一般情况下,只要在目标函数触发之前,重写这个函数即可. 比如,劫持eval函数的代码如下: var _eval=eval; eval=function(x){ if ...
- Python Spider 抓取猫眼电影TOP100
""" 抓取猫眼电影TOP100 """ import re import time import requests from bs4 im ...
- STS或eclipse安装SVN插件
安装sts--SVN插件 简介:sts是与eclipse类似的Java IDE开发工具(不了解的百度) 1.sts菜单栏 help->install New Software 依据大家的版本选择 ...
- xStream转换XML、JSON
一. 简介 xStream可以很容易实现Java对象和xml文档互相转换, 可以修改某个特定的属性和节点名称,xStream提供annotation注解, 可以在JavaBean中完成对xml ...