语法c++
CMakeLists.txt 的语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的,可是变量是区分的。
符号 # 后面的内容被认为是注释。
命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。数组
最简单的CMakeLists.txt能够只有三行:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.6)
# 该命令表示项目的名称是 Tutorial
project (Tutorial)
# 将名为 tutorial.cc 的源文件编译成一个名称为 Tutorial 的可执行文件
add_executable(Tutorial tutorial.cxx)ide
添加版本号
首先修改顶层 CMakeLists 文件,在 project 命令以后加入以下两行
set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)函数
分别指定当前的项目的主版本号和副版本号。
以后,为了在代码中获取版本信息,咱们能够修改 TutorialConfig.h.in 文件,添加两个预约义变量
// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@测试
在CmakeList.txt 中添加如下代码:
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")ui
再在代码中包含 TutorialConfig.h头文件,这样就能够使用版本号的宏了。debug
生成库文件
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])
注:默认是STATIC,code
多个目录,多个源文件orm
使用命令 add_subdirectory 指明本项目包含一个子目录, 该目录下必须有CMakeLists.txt
aux_source_directory(. DIR_LIB_SRCS),查找当前目录下的全部源文件,并将名称保存到
DIR_LIB_SRCS 变量
使用命令 target_link_libraries 指明可执行文件 main 须要链接一个连接库get
自定义编译选项
CMake 容许为项目增长编译选项,从而能够根据用户的环境和需求选择最合适的编译方案。
# should we use our own math functions?
option (USE_MYMATH
"Use tutorial provided math implementation" ON)
# add the MathFunctions library?
#
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
在configured 中添加如下代码:
#cmakedefine USE_MYMATH
安装
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
CMake变量
常常配合set命令使用的CMake变量,使用set(variable value)进行设置。
CMAKE_VERBOSE_MAKEFILE on 输出详细的编译和连接信息
CMAKE_CXX_COMPILER "g++" c++编译器
CMAKE_CXX_FLAGS "-Wall" c++编译器参数
CMAKE_CXX_FLAGS_DEBUG 除CMAKE_CXX_FLAGS外,debug版本的额外编译器参数
CMAKE_CXX_FLAGS_RELEASE 除CMAKE_CXX_FLAGS外,release版本的额外编译器参数
EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin 可执行文件的输出目录
LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib 连接库的输出目录
添加 CheckFunctionExists 宏
首先在顶层 CMakeLists 文件中添加 CheckFunctionExists.cmake 宏,并调用 check_function_exists 命令测试连接器是否可以在连接阶段找到 pow 函数。
# 检查系统是否支持 pow 函数
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (pow HAVE_POW)
将上面这段代码放在 configure_file 命令前。
接下来修改 config.h.in 文件,预约义相关的宏变量。
// does the platform provide pow function?
#cmakedefine HAVE_POW
添加生成的源文件
add_custom_command(OUTPUT output1 [output2 ...]
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[MAIN_DEPENDENCY depend]
[DEPENDS [depends...]]
[BYPRODUCTS [files...]]
[IMPLICIT_DEPENDS <lang1> depend1
[<lang2> depend2] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment]
[DEPFILE depfile]
[VERBATIM] [APPEND] [USES_TERMINAL]
[COMMAND_EXPAND_LISTS])
参考连接:https://www.hahack.com/codes/cmake/https://cmake.org/cmake-tutorial/