热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CentOS7.8下编译muduo库找不到Boost库报错的解决方法

本文介绍了在CentOS7.8下编译muduo库时出现找不到Boost库报错的问题,并提供了解决方法。文章详细介绍了从Github上下载muduo和muduo-tutorial源代码的步骤,并指导如何编译muduo库。最后,作者提供了陈硕老师的Github链接和muduo库的简介。

很早之前在CentOS7下编译安装过陈硕的muduo库并且成功运行示例程序muduo-tutorial,但是今天从github上面下载muduo源代码确报错了,提示Boost库找不到,但是我明明安装过Boost1.69.0的
陈硕老师的Github为:https://github.com/chenshuo
muduo_chenshuo


一、从Github上 下载muduo和muduo-tutorial源代码


下载muduo源代码

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

muduo01


下载muduo-tutorial示例程序

git clone https://github.com/chenshuo/muduo-tutorial.git

muduo02


二、编译muduo库

进入到刚才下载的muduo目录,比如我的腾讯云主机上muduo的下载目录为:/root/GithubProjects

muduo03
按照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
centOS7.8
在muduo源代码所在目录执行build.sh脚本报错,错误信息如下图所示:
Could not find Boost
从错误信息可以看出找不到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
libs
命名自己将Boost1.69.0安装到/usr/local/boost目录下了,为什么muduo库找不到呢?很是纳闷,估计是/usr/local/boost目录不是默认的安装目录所以会导致这个问题吧,最后找到了如下两篇博客:



  • Linux下使用CMake进行编译的时候寻找Boost库

  • cmake使用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
FindBoost

根据简书上的这篇博客Linux下使用CMake进行编译的时候寻找Boost库

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库需要一些时间:
build_muduo01

build_muduo_final
从上图可以看出,我们成功编译了muduo源代码。
release-cpp11
可以看到在/root/GithubProjects/build目录下生成了release-cpp11目录,里面有如下目录和文件,包括可执行程序、lib、examples、muduo目录:
dirs


三、使用cmake编译安装muduo库

cd /root/GithubProjects/muduo
mkdir build
cd build
cmake ..
make
make install

muduo_install
可以看到执行make install后,头文件被拷贝到了/usr/local/muduo目录下,lib库文件拷贝到了/usr/local/lib目录下。


四、使用编译muduo-tutorial

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库的路径需要根据自己编译后的路径为准。


推荐阅读
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Ubuntu 16.04 LTS上配置Qt Creator开发环境
    本文详细介绍了如何在Ubuntu 16.04 LTS系统中安装和配置Qt Creator,涵盖了从下载到安装的全过程,并提供了常见问题的解决方案。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 本文详细介绍了Linux系统中init进程的作用及其启动过程,解释了运行级别的概念,并提供了调整服务启动顺序的具体步骤和实例。通过了解这些内容,用户可以更好地管理系统的启动流程和服务配置。 ... [详细]
  • 在成功安装和测试MySQL及Apache之后,接下来的步骤是安装PHP。为了确保安全性和配置的一致性,建议在安装PHP前先停止MySQL和Apache服务,并将MySQL集成到PHP中。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文深入探讨了Linux系统中网卡绑定(bonding)的七种工作模式。网卡绑定技术通过将多个物理网卡组合成一个逻辑网卡,实现网络冗余、带宽聚合和负载均衡,在生产环境中广泛应用。文章详细介绍了每种模式的特点、适用场景及配置方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 解决微信电脑版无法刷朋友圈问题:使用安卓远程投屏方案
    在工作期间想要浏览微信和朋友圈却不太方便?虽然微信电脑版目前不支持直接刷朋友圈,但通过远程投屏技术,可以轻松实现在电脑上操作安卓设备的功能。 ... [详细]
  • 本文详细记录了在银河麒麟操作系统和龙芯架构上使用 Qt 5.15.2 进行项目打包时遇到的问题及解决方案,特别关注于 linuxdeployqt 工具的应用。 ... [详细]
  • 本文详细介绍了如何在 Windows 环境下使用 node-gyp 工具进行 Node.js 本地扩展的编译和配置,涵盖从环境搭建到代码实现的全过程。 ... [详细]
  • 阿里云ecs怎么配置php环境,阿里云ecs配置选择 ... [详细]
author-avatar
canku
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有