目录
3,PROJECT_NAME、CMAKE_PROJECT_NAME
7,include_directories、target_include_directories
9,add_library、target_link_libraries
10,add_compile_options、target_compile_options
一,cmake
In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method.
二,基本词法
1,核心分词规则
根据空格识别词,根据换行识别句子,行尾不需要分号
2,注释#
如:
#cmake_minimum_required(VERSION 3.16)
单行注释
3,大小写
指令是大小写无关的,参数和变量是大小写相关的
4,变量、命令参数
${A}用来取变量A的值
指令(参数1 参数2...)
5,引号
SET(SRC_LIST main.c)也可以写成SET(SRC_LIST “main.c”),是没有区别的,
但是假设一个源文件的文件名是fu nc.c(文件名中间包含了空格)这时候就必须使用双引号,SET(SRC_LIST “fu nc.c”)
三,常用语法
1,cmake_minimum_required
cmake_minimum_required(VERSION <min>[...<max>] [FATAL_ERROR])
min是最低版本号,max是最高版本号,FATAL_ERROR是错误处理
min是必选参数,max是非必选参数,FATAL_ERROR在2.6版本之后被忽略
cmake_minimum_required命令需要放在文本中最前面
如:
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.16[...3.18])
cmake_minimum_required(VERSION 3.16[...3.18][ergtrehgrerhw])
2,project
Sets the name of the project, and stores it in the variable PROJECT_NAME.
即设置项目名称
如:
project(cpptmp)
3,PROJECT_NAME、CMAKE_PROJECT_NAME
PROJECT_NAME is the name given to the most recently called project() command in the current directory scope or above.
CMAKE_PROJECT_NAME is the name of the top level project.
即最近项目名称和最顶层项目名称
4,set、变量
Set a normal, cache, or environment variable to a given value.
如:
set(CMAKE_CXX_STANDARD 14) #设置语言版本
set(MASTER ${CMAKE_CURRENT_SOURCE_DIR}/../) #定义变量,表示某个路径
set用来定义变量
5,add_executable
Add an executable to the project using the specified source files.
有三种可执行文件:Normal Executables、Imported Executables、Alias Executables
(1)Normal Executables
完整:add_executable(<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [source1] [source2 ...])
简单:add_executable(<name> [source1] [source2 ...])
name是可执行文件的名称,必须全局唯一
source*是代码文件名
如:
add_executable(cpptmp main.cpp)
(2)Imported Executables
add_executable(<name> IMPORTED [GLOBAL])
An IMPORTED executable target references an executable file located outside the project.
(3)Alias Executables
add_executable(<name> ALIAS <target>)
Creates an Alias Target, such that <name> can be used to refer to <target> in subsequent commands.
6,list
list命令很丰富:
(1)增删改
list(APPEND <list> [<element>...])
list(FILTER <list> {INCLUDE | EXCLUDE} REGEX <regex>)
list(INSERT <list> <index> [<element>...])
list(POP_BACK <list> [<out-var>...])
list(POP_FRONT <list> [<out-var>...])
list(PREPEND <list> [<element>...])
list(REMOVE_ITEM <list> <value>...)
list(REMOVE_AT <list> <index>...)
list(REMOVE_DUPLICATES <list>)
list(TRANSFORM <list> <ACTION> [...])
如:
list(APPEND include_paths path1 path2 path3)
include_directories(${include_paths})
APPEND 可以用在各级项目中设置include_paths这个自定义变量,然后include_directories就可以设置包含这些目录
(2)查
list(LENGTH <list> <out-var>)
list(GET <list> <element index> [<index> ...] <out-var>)
list(JOIN <list> <glue> <out-var>)
list(SUBLIST <list> <begin> <length> <out-var>)
list(FIND <list> <value> <out-var>)
(3)排序
list(REVERSE <list>)
list(SORT <list> [...])
7,include_directories、target_include_directories
如:
include_directories(${include_paths})
target_include_directories(app PUBLIC ${include_paths})
都是用来设置include目录的,include_directories是范围最大的,影响全局,target_include_directories只影响单个目标
8,file、正则表达式
如:
file(GLOB FILES src/*)
add_library(app ${FILES} )
利用正则表达式,把一些目录下的所有文件添加到FILES变量中,然后add_library就可以用这些文件编一个库
9,add_library、target_link_libraries
(1)add_library
add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] source1 [source2 ...])
Adds a library target called <name>
to be built from the source files listed in the command invocation. The <name>
corresponds to the logical target name and must be globally unique within a project.
SHARED,动态库
STATIC,静态库
MODULE,在使用 the dynamic link editor的系统有效,如果不支持dyld,则被当作SHARED对待。
EXCLUDE_FROM_ALL参数的意思是这个库不会被默认构建,除非有其他的组件依赖或者手工构建。
如:
add_library(mylib ${FILES} )
用列表中的文件生成一个库文件,库的名字必须全局唯一
(2)target_link_libraries
target_link_libraries(<target> ... <item>... ...)
The named <target> must have been created in the current directory by a command such as add_executable() or add_library().
target必须是前面已经用别的命令创建的目标,比如add_executable创建的可执行文件,add_library创建的库文件
10,add_compile_options、target_compile_options
(1)add_compile_options
add_compile_options(<option> ...)
Adds options to the compiler command line for targets in the current directory and below that are added after this command is invoked.
add_compile_options是添加编译选项,影响范围是当前目录和本命令下面引用的子目录
(2)target_compile_options
target_compile_options(<target> [BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
Specify compile options to use when compiling a given target. The named <target> must have been created by a command such as add_executable() or add_library() and must not be an IMPORTED Target.
target必须是前面已经用别的命令创建的目标,而且不能是IMPORTED目标