CMake简介

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. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
生成可执行文件
CMakeList.txt
PROJECT(程序名)
SET(SRC_LIST 源文件1 源文件2...)
ADD_EXECUTABLE(可执行文件名 ${SRC_LIST})
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
添加一个版本号:
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)
Config.h.in文件
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
在源码中使用版本号
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
生成共享库
指令说明
注:指令大小写无关,指令参数大小写相关
ADD_LIBRARY(libname [SHARED|STATIC|MODULE] [EXCLUDE_FROM_ALL] source1 source2 ... sourceN)
SHARED:动态库
STATIC:静态库
MODULE:在使用dyld的系统有效,如果不支持dyld,则被当作SHARED对待。
EXCLUDE_FROM_ALL:参数的意思是这个库不会被默认构建,除非有其他的组件依赖或者手动构建。
PROJECT指令语法:PROJECT(工程名 [C] [C++] [java])
定义工程名称,并指定工程支持的语言,支持语言列表可忽略。默认支持所有语言。
这个指令隐式的定义了两个cmake变量:
| 变量名称 | 说明 |
|---|---|
| <projectname_BINARY_DIR> | 工程编译目录 |
| <projectname_SOURCE_DIR> | 工程源码目录 |
Cmake系统预定了<PROJECT_BINARY_DIR>和<PROJECT_SOURCE_DIR>两个变量。变量值和上述两个变量保持一致。
MESSAGE指令语法:MESSAGE([SEND_ERROR][STATUS][FATAL_ERROR] "strings")
SEND_ERROR:产生错误生成过程被跳过
STATUS:输出前缀为'-'的信息
FATAL_ERROR:立即终止cmake过程