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

1. 下载

bash 复制代码
https://github.com/chenshuo/muduo

2. 安装依赖库

1. 安装 boost 库

bash 复制代码
sudo apt-get update
sudo apt-get install libboost-all-dev

下面我们测试一下 boost 库

cpp 复制代码
#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;
}

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

bash 复制代码
g++ test.cpp -o test -std=c++11

2. 安装 cmake

bash 复制代码
sudo apt install cmake  -y

3. 编译

bash 复制代码
./build.sh

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

1. 头文件

bash 复制代码
sudo cp muduo/ /usr/include/

2. 库文件

进入到 build 目录下面的 lib 目录,将所有文件拷贝到

bash 复制代码
sudo cp * /usr/local/lib/

5. 测试 muduo 库

创建一个 C++ 文件

cpp 复制代码
#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();
}

代码编译:

bash 复制代码
g++ test_muduo1.cc -o test_muduo1 -lmuduo_net -lmuduo_base -lpthread -std=c++11

测试:

终端1:

bash 复制代码
./test_muduo1 

终端2:

bash 复制代码
echo "hello world" | nc localhost 8888

测试结果如下:

相关推荐
-dzk-2 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
m0_706653233 小时前
C++编译期数组操作
开发语言·c++·算法
嵩山小老虎3 小时前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
qq_423233903 小时前
C++与Python混合编程实战
开发语言·c++·算法
m0_715575343 小时前
分布式任务调度系统
开发语言·c++·算法
Fleshy数模3 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
a41324473 小时前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
CSDN_RTKLIB3 小时前
简化版unique_ptr说明其本质
c++
naruto_lnq3 小时前
泛型编程与STL设计思想
开发语言·c++·算法