cmake 官方学习教程

CMake官方学习教程

下面是一个一步一步的教程,包含了CMake帮助解决的常见构建系统的用例。 其中许多主题已经在Mastering CMake中作为单独的问题引入,但是看看它们如何在示例项目中一起工作可能是非常有用的。本教程可以在CMake源代码树的Tests/Tutorial目录中找到。每个步骤都有自己的子目录,其中包含该步骤完整教程的副本。
有关CMake概念和源码树组织的概述,另请参阅cmake-buildsystem(7)cmake-language(7)手册页的介绍部分。

一个基本起点 (Step1)

最基本的项目是从源代码文件构建的可执行文件。 对于简单的项目,一个两行CMakeLists.txt文件是必要的。 这将是本教程的起点。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;
}

运行结果:

hello@dhellodeMacBook-Pro:cmake_build$./Tutorial 4
The square root of 4 is 2
hello@dhellodeMacBook-Pro:cmake_build$./Tutorial 
Usage: ./Tutorial number

添加版本号和配置的头文件
我们将添加的第一个功能是为我们的可执行文件和项目提供版本号。 虽然您可以在源代码中专门执行此操作,但在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;
}

运行结果:

hello@hellodeMacBook-Pro:cmake_build$./Tutorial
./Tutorial Version 3.2
Usage: ./Tutorial number
hello@hellodeMacBook-Pro:cmake_build$./Tutorial 4
The square root of 4 is 2

主要的改变是包含TutorialConfig.h头文件,并打印出版本号作为使用信息的一部分。

添加库 (Step2)

现在我们将为我们的项目添加一个库。 这个库将包含我们自己用于计算数字的平方根的实现。然后,可执行文件可以使用这个库而不是编译器提供的标准平方根函数。在本教程中,我们将把这个库放入一个MathFunctions子目录中。它将包含下面一行CMakeLists.txt文件:

add_library(MathFunctions mysqrt.cxx)

未完待续

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章