Linux多线程服务端编程学习muduo(四)finger服务的实现

例 七步实现finger服务


本文以陈硕老师的 Linux多线程服务端编程 为参考书,展示如何通过陈硕老师的源码编写对应的CMakeLists.txt ,然后编译运行。(在进行本部分的实验前,需要先 进行网络库的安装 参考链接 https://blog.csdn.net/YoungSusie/article/details/90021742 以及 库文件和头文件的设置 参考链接 https://blog.csdn.net/YoungSusie/article/details/90035042
陈硕老师在源代码中,example/twisted/finger 下包含01-07 代码文件 、 一个 CMakeLists.txt 文件 和一个README 文件
在这里插入图片描述
其中,CMakeLists.txt文件的内容
在这里插入图片描述
很明显,这不是一个完整的cmakelists 文件,因此,先修改CMakeLists.txt的内容

  • 建立CMakeLists.txt 文件

cmake_minimum_required(VERSION 2.6)

project(figure C CXX)

enable_testing()

if(NOT MUDUO_PATH)  
    set(MUDUO_PATH "/usr")
endif()

set(CXX_FLAGS
 -g
 # -DVALGRIND
 -DCHECK_PTHREAD_RETURN_VALUE
 -D_FILE_OFFSET_BITS=64
 -Wall
 -Wextra
 -Werror
 -Wconversion
 -Wno-unused-parameter
 -Wold-style-cast
 -Woverloaded-virtual
 -Wpointer-arith
 -Wshadow
 -Wwrite-strings
 -march=native
 # -MMD
 #-std=c++11
 -std=c++0x
 -rdynamic
 )

if(CMAKE_BUILD_BITS EQUAL 32)
  list(APPEND CXX_FLAGS "-m32")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  list(APPEND CXX_FLAGS "-Wno-null-dereference")
  list(APPEND CXX_FLAGS "-Wno-sign-conversion")
  list(APPEND CXX_FLAGS "-Wno-unused-local-typedef")
  list(APPEND CXX_FLAGS "-Wthread-safety")
  list(REMOVE_ITEM CXX_FLAGS "-rdynamic")
endif()
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")

set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)


find_path(Muduo_INCLUDE_DIR muduo "${MUDUO_PATH}/include")
find_path(Muduo_LIBRARY_DIR libmuduo_net.a "${MUDUO_PATH}/lib")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${Muduo_LIBRARY_DIR})
message(STATUS ${Muduo_INCLUDE_DIR})
message(STATUS ${Muduo_LIBRARY_DIR})

include_directories(${Muduo_INCLUDE_DIR})
find_library(muduo_base muduo_base)
find_library(muduo_net muduo_net)
message(STATUS ${muduo_base})
message(STATUS ${muduo_net})

find_package(Boost REQUIRED)
find_package(ZLIB)
find_path(CARES_INCLUDE_DIR ares.h)
find_library(CARES_LIBRARY NAMES cares)
find_path(MHD_INCLUDE_DIR microhttpd.h)
find_library(MHD_LIBRARY NAMES microhttpd)
find_library(BOOSTTEST_LIBRARY NAMES boost_unit_test_framework)
find_library(BOOSTPO_LIBRARY NAMES boost_program_options)
find_library(BOOSTSYSTEM_LIBRARY NAMES boost_system)
find_path(TCMALLOC_INCLUDE_DIR gperftools/heap-profiler.h)
find_library(TCMALLOC_LIBRARY NAMES tcmalloc_and_profiler)
find_path(HIREDIS_INCLUDE_DIR hiredis/hiredis.h)
find_library(HIREDIS_LIBRARY NAMES hiredis)
find_path(GD_INCLUDE_DIR gd.h)
find_library(GD_LIBRARY NAMES gd)
find_program(THRIFT_COMPILER thrift)
find_path(THRIFT_INCLUDE_DIR thrift)
find_library(THRIFT_LIBRARY NAMES thrift)  


include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR})


add_executable(twisted_finger01 finger01.cc)
target_link_libraries(twisted_finger01 ${muduo_net})
target_link_libraries(twisted_finger01 ${muduo_base})   
target_link_libraries(twisted_finger01 pthread rt)

add_executable(twisted_finger02 finger02.cc)
target_link_libraries(twisted_finger02 ${muduo_net})
target_link_libraries(twisted_finger02 ${muduo_base}) 
target_link_libraries(twisted_finger02 pthread rt)

add_executable(twisted_finger03 finger03.cc)
target_link_libraries(twisted_finger03 ${muduo_net})
target_link_libraries(twisted_finger03 ${muduo_base}) 
target_link_libraries(twisted_finger03 pthread rt)

add_executable(twisted_finger04 finger04.cc)
target_link_libraries(twisted_finger04 ${muduo_net})
target_link_libraries(twisted_finger04 ${muduo_base}) 
target_link_libraries(twisted_finger04 pthread rt)

add_executable(twisted_finger05 finger05.cc)
target_link_libraries(twisted_finger05 ${muduo_net})
target_link_libraries(twisted_finger05 ${muduo_base}) 
target_link_libraries(twisted_finger05 pthread rt)

add_executable(twisted_finger06 finger06.cc)
target_link_libraries(twisted_finger06 ${muduo_net})
target_link_libraries(twisted_finger06 ${muduo_base}) 
target_link_libraries(twisted_finger06 pthread rt)

add_executable(twisted_finger07 finger07.cc)
target_link_libraries(twisted_finger07 ${muduo_net})
target_link_libraries(twisted_finger07 ${muduo_base}) 
target_link_libraries(twisted_finger07 pthread rt)
  • 修改源代码中的include部分

将finger 01-07 代码中的include " " 全部改为 include <>
在这里插入图片描述
全部修改完成后,在finger文件夹下打开一个终端

#  mkdir build

#  cd  build

# cmake ..

#  make

在这里插入图片描述

  • 测试

在/build/bin 目录下打开一个终端,运行finger07

#  ./twisted_finger07

打开另外一个终端,运行命令

# telnet localhost 1079

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YoungSusie/article/details/90341506