1. Linux C++ muduo 库学习——库的编译安装

1. 下载

https://github.com/chenshuo/muduo

2. 安装依赖库

1. 安装 boost 库

sudo apt-get update
sudo apt-get install libboost-all-dev

下面我们测试一下 boost 库


#define BOOST_BIND_GLOBAL_PLACEHOLDERS

#include <iostream>
#include <boost/bind.hpp>
#include <string>
using namespace std;

class Hello
{
public:
	void say(string name) 
	{ cout << name << " talk to America.!" << endl; }
};

int main()
{
	Hello h;
	auto func = boost::bind(&Hello::say, &h, "Li si");
	func();
	return 0;
}

代码写好了,开始编译一下:

g++ test.cpp -o test -std=c++11

2. 安装 cmake

sudo apt install cmake  -y

3. 编译

./build.sh

4. 手动安装头文件和 .a (默认静态编译)

1. 头文件

在这里插入图片描述

sudo cp muduo/ /usr/include/

2. 库文件

进入到 build 目录下面的 lib 目录,将所有文件拷贝到
在这里插入图片描述

sudo cp * /usr/local/lib/

5. 测试 muduo 库

创建一个 C++ 文件

#include <muduo/net/TcpServer.h>
#include <muduo/base/Logging.h>
#include <boost/bind.hpp>
#include <muduo/net/EventLoop.h>

// 使用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_muduo1.cc -o test_muduo1 -lmuduo_net -lmuduo_base -lpthread -std=c++11

测试:
终端1:

./test_muduo1 

终端2:

echo "hello world" | nc localhost 8888

测试结果如下:
在这里插入图片描述

6. 生成的自带测试执行文件

muduo 自身也有测试文件,编译后产物地址是:muduo/build/release-cpp11/bin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值