Cmake学习笔记

语法

CMakeLists.txt 的语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的,但是变量是区分的。
符号 # 后面的内容被认为是注释。
命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。

最简单的CMakeLists.txt可以只有三行:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.6)
# 该命令表示项目的名称是 Tutorial
project (Tutorial)
# 将名为 tutorial.cc 的源文件编译成一个名称为 Tutorial 的可执行文件
add_executable(Tutorial tutorial.cxx)

添加版本号
首先修改顶层 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}")

再在代码中包含 TutorialConfig.h头文件,这样就可以使用版本号的宏了。

生成库文件
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])
注:默认是STATIC,

多个目录,多个源文件

使用命令 add_subdirectory 指明本项目包含一个子目录, 该目录下必须有CMakeLists.txt
aux_source_directory(. DIR_LIB_SRCS),查找当前目录下的所有源文件,并将名称保存到
DIR_LIB_SRCS 变量
使用命令 target_link_libraries 指明可执行文件 main 需要连接一个链接库

自定义编译选项

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/

 

posted @ 2019-03-22 17:43  dgang1989  阅读(588)  评论(0编辑  收藏  举报