欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Cmake记录

程序员文章站 2022-07-15 16:43:17
...

cmake命令的大小写不敏感,

cmake_minimum_required(VERSION 2.6)
project(Turtorial)
include_directories("")
add_executable(Tutorial tutorial.cxx)

	add_library(MathFunctions mysqrt.cxx)

add_subdirectory(MathFunctions)
target_link_libraries(Tutorial MathFunctions)
是否需要调用自定义的mysqrt函数,使用可选择的形式:
option(USE_MYMATH
		"Use tutorial provided math implementation" ON)
默认值为ON

这个设置被保存在cache中,

if (USE_MYMATH)
	include_directories("")	//头文件所在目录
	add_subdirectory()
	set(EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial ${EXTRA_LIBS})

include

Load and run CMake code from a file or module.	//"include用来加载和运行.cmake文件"

include(<file|module> [OPTIONAL] [RESULT_VARIABLE <VAR>]
                      [NO_POLICY_SCOPE])

Load and run CMake code from the file given. Variable reads and writes access the scope of the caller (dynamic scoping). If OPTIONAL is present, then no error is raised if the file does not exist. If RESULT_VARIABLE is given the variable will be set to the full filename which has been included or NOTFOUND if it failed.

If a module is specified instead of a file, the file with name <modulename>.cmake is searched first in CMAKE_MODULE_PATH, then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards. See also policy CMP0017.

See the cmake_policy() command documentation for discussion of the NO_POLICY_SCOPE option.

include_directories

List of preprocessor include file search directories.

This property specifies the list of directories given so far to the target_include_directories() command. In addition to accepting values from that command, values may be set directly on any target using the set_property() command. A target gets its initial value for this property from the value of the INCLUDE_DIRECTORIES directory property. Both directory and target property values are adjusted by calls to the include_directories() command.

The value of this property is used by the generators to set the include paths for the compiler.

Relative paths should not be added to this property directly. Use one of the commands above instead to handle relative paths.

Contents of INCLUDE_DIRECTORIES may use “generator expressions” with the syntax $<...>. See the cmake-generator-expressions(7) manual for available expressions. See the cmake-buildsystem(7) manual for more on defining buildsystem properties.

make_directory使用file()代替

Deprecated. Use the file(MAKE_DIRECTORY ) command instead.	/*使用file(MAKE_DIRECTORY)来代替*/

make_directory(directory)
Creates the specified directory. Full paths should be given. Any parent directories that do not exist will also be created. Use with care.

file

https://cmake.org/cmake/help/v3.0/command/file.html?highlight=file

Reading
  file(READ <filename> <out-var> [...])
  file(STRINGS <filename> <out-var> [...])
  file(<HASH> <filename> <out-var>)
  file(TIMESTAMP <filename> <out-var> [...])

Writing
  file({WRITE | APPEND} <filename> <content>...)
  file({TOUCH | TOUCH_NOCREATE} [<file>...])
  file(GENERATE OUTPUT <output-file> [...])

Filesystem
  file({GLOB | GLOB_RECURSE} <out-var> [...] [<globbing-expr>...])
  file(RENAME <oldname> <newname>)
  file({REMOVE | REMOVE_RECURSE } [<files>...])
  file(MAKE_DIRECTORY [<dir>...])
  file({COPY | INSTALL} <file>... DESTINATION <dir> [...])

Path Conversion
  file(RELATIVE_PATH <out-var> <directory> <file>)
  file({TO_CMAKE_PATH | TO_NATIVE_PATH} <path> <out-var>)

Transfer
  file(DOWNLOAD <url> <file> [...])
  file(UPLOAD <file> <url> [...])

Locking
  file(LOCK <path> [...])

file(MAKE_DIRECTORY [<dir>...]):Create the given directories and their parents as needed.

IF/While/Function

Function

官方的文档:https://cmake.org/cmake/help/v3.0/command/function.html?highlight=function

Start recording a function for later invocation as a command.

function(<name>/*函数名*/ [arg1 [arg2 [arg3 ...]]]/*函数预期的函数个数*/)
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
endfunction(<name>/*函数名*/)

${ARGC}:参数个数;
${ARGV}:${ARGV0}、${ARGV1}、${ARGV2}..获得该函数接收的所有参数
${ARGN}:函数被定义成可变参数,但定义函数时定义的是期待的参数个数,当实现中传入该函数的参数超出预期的个数时,将会保存在变量ARGN中

Define a function named that takes arguments named arg1 arg2 arg3 (…). Commands listed after function, but before the matching endfunction, are not invoked until the function is invoked. When it is invoked, the commands recorded in the function are first modified by replacing formal parameters (${arg1}) with the arguments passed, and then invoked as normal commands. In addition to referencing the formal parameters you can reference the variable ARGC which will be set to the number of arguments passed into the function as well as ARGV0 ARGV1 ARGV2 … which will have the actual values of the arguments passed in. This facilitates creating functions with optional arguments. Additionally ARGV holds the list of all arguments given to the function and ARGN holds the list of arguments past the last expected argument.

A function opens a new scope: see set(var PARENT_SCOPE) for details.

See the cmake_policy() command documentation for the behavior of policies inside functions.

IF

官方的文档:https://cmake.org/cmake/help/v3.0/command/if.html?highlight=#command:if

if(expression)
  # then section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
elseif(expression2)
  # elseif section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
else(expression)
  # else section.
  COMMAND1(ARGS ...)
  COMMAND2(ARGS ...)
  ...
endif(expression)

判断的先后顺序:

  • EXISTS, COMMAND, and DEFINED
  • EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, and MATCHES
  • NOT,AND,OR;

参考

相关标签: Cmake