CMake 语法
CMake 语句主要有 3 类用法:
设置变量:
set
file
list
find_library
aux_source_directory
$<...>
: generator expressions
设置
target
: 构建的目标(一般来说就是库或者可执行文件)add_library
add_executable
设置
target
的属性: 定义如何生成 target(源文件的路径、编译选项、要链接的库…)add_definitions
target_link_libraries
link_directories
include_directories
target_include_directories
预处理
project
设置项目的名字
1 | project(SYSZUXrtp) |
设置变量
set(var content)
其中 content
可以有空格换行等,第一个空格前是变量名
1 | set(SYSZUX_HEADERS |
file
使用正则匹配文件,并将文件路径赋值给第一个参数(为变量)
list
针对list
进行各种操作,如增删改查
find_library
寻找一个库,将找到的库的绝对路径赋值给变量。如
1 | find_library(LIBGEMFIELD_PATH libgemfield.so PATHS ${CUDA_TOOLKIT_ROOT_DIR}/lib64/) |
aux_source_directory(<dir> <var>)
找到dir
下所有的源文件赋值给var
,如
1 | aux_source_directory(${gemfield_root}/include gemfield_src) |
$<...>
: generator expressions
生成表达式,暂略
设置target
add_library
1 | add_library(<name> [STATIC | SHARED | MODULE] |
将一系列库整合命名为<name>
add_executable
1 | add_executable(<name> [WIN32] [MACOSX_BUNDLE] |
将源文件生成可执行文件<name>
设置target
属性
add_definitions
定义一些属性
1 | add_definitions(-DENABLE_GEMFIELD) |
target_link_libraries
链接
1 | target_link_libraries(<target> [item1 [item2 [...]]] |
链接gemfield_proxy
的时候需要有后面的库
CMake 控制
if-else
1 | if (xx AND aa) |
for
1 | foreach(i list_i) |
内置变量
系统变量
MAKE_MAJOR_VERSION
: major version number for CMake, e.g. the “2” in CMake 2.4.3CMAKE_MINOR_VERSION
: minor version number for CMake, e.g. the “4” in CMake 2.4.3CMAKE_PATCH_VERSION
: patch version number for CMake, e.g. the “3” in CMake 2.4.3CMAKE_TWEAK_VERSION
: tweak version number for CMake, e.g. the “1” in CMake X.X.X.1. Releases use tweak < 20000000 and development versions use the date format CCYYMMDD for the tweak level.CMAKE_VERSION
: The version number combined, eg. 2.8.4.20110222-ged5ba for a Nightly build. or 2.8.4 for a Release build.CMAKE_GENERATOR
: the generator specified on the commandline.BORLAND
: is TRUE on Windows when using a Borland compilerWATCOM
: is TRUE on Windows when using the Open Watcom compilerMSVC, MSVC_IDE, MSVC60, MSVC70, MSVC71, MSVC80, CMAKE_COMPILER_2005, MSVC90, MSVC10 (Visual Studio 2010)
: Microsoft compilerCMAKE_C_COMPILER_ID
: one of “Clang”, “GNU”, “Intel”, or “MSVC”. This works even if a compiler wrapper like ccache is used.CMAKE_CXX_COMPILER_ID
: one of “Clang”, “GNU”, “Intel”, or “MSVC”. This works even if a compiler wrapper like ccache is used;cmake_minimum_required
:设置所需 CMake 的最小版本;
编译相关变量!!
CMAKE_CXX_STANDARD
:设置 C++ 标准;CMAKE_CXX_FLAGS
:设置 C++ 编译参数;CMAKE_C_FLAGS
:设置 C 编译参数
1 | set(CMAKE_CXX_STANDARD 11) |
BUILD_SHARED_LIBS
: if this is set to ON, then all libraries are built as shared libraries by default. SET(BUILD_SHARED_LIBS ON) ;CMAKE_BUILD_TYPE
: A variable which controls the type of build when using a single-configuration generator like the Makefile generator. It is case-insensitive;If you are using the Makefile generator, you can create your own build type like this:
1 | set(CMAKE_BUILD_TYPE distribution) |
参考文献
关键词
https://www.cnblogs.com/binbinjx/p/5626916.html
多源文件
https://blog.csdn.net/dyyzlzc/article/details/105189374
一些内容
https://elloop.github.io/tools/2016-04-10/learning-cmake-2-commands