CMake教程讀書筆記-第一步,一個基本的開始 第一步,一個基本的開始

翻譯:原文參見:cmake-tutorial

實踐測試路徑: 實踐過程

下面是一個 step-by-step 教程,涵蓋了CMake能夠涉及的通用的編譯系統問題。這些內容可能在其它的資料中 (http://www.kitware.com/products/books.php) 已經分別被介紹過。

但是如果能夠看到它們在整體是如何在一個實例項目中運作的,將會非常有幫助。這個教程可以在 CMake源代碼中的 Tests/Tutorial 目錄中找到。每個步驟有它自己的子目錄,包含了那個步驟的內容。

第一步,一個基本的開始

最基本的項目是一個只有一個由源代碼編譯而來的可執行文件。對於一個簡單的項目,我們只需要在 CMakeLists 文件中加入兩行代碼。

最基本的過程

這裏是我們這個教程的起點。

CMakeLists.txt

文件內容如下:

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

注意,這個例子在 CMakeLists 文件中使用了小寫字母的命令。CMake支持大寫,小寫,混合大小寫的命令。

tutorial.cxx

源代碼文件 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;
}

詳細過程

下面通過實踐來回顧一下具體發生了什麼。

  1. 原始文件目錄結構

    經過上面實踐,我們的目錄中只有兩個文件,如下:

    $ls -p
    tutorial/
    
    $tree tutorial/
    tutorial/
    ├── CMakeLists.txt
    └── tutorial.cpp
    
    0 directories, 2 files
    

    當前路徑下,總共只有1個目錄 tutorial , 2個文件。

  2. cmake 生成 Makefile

    將項目目錄 tutorial 做爲參數,傳遞給 cmake 之後,會在當前目錄(運行 cmake 的目錄)生成許多文件,包括 Makefile, 如下:

    $cmake tutorial/
    
    $ls -p
    CMakeCache.txt  CMakeFiles/  cmake_install.cmake  Makefile  tutorial/
    
    $tree .
    .
    ├── CMakeCache.txt
    ├── CMakeFiles
    │   ├── CMakeCCompiler.cmake
    │   ├── cmake.check_cache
    │   ├── CMakeCXXCompiler.cmake
    │   ├── CMakeDetermineCompilerABI_C.bin
    │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── CMakeOutput.log
    │   ├── CMakeSystem.cmake
    │   ├── CMakeTmp
    │   │   └── CMakeFiles
    │   │       └── cmTryCompileExec.dir
    │   ├── CompilerIdC
    │   │   ├── a.out
    │   │   └── CMakeCCompilerId.c
    │   ├── CompilerIdCXX
    │   │   ├── a.out
    │   │   └── CMakeCXXCompilerId.cpp
    │   ├── Makefile2
    │   ├── Makefile.cmake
    │   ├── progress.marks
    │   ├── TargetDirectories.txt
    │   └── Tutorial.dir
    │       ├── build.make
    │       ├── cmake_clean.cmake
    │       ├── DependInfo.cmake
    │       ├── depend.make
    │       ├── flags.make
    │       ├── link.txt
    │       └── progress.make
    ├── cmake_install.cmake
    ├── Makefile
    └── tutorial
     ├── CMakeLists.txt
     └── tutorial.cpp
    
     8 directories, 28 files
    

    cmake 之後,當前目錄下多了 CMakeCache.txt, CMakeFiles, cmake_install.cmake, Makefile 。當前文件總共是8個目錄,28個文件, tutorial 內容不變。其中 Makefile 是生成的 makefile。

  3. make 編譯

    $make
    Scanning dependencies of target Tutorial
    [100%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cpp.o
    Linking CXX executable Tutorial
    [100%] Built target Tutorial
    
    $ls -p
    CMakeCache.txt  CMakeFiles/  cmake_install.cmake  Makefile  tree.txt  tutorial/  Tutorial
    
    $tree .
    .
    ├── CMakeCache.txt
    ├── CMakeFiles
    │   ├── CMakeCCompiler.cmake
    │   ├── cmake.check_cache
    │   ├── CMakeCXXCompiler.cmake
    │   ├── CMakeDetermineCompilerABI_C.bin
    │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── CMakeOutput.log
    │   ├── CMakeSystem.cmake
    │   ├── CMakeTmp
    │   │   └── CMakeFiles
    │   │       └── cmTryCompileExec.dir
    │   ├── CompilerIdC
    │   │   ├── a.out
    │   │   └── CMakeCCompilerId.c
    │   ├── CompilerIdCXX
    │   │   ├── a.out
    │   │   └── CMakeCXXCompilerId.cpp
    │   ├── Makefile2
    │   ├── Makefile.cmake
    │   ├── progress.marks
    │   ├── TargetDirectories.txt
    │   └── Tutorial.dir
    │       ├── build.make
    │       ├── cmake_clean.cmake
    │       ├── CXX.includecache
    │       ├── DependInfo.cmake
    │       ├── depend.internal
    │       ├── depend.make
    │       ├── flags.make
    │       ├── link.txt
    │       ├── progress.make
    │       └── tutorial.cpp.o
    ├── cmake_install.cmake
    ├── Makefile
    ├── tree.txt
    ├── tutorial
    │   ├── CMakeLists.txt
    │   └── tutorial.cpp
    └── Tutorial
    
    8 directories, 33 files
    

    make 之後當前路徑下比之前多了: CMakeFiles/Tutorial.dir/CXX.includecache, CMakeFiles/Tutorial.dir/depend.internal, CMakeFiles/Tutorial.dir/tutorial.cpp.o, Tutorial ,總共8個目錄33個文件, tutorial 目錄內容仍舊不變。其中, Tutorial 是可執行文件。

    另外,實踐後, make cleanmake 少了 TutorialCMakeFiles/Tutorial.dir/tutorial.cpp.o

更多信息

通常過程

代碼如下:

$ tree .
.
└── tutorial
    ├── CMakeLists.txt
    └── tutorial.cpp

1 directory, 2 files

命令如下:

$ mkdir build && cd build
$ cmake
$ make
$ ./Tutorial 
Usage: ./Tutorial number
$ ./Tutorial 25
The square root of 25 is 5

添加版本號和配置頭文件

現在我們將爲項目加入的第一個特性,爲可執行文件和項目提供一個版本號。你可以在源代碼中使用互斥的代碼來實現這一點,但是使用 CMakeLists 文件來做這些事情會很靈活。

CMakeLists.txt

爲了添加一個版本號,我們需要修改 CMakeLists 文件如下:

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)

