原文 https://cmake.org/cmake-tutorial/

以下是一个循序渐进的教程,它覆盖了CMAKE帮助改进的通常的构建系统的话题。许多话题在《掌握CMAKE》(《Mastering CMake》)已经作为单独话题介绍,但是在一个示例工程中运用它们会更有帮助于学习。教程可以再本项目开源代码Tests/Tutorial 目录下找到代码树。每一步操作都有包含完整代码的子目录。

第一步 基础起点

最基础的工程就是从代码中构建一个执行文件。对于简单工程来说,仅仅需要两行CMAKELISTS文件内容。 CMakeLists.txt内容看起来是这样:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

注意CMakeLists.txt文件中的例子使用的是小写命令。CMake支持大写、小写、混合大小写命令。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;
}

添加版本号和配置头文件
我们添加的第一个功能就是添加版本号。相比在代码中写入版本号,在CMakeLists.txt中提供此功能会更具有灵活性。 CMakeLists.txt文件中添加版本号修改如下:

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@

当CMake配置这个头文件时,@Tutorial_VERSION_MAJOR@与 @Tutorial_VERSION_MINOR@的值将由CMakeLists.txt文件中的值来替代.当我们修改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;
}

主要的改变在于添加了TutorialConfig.h头文件和将版本信息作为用户信息的一部分打印出来

//=========================================================
第二步 添加库
我们将添加一个库到我们的工程中。这个库包含我们自己实现的计算数字平方根的实现.执行文件能使用这个库来替代编译器提供的标准平方根函数。本教程中,我们将这个库放进一个子目录MathFunctions中。该目录包含一行内容的CMakeLists.txt 文件:
add_library(MathFunctions mysqrt.cxx)

源码文件mysqrt.cxx 有一个叫做mysqrt的函数,这个函数提供类型编译器的sqrt函数的功能。在顶层CMakeFiles.txt文件中调用我们加进子目录的库,以便于该库得到构造。我们同样使用另一个包含目录以便于MathFunctions/mysqrt.h 头文件能够找到函数原型。最后改变是添加这个新库到执行文件。顶层CMakeLists.txt文件最后几行看起来如下:
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)

# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)

现在我们来考虑如何使得这个库能够变得可选。本教程中,没有任何理由需要如此,但是在第三方提供的较大型的库中,你可能需要如此。第一步是在CMakeLists.txt文件中添加一个选项

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

This will show up in the CMake GUI with a default value of ON that the user can change as desired. This setting will be stored in the cache so that the user does not need to keep setting it each time they run CMake on this project. The next change is to make the build and linking of the MathFunctions library conditional. To do this we change the end of the top level CMakeLists.txt file to look like the following:

一下显示了CMake如何在默认值工作,该值用户可以改变。这个设置将存储在缓存中以便用户不需要每次运行CMake都保持该设置。下一步是是的构建和连接该库可根据条件构建。顶层CMakeLists.txt文件中我们添加如下:
# add the MathFunctions library?
#
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial ${EXTRA_LIBS})

USE_MYMATH的设定的使用决定MathFunctions是否编译和使用。

最新文章

  1. MySQL数据库和InnoDB存储引擎文件
  2. Elixir 1.0 Release
  3. Android基于mAppWidget实现手绘地图(四)--如何附加javadoc
  4. python读取文件的前几行
  5. P值,“差异具有显著性”和“具有显著差异”
  6. 替换APK中的jar包文件
  7. 使用MyBatis链接MySQL
  8. 安装linux系统后要做的事情
  9. Android Studio学习随笔-移动动画的实现
  10. MySQL Replication Error 处理一例
  11. 【转】 Ubuntu下配置USB转串口及串口工具配置--不错
  12. java代码用dom4j解析xml文件的简单操作
  13. UVa 124 - Following Orders
  14. 【费用流】BZOJ1061: [Noi2008]志愿者招募(这题超好)
  15. HTTP请求和MIME介绍
  16. 最简单的视频编码器:基于libx264(编码YUV为H.264)
  17. 以语音评测的PC端demo代码为例,讲解口语评测如何实现
  18. MySQL安全机制 DDL DCL
  19. UML类关系:依赖、关联、聚合、组合
  20. JAVA中的CountDownLatch、CyclicBarrier、Semaphore的简单测试

热门文章

  1. git回滚分支版本到指定版本
  2. SpringIoc 和 工厂模式(反射实现)
  3. Bootstrap-Other:可视化布局
  4. 语音-数字中继-E1-学习帖
  5. PHP外部调用网站百度统计数据的方法详解
  6. php 数字格式化
  7. Mysql插入数据报错java.sql.SQLException: Incorrect string value: &#39;\xF0\x9F\x93\x8D\xE6\x88...&#39;
  8. Ubuntu12.10下Python(pyodbc)访问SQL Server解决方案
  9. CFGym 101194D 题解
  10. Bootstrap的介绍和响应式媒体查询