通过镜像源去安装开源库(最快的方式)
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
# 安装boost库
# 先安装boost库需要的东西
sudo yum install gcc gcc-c++ python python-devel libicu libicu-devel zlib zlib-devel
sudo yum install boost
sudo yum install boost-devel
sudo yum install boost-doc
# 安装protobuf curl zlib依赖库
sudo yum install protobuf protobuf-devel
sudo yum install curl curl-devel
sudo yum install zlib zlib-devel
# 安装完成后,可以通过以下命令检查库是否已正确安装:
sudo yum install pkgconfig
pkg-config --list-all | grep protobuf
pkg-config --list-all | grep libcurl
pkg-config --list-all | grep zlib
# 如果这些库出现在输出中,说明它们已经被系统识别。
//boost库测试程序
//b.cpp
//g++ b.cpp -g -o b -lboost_thread
#include <boost/thread/thread.hpp>
#include <iostream>
#include <cstdlib>
using namespace std;
volatile bool isRuning = true;
void func1(){
static int cnt1 = 0;
while(isRuning){
cout << "func1:" << cnt1++ << endl;
sleep(1);
}
}
void func2(){
static int cnt2 = 0;
while(isRuning){
cout << "\tfunc2:" << cnt2++ << endl;
sleep(2);
}
}
int main()
{
boost::thread thread1(&func1);
boost::thread thread2(&func2);
system("read");
isRuning = false;
thread2.join();
thread1.join();
cout << "exit" << endl;
return 0;
}
准备工作结束啦~
# 找个风水宝地放muduo库
git clone https://github.com/chenshuo/muduo.git
sudo yum install vim
vim muduo/CMakeLists.txt
# 把 -Werror注释掉就好了
# 建议也把 option(MUDUO_BUILD_EXAMPLES "Build Muduo examples" ON) 这行也注释掉,例子编译太耗时间
cd muduo
./build
./build install
cd ../build
mv build/release-install-cpp11/include/muduo /usr/include
mv build/release-install-cpp11/lib/* /usr/lib
muduo库编译链接完成!
//echoserver.cpp
//g++ -o echoserver ./echoserver.cpp -lmuduo_net -lmuduo_base -lpthread -std=c++11
//用muduo库源码 写个echoserver
#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();
}
./echoserver #运行echoserver服务器
echo "hello world" | nc localhost 8888
hello world # 模拟客户端请求
到此,muduo安装成功。
使用gdb调试echoserver,观察muduo库运行过程
gdb echoserver
start
s
.....
.....