【CMake】
1、最基本的配置文件
cmake_minimum_required (VERSION 2.6) project (Tutorial) add_executable(Tutorial tutorial.cxx)
2、option命令
Provide an option for the user to select as ON or OFF. If no initial value is provided, OFF is used.
option(<option_variable> "help string describing option" [initial value])
3、configure_file
Copies an <input>
file to an <output>
file and substitutes variable values referenced as @VAR@
or ${VAR}
in the input file content.
configure_file(<input><output> @ONLY)
Furthermore, input lines of the form:
#cmakedefine VAR...
will be replaced with either:
#define VAR ... // or /* #undef VAR */
@ONLY:Restrict variable replacement to references of the form @VAR@
. This is useful for configuring scripts that use ${VAR}
syntax.
Example:
Consider a source tree containing a foo.h.in
file:
#cmakedefine FOO_ENABLE #cmakedefine FOO_STRING "@FOO_STRING@"
An adjacent CMakeLists.txt
may use configure_file
to configure the header:
option(FOO_ENABLE "Enable Foo" ON) if(FOO_ENABLE) set(FOO_STRING "foo") endif() configure_file(foo.h.in foo.h @ONLY)
This creates a foo.h
in the build directory corresponding to this source directory. If the FOO_ENABLE
option is on, the configured file will contain:
#define FOO_ENABLE #define FOO_STRING "foo"
Otherwise it will contain:
/* #undef FOO_ENABLE */ /* #undef FOO_STRING */
4、PROJECT_BINARY_DIR、PROJECT_SOURCE_DIR
binary_dir 为当前执行目录。source_dir 为传入的路径。如“cmake ../src”,
5、Adding a Version Number and Configured Header File
cmake_minimum_required (VERSION 2.6) project (Tutorial) # The version number. set (Tutorial_VERSION_MAJOR 1) set (Tutorial_VERSION_MINOR 0) # 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}") # add the executable add_executable(Tutorial tutorial.cxx)
TutorialConfig.h.in 的内容如下:
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
6、target_link_libraries
The named <target>
must have been created in the current directory by a command such as add_executable()
or add_library()
.
target_link_libraries(<target> ... <item>... ...)
7、Adding a Library (Step 2)
在子lib目录下,添加 add_library,以生成库。
add_library(MathFunctions mysqrt.cxx)
在父目录中,add_subdirectory可以使得编执行 CMakeList,并且 include_directories 可以将此目录添加搜索路径。
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions") add_subdirectory (MathFunctions)
添加exe与库的依赖
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
8、pass_regular_expression
The output must match this regular expression for the test to pass.
set_tests_properties(mytest PROPERTIES PASS_REGULAR_EXPRESSION "TestPassed;All ok" )
9、add_test
# enable testing enable_testing () # does the application run add_test (TutorialRuns Tutorial 25) # does it sqrt of 25 add_test (TutorialComp25 Tutorial 25) set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5" )
10、macro
#define a macro to simplify adding tests, then use it macro (do_test arg result) add_test (TutorialComp${arg} Tutorial ${arg}) set_tests_properties (TutorialComp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result}) endmacro (do_test) # do a bunch of result based tests do_test (25 "25 is 5") do_test (-25 "-25 is 0")
11、install
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
12、CMAKE_ROOT
Install directory for running cmake.
This is the install root for the running CMake and the Modules directory can be found here. This is commonly used in this format: ${CMAKE_ROOT}/Modules
13、add_custom_command
# first we add the executable that generates the table add_executable(MakeTable MakeTable.cxx) # add the command to generate the source code add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h DEPENDS MakeTable ) # add the binary tree directory to the search path for # include files include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) # add the main library add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h )
14、UseSWIG
SWIG_ADD_MODULE(name language [ files ])
- Define swig module with given name and specified language
SWIG_LINK_LIBRARIES(name [ libraries ])
- Link libraries to swig module
15、List
Although all values in CMake are stored as strings, a string may be treated as a list in certain contexts . In such contexts, a string is divided into list elements by splitting on ;
characters not following an unequal number of [
and ]
characters and not immediately preceded by a \
. The sequence \;
does not divide a value but is replaced by ;
in the resulting element.
A list of elements is represented as a string by concatenating the elements separated by ;
. For example, the set()
command stores multiple values into the destination variable as a list:
set(srcs a.c b.c c.c) # sets "srcs" to "a.c;b.c;c.c"
参考:https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#lists
16、
17、
参考:
1、https://cmake.org/cmake-tutorial/
2、https://gitlab.kitware.com/cmake/cmake/tree/master/Tests/Tutorial