# 一. 基本开始

## 1. 构建简单工程

```cmake_minimum_required (VERSION 2.6)
project (Tutorial)

```// tutorial.cxx
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (argc < 2)
{
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;
}```

## 2. 添加版本号并配置头文件

```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}")

```// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@```

```// tutorial.cxx
// 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(MathFunctions mysqrt.cxx)`

```include_directories ("\${PROJECT_SOURCE_DIR}/MathFunctions")

```# should we use our own math functions?
option (USE_MYMATH
"Use tutorial provided math implementation" ON) ```

```# add the MathFunctions library?
#
if (USE_MYMATH)
include_directories ("\${PROJECT_SOURCE_DIR}/MathFunctions")
set (EXTRA_LIBS \${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

```// tutorial.cxx
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif

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]);

#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
#endif

fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}```

`#cmakedefine USE_MYMATH`

# 三. 安装与测试

```install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)```

```# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "\${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include)```

```include(CTest)

# does the application run

# does it sqrt of 25
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")

# does it handle negative numbers
set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")

# does it handle small numbers
set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")

# does the usage message work?
set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")```

PS：官网教程中还有剩余几步，但笔者感觉好像没有什么用…… 所以就写到这里好了。 注：略去的几步： 4、添加系统内省(Adding System Introspection) 5、添加已生成文件和生成器(Adding a Generated File and Generator) 6、构建一个安装程序(Building an Installer)

62 篇文章33 人订阅

0 条评论

## 相关文章

2363

3125

4286

2613

2724

1365

### 透彻理解MyBatis设计思想之手写实现

MyBatis，曾经给我的感觉是一个很神奇的东西，我们只需要按照规范写好XXXMapper.xml以及XXXMapper.java接口。要知道我们并没有提供XX...

791

4688

### Spring IOC 容器源码分析系列文章导读

Spring 是一个轻量级的企业级应用开发框架，于 2004 年由 Rod Johnson 发布了 1.0 版本。经过十几年的迭代，现在的 Spring 框架已...

1453

4174