目录结构
.
|-- cmake
| |-- CompilerSettings.cmake
| |-- Options.cmake
| |-- ProjectBoost.cmake
| `-- UseOpenSSL.cmake
|-- CMakeLists.txt
`-- hello-cpp
|-- CMakeLists.txt
`-- main.cpp
根目录cmake
# cmake版本要求
cmake_minimum_required(VERSION 3.7)
# 项目信息
project(hello-cpp
VERSION 1.0.0
LANGUAGES CXX)
# 添加模块目录(CMAKE_CURRENT_SOURCE_DIR表示当前CMakeList.txt的路径)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# 基本设置
include(Options)
include(CompilerSettings)
print_config()
# 下载安装依赖库
include(UseOpenSSL)
include(ProjectBoost)
# 添加子目录
add_subdirectory(hello-cpp)
cmake目录
# 构建类型,支持Debug Release RelWithDebInfo MinSizeRel,默认为RelWithDebInfo
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
# 显示所有配置信息
macro(print_config)
message("")
message("------------------------------------------------------------------------")
message("-- Configuring ${PROJECT_NAME} ${PROJECT_VERSION}${VERSION_SUFFIX}")
message("------------------------------------------------------------------------")
message("-- CMake Cmake version and location ${CMAKE_VERSION} (${CMAKE_COMMAND})")
message("-- Compiler C++ compiler version ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message("-- CMAKE_BUILD_TYPE Build type ${CMAKE_BUILD_TYPE}")
message("-- TARGET_PLATFORM Target platform ${CMAKE_SYSTEM_NAME}")
message("------------------------------------------------------------------------")
message("")
endmacro()
- CompilerSettings.cmake:用于指定编译选项
# 添加根目录到头文件搜索目录
include_directories(BEFORE ${PROJECT_SOURCE_DIR})
# 设置可执行文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 设置编译器选项
if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
# gcc
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.8+. Current is ${CMAKE_CXX_COMPILER_VERSION}.")
endif()
# 各个编译模式下共用的编译选项
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -pthread")
# 各编译模式专有的编译选项
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
else()
message(FATAL_ERROR "Your compiler is not supported.")
endif()
- ProjectBoost.cmake:下载编译boost库
# 导入插件
include(ExternalProject)
include(ProcessorCount)
# 获取cpu核数
ProcessorCount(CORES)
if(CORES EQUAL 0)
set(CORES 1)
endif()
# 配置命令
set(BOOST_BOOTSTRAP_COMMAND ./bootstrap.sh)
# 构建命令
set(BOOST_BUILD_TOOL ./b2)
# 编译选项
set(BOOST_CXXFLAGS "cxxflags=-std=c++11")
# 下载构建安装依赖库
ExternalProject_Add(boost
# 项目根目录
PREFIX ${CMAKE_SOURCE_DIR}/deps
# 下载名
DOWNLOAD_NAME boost_1_74_0.tar.bz2
# 下载链接(支持多源下载)
URL https://dl.bintray.com/boostorg/release/1.74.0/source/boost_1_74_0.tar.bz2
# sha256哈希值校验
URL_HASH SHA256=83bfc1507731a0906e387fc28b7ef5417d591429e51e788417fe9ff025e116b1
# 在源码中构建
BUILD_IN_SOURCE 1
# 配置命令
CONFIGURE_COMMAND ${BOOST_BOOTSTRAP_COMMAND}
# 构建命令
BUILD_COMMAND ${BOOST_BUILD_TOOL} stage
${BOOST_CXXFLAGS}
threading=multi
variant=release
link=static
--layout=system
--with-system
--with-serialization
--with-date_time
--with-regex
-j${CORES}
# 安装命令
INSTALL_COMMAND ""
# 日志记录
LOG_CONFIGURE 1
LOG_BUILD 1
LOG_INSTALL 1
)
# boost库头文件目录和库目录
ExternalProject_Get_Property(boost SOURCE_DIR)
set(BOOST_INCLUDE_DIR ${SOURCE_DIR})
set(BOOST_LIBRARY_PREFIX ${SOURCE_DIR}/stage/lib/${CMAKE_STATIC_LIBRARY_PREFIX})
# Boost::Algorithm库
add_library(Boost::Algorithm INTERFACE IMPORTED GLOBAL)
set_property(TARGET Boost::Algorithm PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
add_dependencies(Boost::Algorithm boost)
# Boost::ASIO库
add_library(Boost::ASIO INTERFACE IMPORTED GLOBAL)
set_property(TARGET Boost::ASIO PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::ASIO PROPERTY INTERFACE_LINK_LIBRARIES Boost::DateTime Boost::Regex Boost::Serialization
Boost::System OpenSSL::SSL)
add_dependencies(Boost::ASIO boost)
# Boost::DateTime库
add_library(Boost::DateTime STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::DateTime PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::DateTime PROPERTY IMPORTED_LOCATION ${BOOST_LIBRARY_PREFIX}boost_date_time${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET Boost::DateTime PROPERTY INTERFACE_LINK_LIBRARIES Boost::Serialization)
add_dependencies(Boost::DateTime boost)
# Boost::Regex库
add_library(Boost::Regex STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Regex PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::Regex PROPERTY IMPORTED_LOCATION ${BOOST_LIBRARY_PREFIX}boost_regex${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(Boost::Regex boost)
# Boost::Serialization库
add_library(Boost::Serialization STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Serialization PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::Serialization PROPERTY IMPORTED_LOCATION ${BOOST_LIBRARY_PREFIX}boost_serialization${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(Boost::Serialization boost)
# Boost::System库
add_library(Boost::System STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::System PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::System PROPERTY IMPORTED_LOCATION ${BOOST_LIBRARY_PREFIX}boost_system${CMAKE_STATIC_LIBRARY_SUFFIX})
add_dependencies(Boost::System boost)
# 取消临时定义
unset(SOURCE_DIR)
- UseOpenSSL.cmake:ASIO种的ssl特性依赖openssl库:
# OpenSSL::SSL库
add_library(OpenSSL::SSL SHARED IMPORTED GLOBAL)
set_property(TARGET OpenSSL::SSL PROPERTY IMPORTED_LOCATION -lssl)
main模块cmake
# 获取源文件和头文件列表
file(GLOB SRCS "*.cpp")
file(GLOB HEADERS "*.h")
# 添加可执行目标
add_executable(hello-cpp ${SRCS} ${HEADERS})
# 添加依赖库
target_link_libraries(hello-cpp PUBLIC Boost::ASIO)