muduo CMakeLists.txt注释

cmake_minimum_required(VERSION 2.6) #使用的版本是CMake >=2.6

project(muduo C CXX) # 是C和C++的工程项目

enable_testing() #开启测试

if(NOT CMAKE_BUILD_TYPE) #一般项目的默认写法, 如果没有定义编译版本,就采用Release 版本吧
  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()

#C++ 编译的一些标志, 可参考 [编译选项](https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html)
set(CXX_FLAGS
 -g #用来编译带调试信息的应用程序,方便后续的调试
 # -DVALGRIND
 -DCHECK_PTHREAD_RETURN_VALUE #定义全局宏, 在代码中有使用到
 -D_FILE_OFFSET_BITS=64 #linux下对大文件的操作定义, [参考链接](https://www.cnblogs.com/hime/archive/2012/06/21/2557315.html)
 -Wall #编译尽可能的告警
 -Wextra #一些-Wall没有打开的告警,用extra 在打开
 -Werror #让编译器将任何的告警,当成编译错误处理
 -Wconversion # 隐式转换告警
 -Wno-unused-parameter # 有没有使用的函数或者变量会发出此告警,可用 #define UNUSED(x) (void)(x)
 -Wold-style-cast #使用c式转换告警, 使用c++转化可以消除这样的告警
 -Woverloaded-virtual #为了防止误写冲在的虚函数, 发次警告, 如果式真的需要冲在虚函数,可以去除此警告
 -Wpointer-arith # 在void 类型的指针上做加或减操作,使用该扩展的话会警告,[参考](https://blog.csdn.net/xiangguiwang/article/details/81839615)
 -Wshadow #简单的说就是变量不同作用域中定义了相同的变量, 类, enum等, 会形成shadow
 -Wwrite-strings # 将常量字符串复制到非常量字符串, 主要是const char* 拷贝到 const char*
 -march=native #告诉编译器调整生成的代码以用于主机CPU的微架构和ISA扩展
 # -MMD
 -std=c++11  #用c++标准11, 这个是老的写法, 新的写法如下: 
                    #set(CMAKE_CXX_STANDARD 11)
                    # set(CMAKE_CXX_STANDARD_REQUIRED ON)
 -rdynamic #用来通知链接器将所有符号添加到动态符号表

 )
if(CMAKE_BUILD_BITS EQUAL 32) #针对32位机器
  list(APPEND CXX_FLAGS "-m32")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # 针对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}") #将CXX_FLAGS中如果有“;”,替换成“ ”, 赋值给CMAKE_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()

#[编译选项参考](https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Invoking-GCC.html#Invoking-GCC)

你可能感兴趣的:(muduo CMakeLists.txt注释)