很早之前在CentOS7下编译安装过陈硕的muduo库并且成功运行示例程序muduo-tutorial,但是今天从github上面下载muduo源代码确报错了,提示Boost库找不到,但是我明明安装过Boost1.69.0的
陈硕老师的Github为:https://github.com/chenshuo
git clone https://github.com/chenshuo/muduo.git
git clone https://github.com/chenshuo/muduo-tutorial.git
进入到刚才下载的muduo目录,比如我的腾讯云主机上muduo的下载目录为:/root/GithubProjects
按照muduo Github官网README中的说法, 只需要编译muduo库根目录下的build.sh可执行文件即可。
Muduo is a multithreaded C++ network library based on
the reactor pattern.http://github.com/chenshuo/muduoCopyright (c) 2010, Shuo Chen. All rights reserved.Use of this source code is governed by a BSD-style
license that can be found in the License file.Requires:Linux kernel version >= 2.6.28.GCC >= 4.7 or Clang >= 3.5Boost (for boost::any only.)Tested on:Debian 7 and aboveUnbuntu 14.04 and aboveCentOS 7 and aboveTo build, run:./build.shSee https://github.com/chenshuo/muduo-tutorial for
how to use muduo in your project.__ __ _| \/ | | || \ / |_ _ __| |_ _ ___| |\/| | | | |/ _` | | | |/ _ \| | | | |_| | (_| | |_| | (_) ||_| |_|\__,_|\__,_|\__,_|\___/
我的腾讯云主机的系统是CentOS7.8
在muduo源代码所在目录执行build.sh脚本报错,错误信息如下图所示:
从错误信息可以看出找不到Boost库的头文件所在路径,于是我查看了一下muduo源代码根目录下的CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.6)project(muduo C CXX)enable_testing()if(NOT CMAKE_BUILD_TYPE)set(CMAKE_BUILD_TYPE "Release")
endif()# only build examples if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL "muduo")option(MUDUO_BUILD_EXAMPLES "Build Muduo examples" ON)
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-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_package(Boost REQUIRED)
find_package(Protobuf)
find_package(CURL)
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)if(CARES_INCLUDE_DIR AND CARES_LIBRARY)message(STATUS "found cares")
endif()
if(CURL_FOUND)message(STATUS "found curl")
endif()
if(PROTOBUF_FOUND)message(STATUS "found protobuf")
endif()
if(TCMALLOC_INCLUDE_DIR AND TCMALLOC_LIBRARY)message(STATUS "found tcmalloc")
endif()
if(ZLIB_FOUND)message(STATUS "found zlib")
endif()
if(HIREDIS_INCLUDE_DIR AND HIREDIS_LIBRARY)message(STATUS "found hiredis")
endif()
if(GD_INCLUDE_DIR AND GD_LIBRARY)message(STATUS "found gd")
endif()
if(THRIFT_COMPILER AND THRIFT_INCLUDE_DIR AND THRIFT_LIBRARY)message(STATUS "found thrift")
endif()include_directories(${Boost_INCLUDE_DIRS})include_directories(${PROJECT_SOURCE_DIR})string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
message(STATUS "CXX_FLAGS = " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${BUILD_TYPE}})add_subdirectory(muduo/base)
add_subdirectory(muduo/net)if(MUDUO_BUILD_EXAMPLES)add_subdirectory(contrib)add_subdirectory(examples)
else()if(CARES_INCLUDE_DIR AND CARES_LIBRARY)add_subdirectory(examples/cdns)endif()
endif()
从上述CMakeLists.txt文件中可以看出,muduo库依赖于Boost、ProtoBuf、CURL、ZLIB等库的。我的腾讯云CentOS7.8系统下已经安装过了Boost1.69和ProtoBuf3.9.1
命名自己将Boost1.69.0安装到/usr/local/boost目录下了,为什么muduo库找不到呢?很是纳闷,估计是/usr/local/boost目录不是默认的安装目录所以会导致这个问题吧,最后找到了如下两篇博客:
cmake_minimum_required(VERSION 2.8.3)
project(boost_test)
find_package(Boost REQUIRED COMPONENTS system thread)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test ${Boost_LIBRARIES})
还有CMake官方提供的FindBoost
根据简书上的这篇博客Linux下使用CMake进行编译的时候寻找Boost库
所以解决办法很明了了,既然muduo库找不到我安装的boost1.69.0的头文件所在目录,那么我修改一下CMakeLists.txt文件,在find_package(Boost REQUIRED)这句查找Boost库之前,手动设置boost根目录、头文件、lib库文件所在目录就好了即:
可以在搜索package之前,通过设置一些变量来帮助boost库的查找
BOOST_ROOT - 首选的Boost安装路径
BOOST_INCLUDEDIR - 首选的头文件搜索路径 e.g. <prefix>/include
BOOST_LIBRARYDIR - 首选的库文件搜索路径 e.g. <prefix>/lib
Boost_NO_SYSTEM_PATHS - 默认是OFF. 如果开启了,则不会搜索用户指定路径之外的路径
所以可以首先设置BOOST_ROOT、BOOST_INCLUDEDIR、BOOST_LIBRARYDIR这几个变量的值,改成自己的Boost库的目录,比如我的Boost1.69.0的安装目录是: /usr/local/boost
set (BOOST_ROOT /usr/local/boost)
set (BOOST_INCLUDEDIR ${BOOST_ROOT}/include)
set (BOOST_LIBRARYDIR ${BOOST_ROOT}/lib)
修改后的CMakeLists.txt文件如下所示:
cmake_minimum_required(VERSION 2.6)project(muduo C CXX)enable_testing()if(NOT CMAKE_BUILD_TYPE)set(CMAKE_BUILD_TYPE "Release")
endif()# only build examples if this is the main project
if(CMAKE_PROJECT_NAME STREQUAL "muduo")option(MUDUO_BUILD_EXAMPLES "Build Muduo examples" ON)
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-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)# 在搜索Boost的package之前,通过设置一些变量来帮助boost库的查找
set (BOOST_ROOT /usr/local/boost)
set (BOOST_INCLUDEDIR ${BOOST_ROOT}/include)
set (BOOST_LIBRARYDIR ${BOOST_ROOT}/lib)# 寻找boost库
find_package(Boost REQUIRED)if (Boost_FOUND)message( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")message( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}.")message( STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
endif()find_package(Protobuf)
find_package(CURL)
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)if(Boost_FOUND)find_library(BOOSTTEST_LIBRARY NAMES boost_unit_test_framework)find_library(BOOSTPO_LIBRARY NAMES boost_program_options)find_library(BOOSTSYSTEM_LIBRARY NAMES boost_system)
endif()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)if(CARES_INCLUDE_DIR AND CARES_LIBRARY)message(STATUS "found cares")
endif()
if(CURL_FOUND)message(STATUS "found curl")
endif()
if(PROTOBUF_FOUND)message(STATUS "found protobuf")
endif()
if(TCMALLOC_INCLUDE_DIR AND TCMALLOC_LIBRARY)message(STATUS "found tcmalloc")
endif()
if(ZLIB_FOUND)message(STATUS "found zlib")
endif()
if(HIREDIS_INCLUDE_DIR AND HIREDIS_LIBRARY)message(STATUS "found hiredis")
endif()
if(GD_INCLUDE_DIR AND GD_LIBRARY)message(STATUS "found gd")
endif()
if(THRIFT_COMPILER AND THRIFT_INCLUDE_DIR AND THRIFT_LIBRARY)message(STATUS "found thrift")
endif()if(Boost_FOUND)include_directories(${Boost_INCLUDE_DIRS})
endif()include_directories(${PROJECT_SOURCE_DIR})string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
message(STATUS "CXX_FLAGS = " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${BUILD_TYPE}})add_subdirectory(muduo/base)
add_subdirectory(muduo/net)if(MUDUO_BUILD_EXAMPLES)add_subdirectory(contrib)add_subdirectory(examples)
else()if(CARES_INCLUDE_DIR AND CARES_LIBRARY)add_subdirectory(examples/cdns)endif()
endif()
修改CMakeLists.txt再运行build.sh脚本,需要注意的是还需要在设置CXX_FLAGS变量时,注释掉如下三个预处理指令:
#-Werror#-Wold-style-cast#-Wshadow
不然可能会在CentOS7下编译muduo报错。CentOS7下编译muduo库需要一些时间:
从上图可以看出,我们成功编译了muduo源代码。
可以看到在/root/GithubProjects/build目录下生成了release-cpp11目录,里面有如下目录和文件,包括可执行程序、lib、examples、muduo目录:
cd /root/GithubProjects/muduo
mkdir build
cd build
cmake ..
make
make install
可以看到执行make install后,头文件被拷贝到了/usr/local/muduo目录下,lib库文件拷贝到了/usr/local/lib目录下。
muduo-tutorial示例程序的README文件描述了如何使用muduo库:
Examples of Muduo network library0) src - source file
1) bazel - build with Bazel
2) cmake-submodule - build with CMake, checking out muduo as a git submodule
3) cmake - build with CMake
2) makefile - build with Makefile1) build with bazel
cd bazel
bazel build -c opt :all2) build with CMake with git submodule
git submodule update --init
mkdir build
cd build
cmake ../cmake-submodule
makeAssuming Muduo is installed in $HOME/build/debug-install3) build with CMake
mkdir build
cd build
cmake ../cmake
make
# echo binary is in ./bin/4) build with GNU make
cd makefile
make
# echo binary is in ./
上面4种方法中,我所熟悉的是第三种和第四种,这里我以cmake第三种为例:
注意:不论使用make还是cmake也好,注意:muduo库的路径需要根据自己编译后的路径为准。