目录结构
.
|-- cmake
| |-- CompilerSettings.cmake
| |-- Options.cmake
| `-- ProjectJsonCpp.cmake
|-- CMakeLists.txt
|-- libdevcore
| |-- CMakeLists.txt
| |-- Foo.cpp
| `-- Foo.h
`-- main
|-- CMakeLists.txt
`-- main.cpp
根目录cmake
# cmake版本要求
cmake_minimum_required(VERSION 3.7)
# 项目信息
project(hello-cmake
VERSION 1.0.0
LANGUAGES CXX)
# 添加模块目录
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# 基本设置
include(Options)
include(CompilerSettings)
# 下载安装依赖库
include(ProjectJsonCpp)
# 添加子目录
add_subdirectory(libdevcore)
add_subdirectory(main)
cmake目录
- Options.cmake:用于指定编译特性选项,比如是否编译test模块,构建类型等
# 构建类型,支持Debug Release RelWithDebInfo MinSizeRel,默认为Release
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
- CompilerSettings.cmake:用于指定编译选项
# 添加根目录到头文件搜索目录
include_directories(BEFORE ${PROJECT_SOURCE_DIR})
# 设置可执行文件输出路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 设置编译器选项
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# 各个编译模式下共用的编译选项
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 "Your compiler is not supported.")
endif()
- ProjectJsonCpp.cmake:引入jsoncpp库
# 导入插件
include(ExternalProject)
include(GNUInstallDirs)
# 下载安装依赖库
ExternalProject_Add(jsoncpp
# 项目根目录
PREFIX ${CMAKE_SOURCE_DIR}/deps
# 下载名
DOWNLOAD_NAME jsoncpp-1.8.4.tar.gz
# 下载链接(支持多源下载)
URL https://github.com/open-source-parsers/jsoncpp/archive/1.8.4.tar.gz
# sha256哈希值校验
URL_HASH SHA256=c49deac9e0933bcb7044f08516861a2d560988540b23de2ac1ad443b219afdb6
# cmake命令
CMAKE_COMMAND ${CMAKE_COMMAND}
# cmake参数
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DJSONCPP_WITH_TESTS=OFF
-DJSONCPP_WITH_PKGCONFIG_SUPPORT=OFF
-DBUILD_STATIC_LIBS=ON
# 日志记录
LOG_CONFIGURE 1
LOG_BUILD 1
LOG_INSTALL 1
)
# jsoncpp的库路径和头文件目录
ExternalProject_Get_Property(jsoncpp INSTALL_DIR)
set(JSONCPP_INCLUDE_DIR ${INSTALL_DIR}/include)
set(JSONCPP_LIBRARY ${INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}jsoncpp${CMAKE_STATIC_LIBRARY_SUFFIX})
file(MAKE_DIRECTORY ${JSONCPP_INCLUDE_DIR}) # Must exist.
# JsonCpp库
add_library(JsonCpp STATIC IMPORTED GLOBAL)
set_property(TARGET JsonCpp PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${JSONCPP_INCLUDE_DIR})
set_property(TARGET JsonCpp PROPERTY IMPORTED_LOCATION ${JSONCPP_LIBRARY})
add_dependencies(JsonCpp jsoncpp)
# 取消临时定义
unset(INSTALL_DIR)
libdevcore模块cmake
# 获取源文件和头文件列表
file(GLOB SRCS "*.cpp")
file(GLOB HEADERS "*.h")
# 添加库目标
add_library(devcore STATIC ${SRCS} ${HEADERS})
main模块cmake
# 获取源文件和头文件列表
file(GLOB SRCS "*.cpp")
file(GLOB HEADERS "*.h")
# 添加可执行目标
add_executable(hello-cmake ${SRCS} ${HEADERS})
# 添加依赖库
target_link_libraries(hello-cmake PUBLIC devcore JsonCpp)