之前粗略看过一段时间的nginx源码,对基于C实现的web server有了初步的理解。现在结合muduo网络库来学习下C++的web server。
一.环境搭建
首先是muduo网络库的安装和环境搭建:
可参见http://blog.youkuaiyun.com/Solstice/article/details/5848547
二.echo程序的编译运行
muduo网络库是给出了很多的例子,我首先是阅读了最简单的echo的源码,然后自己编写makefile来生成可执行文件。
echo的源码路径:
/root/unix/muduo-master/examples/simple/echo
makefile的编写:
lib_path = -L/root/unix/build/release-install/lib/
header_path = -I/root/unix/build/release-install/include/
LIBS=-lmuduo_net -lmuduo_base -lpthread
CFLAGS=$(lib_path) $(header_path)
CC=g++
echo_server: main.o echo.o
$(CC) $(CFLAGS) -o echo_server main.o echo.o $(LIBS)
main.o:main.cc
$(CC) $(CFLAGS) -c main.cc
echo.o: echo.cc echo.h
$(CC) $(CFLAGS) -c echo.cc
把makefile文件放在/echo文件夹里面,在命令行输入make,即可得到可执行文件echo_server。然后运行echo_server