這裏,由於配置文件將會被寫入到 binary tree 中(對應後面運行 cmake 的目錄 ${PROJECT_BINARY_DIR}, 也就是生成可執行文件的目錄),我們必須添加相應的目錄到 paths 列表中(對應 include_directories),以便能夠搜索 include 文件(對應 ${PROJECT_BINARY_DIR}/TutorialConfig.h )。

TutorialConfig.h.in

之後,我們在 source tree 中(對應 Tutorial 的代碼目錄 ${PROJECT_SOURCE_DIR} ),創建一個 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 文件中定義的相應變量值。

tutorial.cxx

下一步,我們將修改 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 文件,並且在 tutorial.cxx 中打印使用消息字串時添加版本號的信息。

過程

下面通過時間回顧一下具體發生了什麼。
注意,這裏爲了便於記錄和對比,我們首先在編譯目錄中建立了 tree.origin, tree.cmakelog, tree.makelog, tree.cleanlog 以記錄文件的變化。

  1. 原始文件目錄結構

    $ls -p
    tree.cleanlog  tree.cmakelog  tree.makelog  tree.origin  tutorial/
    
    $tree . >tree.origin
    
    $cat tree.origin
    .
    ├── tree.cleanlog
    ├── tree.cmakelog
    ├── tree.makelog
    ├── tree.origin
    └── tutorial
        ├── CMakeLists.txt
        ├── TutorialConfig.h.in
        └── tutorial.cpp
    
    1 directory, 7 files
    
  2. cmake 生成 Makefile

    $cmake tutorial
    $tree . >tree.cmakelog
    $cat tree.cmakelog
    .
    ├── CMakeCache.txt
    ├── CMakeFiles
    │   ├── CMakeCCompiler.cmake
    │   ├── cmake.check_cache
    │   ├── CMakeCXXCompiler.cmake
    │   ├── CMakeDetermineCompilerABI_C.bin
    │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── CMakeOutput.log
    │   ├── CMakeSystem.cmake
    │   ├── CMakeTmp
    │   │   └── CMakeFiles
    │   │       └── cmTryCompileExec.dir
    │   ├── CompilerIdC
    │   │   ├── a.out
    │   │   └── CMakeCCompilerId.c
    │   ├── CompilerIdCXX
    │   │   ├── a.out
    │   │   └── CMakeCXXCompilerId.cpp
    │   ├── Makefile2
    │   ├── Makefile.cmake
    │   ├── progress.marks
    │   ├── TargetDirectories.txt
    │   └── Tutorial.dir
    │       ├── build.make
    │       ├── cmake_clean.cmake
    │       ├── DependInfo.cmake
    │       ├── depend.make
    │       ├── flags.make
    │       ├── link.txt
    │       └── progress.make
    ├── cmake_install.cmake
    ├── Makefile
    ├── tree.cleanlog
    ├── tree.cmakelog
    ├── tree.makelog
    ├── tree.origin
    ├── tutorial
    │   ├── CMakeLists.txt
    │   ├── TutorialConfig.h.in
    │   └── tutorial.cpp
    └── TutorialConfig.h
    
    8 directories, 34 files
    

    注意,生成的 TutorialConfig.h 生成路徑是當前運行 cmake 的路徑。 tutorial 源碼路徑內容不變(除非直接在源碼路徑中運行 cmake )。

  3. make 編譯

    $make
    Scanning dependencies of target Tutorial
    [100%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cpp.o
    Linking CXX executable Tutorial
    [100%] Built target Tutorial
    
    $tree .>tree.makelog
    $cat tree.makelog
    .
    ├── CMakeCache.txt
    ├── CMakeFiles
    │   ├── CMakeCCompiler.cmake
    │   ├── cmake.check_cache
    │   ├── CMakeCXXCompiler.cmake
    │   ├── CMakeDetermineCompilerABI_C.bin
    │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── CMakeOutput.log
    │   ├── CMakeSystem.cmake
    │   ├── CMakeTmp
    │   │   └── CMakeFiles
    │   │       └── cmTryCompileExec.dir
    │   ├── CompilerIdC
    │   │   ├── a.out
    │   │   └── CMakeCCompilerId.c
    │   ├── CompilerIdCXX
    │   │   ├── a.out
    │   │   └── CMakeCXXCompilerId.cpp
    │   ├── Makefile2
    │   ├── Makefile.cmake
    │   ├── progress.marks
    │   ├── TargetDirectories.txt
    │   └── Tutorial.dir
    │       ├── build.make
    │       ├── cmake_clean.cmake
    │       ├── CXX.includecache
    │       ├── DependInfo.cmake
    │       ├── depend.internal
    │       ├── depend.make
    │       ├── flags.make
    │       ├── link.txt
    │       ├── progress.make
    │       └── tutorial.cpp.o
    ├── cmake_install.cmake
    ├── Makefile
    ├── tree.cleanlog
    ├── tree.cmakelog
    ├── tree.makelog
    ├── tree.origin
    ├── tutorial
    │   ├── CMakeLists.txt
    │   ├── TutorialConfig.h.in
    │   └── tutorial.cpp
    ├── Tutorial
    └── TutorialConfig.h
    
    8 directories, 38 files
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章