Environment
Download cmake source code from official website
https://cmake.org/download/
Introduction
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.
Basic
add_executable
This is a basic CmakeLists.txt file
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# just like Makefile Tutorial: tutorial.cxx
add_executable(Tutorial tutorial.cxx)
``![在这里插入图片描述](https://img-blog.csdnimg.cn/c2c8f5f9660940a296285094397fc51e.png)
`
```shell
cmake dir # 在当前文件夹生成Makefile文件,CMakeCache等
make # 编译
--------
cmake --build # 也是编译
configure_file
configure_file(input_file output_file [options])
https://cmake.org/cmake/help/latest/command/configure_file.html?highlight=configure_file
把input_file 替换成 output_file,并初始化input_file里面的变量(该变量必须被cmake注册过或是默认的)
target_include_directories
add search dir for specific target
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
PUBLIC will pupulate the INTERFACE_INCLUDE_DIRECTORIES of target
如果去掉target的话,就是全局作用
include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
target_link_libraries
需要与add_subdirectory配合起来
# add the MathFunctions library
add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)
# link library for specific target
target_link_libraries(Tutorial PUBLIC MathFunctions)
add_subdirectory
将一个也要build的文件夹(里面含有子CmakeLists.txt)加入到build过程中,可以成为一个library
比如:
#./lib/CMakeList.txt
add_library(MathFunctions mysqrt.cxx)
#./CmakeLists.txt
add_subdirectory(MathFunctions)
file
File manipulation command.
GLOB Generate a list of files that match the and store it into the .
# file(GLOB <variable> [LIST_DIRECTORIES true|false]
# [CONFIGURE_DEPENDS] [RELATIVE <path>] [<globbing-expressions>...])
file(GLOB SOURCES "src/*.hpp")
GLOB_RECURSE mode will traverse all the subdirectories of the matched directory and match the files. Subdirectories that are symlinks are only traversed if FOLLOW_SYMLINKS is given
# file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
# [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
# [<globbing-expressions>...])
file(GLOB_RECURSE FILES_NEED_FORMAT "src/*.cpp" "src/*.cc" "src/*.hpp" "src/*.h"
"test/*.cc")
add_custom_target
- COMMAND
相当于在build time执行一些customized script,比如用yacc去parse
add_library
Add a library to the project using the specified source files.
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
[<source>...])
Generally:
add_library(sparql_lex
${PROJECT_SOURCE_DIR}/src/core/sparql/lex.yy.c
${PROJECT_SOURCE_DIR}/src/core/sparql/sparql.tab.c)