Installation and use of muduo Library

Today, we started Chen Shuo Da's journey of linux multithreaded server programming to learn the muduo network library. Here we mainly record the installation of muduo in the ubuntu subsystem of WSL under win11.
Firstly, since muduo uses cmake as the build system, cmke must be installed first. In addition, since muduo depends on Boost (the core library depends on TR1), Boost is also installed.

sudo apt-get install cmake
sudo apt-get install libboost-dev libboost-test-dev

Then start to install muduo. According to the book, muduo was originally stored on google's code hosting platform. In fact, it has been transferred to github, so you only need to use git to make a customer visit (installing Git is actually very simple, you can know it by Baidu, and we won't elaborate here)

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

If the clone command of git does not specify a path, it will take the current directory as the destination path, so I switched to another path. Due to the need of permission, I use sudo to temporarily increase the permission. After that, I can see the muduo directory under the current directory. Enter the muduo directory and enter/ build.sh starts building, but it shows ln -sf B U I L D D I R / BUILD_DIR/ BUILDD​IR/BUILD_TYPE-cpp11/compile_commands.json and mkdir execution was rejected, i.e. permission denied. This gave me a headache, so I had to build Modify the command of the SH script, and add sudo prefix to these two commands and the subsequent cmake command and make command, so that there will be no problem in execution.
As follows:

#!/bin/sh

set -x

SOURCE_DIR=`pwd`
BUILD_DIR=${BUILD_DIR:-../build}
BUILD_TYPE=${BUILD_TYPE:-release}
INSTALL_DIR=${INSTALL_DIR:-../${BUILD_TYPE}-install-cpp11}
CXX=${CXX:-g++}

sudo ln -sf $BUILD_DIR/$BUILD_TYPE-cpp11/compile_commands.json

sudo mkdir -p $BUILD_DIR/$BUILD_TYPE-cpp11 \
  && cd $BUILD_DIR/$BUILD_TYPE-cpp11 \
  && sudo cmake \
           -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
           -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
           -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
           $SOURCE_DIR \
  && sudo make $*

# Use the following command to run all the unit tests
# at the dir $BUILD_DIR/$BUILD_TYPE :
# CTEST_OUTPUT_ON_FAILURE=TRUE make test

# cd $SOURCE_DIR && doxygen

However/ build. After SH is executed, only the release directory (including bin directory and lib directory, i.e. executable files and static library files) is found in / usr/include/build directory. There are no header files and library files we need. Read back and find that it should be executed/ build. Only the SH install command has the desired release install directory, but cmake reports an error when executing. Why? Because it can't find the dependency of protobuf and curl, oh, I forgot to install it

sudo apt-get install libcurl4-openssl-dev libc-ares-dev
sudo apt-get install protobuf-compiler libprotobuf-dev

In this way, we have basically compiled the header files and library files we need. Let's run it. Switch to the bin directory of release-cpp11 (the build directory we generated, that is, the release directory mentioned earlier, which should be the updated c++11 standard) and run the test program inspector_test

jack@DESKTOP-SJO8SMG:/usr/include/build/release-cpp11/bin$ ./inspector_test
20211210 14:43:57.949897Z  4627 WARN  HttpServer[Inspector:test] starts listening on 0.0.0.0:12345 - HttpServer.cc:53

You should get the above response, and then we can access port 12345 of the local ip in the browser( http://127.0.0.1:12345/ )You should get the following interface

Then our server program responds as follows:

20211210 14:45:09.441062Z  4627 INFO  TcpServer::newConnection [Inspector:test] - new connection [Inspector:test-0.0.0.0:12345#1] from 127.0.0.1:33248 - TcpServer.cc:80
20211210 14:45:09.441768Z  4627 INFO  TcpServer::newConnection [Inspector:test] - new connection [Inspector:test-0.0.0.0:12345#2] from 127.0.0.1:33250 - TcpServer.cc:80
20211210 14:47:20.255819Z  4627 INFO  TcpServer::removeConnectionInLoop [Inspector:test] - connection Inspector:test-0.0.0.0:12345#2 - TcpServer.cc:109

That should be ok.

Keywords: Linux Ubuntu server

Added by bytes on Fri, 10 Dec 2021 16:57:53 +0200