栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

3.C++ boost库和muduo库的编译安装

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3.C++ boost库和muduo库的编译安装

muduo库是基于boost开发的,需要先在Linux平台上安装boost库

linux上boost源码的安装

boost的源码下载地址:https://boostorg.jfrog.io/artifactory/main/release/

下载.tar.gz
tar -xvf boost*
./bootstrap.sh
./b2
./b2 install
window上boost的编译安装

打开vs2019的命令行窗口,切换到下载好的boost源码的位置

./bootstrap.bat
./b2
./b2 install

如图,我电脑的源码安装位置是c盘

在vs中的配置

测试用例
#include 
#include 
#include 
using namespace std;

class A
{
public:
	void func(string name)
	{
		cout << name << " A: hello world!" << endl;
	}
};

int main()
{
	A a;
	auto func = boost::bind(&A::func, &a, "boost");
	func();
	return 0;
}
muduo库的安装

muduo是基于boost开发的,需要先安装boost库,用git clone来在linux上下载源码

git clone https://github.com/chenshuo/muduo.git

muduo库源码编译会编译很多unit_test测试用例代码,编译耗时长,也用不到,在源码目录打开 CMakeLists.txt.

vim CMakeLists.txt

./build.sh
./build.sh install

./build.sh install把muduo的头文件和lib库文件放到了muduo-master同级目录下的build目录下的release-install-cpp11文件夹下

cd release-install-cpp11/ 

配置以下信息,就可以不用在指定头文件和lib库文件路径,g++会自动从/usr/include和/usr/local/lib路径下寻找所需要的文件。

cd include/
mv muduo/ /usr/include/
cd ../lib/
mv * /usr/local/lib/

测试代码
原文链接:https://blog.csdn.net/QIANGWEIYUAN/article/details/89023980

vim test.cpp
#include 
#include 
#include 
#include 

// 使用muduo开发回显服务器
class EchoServer
{
 public:
  EchoServer(muduo::net::EventLoop* loop,
             const muduo::net::InetAddress& listenAddr);

  void start(); 

 private:
  void onConnection(const muduo::net::TcpConnectionPtr& conn);

  void onMessage(const muduo::net::TcpConnectionPtr& conn,
                 muduo::net::Buffer* buf,
                 muduo::Timestamp time);

  muduo::net::TcpServer server_;
};

EchoServer::EchoServer(muduo::net::EventLoop* loop,
                       const muduo::net::InetAddress& listenAddr)
  : server_(loop, listenAddr, "EchoServer")
{
  server_.setConnectionCallback(
      boost::bind(&EchoServer::onConnection, this, _1));
  server_.setMessageCallback(
      boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
}

void EchoServer::start()
{
  server_.start();
}

void EchoServer::onConnection(const muduo::net::TcpConnectionPtr& conn)
{
  LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "
           << conn->localAddress().toIpPort() << " is "
           << (conn->connected() ? "UP" : "DOWN");
}

void EchoServer::onMessage(const muduo::net::TcpConnectionPtr& conn,
                           muduo::net::Buffer* buf,
                           muduo::Timestamp time)
{
  // 接收到所有的消息,然后回显
  muduo::string msg(buf->retrieveAllAsString());
  LOG_INFO << conn->name() << " echo " << msg.size() << " bytes, "
           << "data received at " << time.toString();
  conn->send(msg);
}


int main()
{
  LOG_INFO << "pid = " << getpid();
  muduo::net::EventLoop loop;
  muduo::net::InetAddress listenAddr(8888);
  EchoServer server(&loop, listenAddr);
  server.start();
  loop.loop();
}
g++  test.cpp -lmuduo_net -lmuduo_base -lpthread -std=c++11
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号