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)

未完待續

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