CMake学习笔记(一)——CMake官网教程


CMake学习笔记(一)——CMake官网教程
前言:
1週間の苦痛なクロスコンパイルを経て、筆者はLinuxの下でmakeの重要性を深く認識した.だから2、3日自分の仕事の進度を緩めて、CMakeとMakefileを勉強するつもりです.結局陳浩大神が言ったように、makefileを書くかどうかは、一人が大型工事を完成する能力を持っているかどうかを一つの側面から説明している.自分に設定した最初の授業は、CMakeの公式サイトで提供されている入門チュートリアルを勉強することです.CMake公式サイトのチュートリアルの住所:https://cmake.org/cmake-tutorial/
一.基本的な開始
1.簡易工事の構築
最も基礎的なプロジェクトは、ソースファイル構築によって生成されます.ここでは最も簡単なプロジェクトを構築し、CMakeLists.txtファイルは2、3行で、チュートリアルを開始します.CMakeLists.txtは以下の通りである.
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

なお、このルーチンにおけるCMakeLists.txtファイルでは、小文字が使用されます.CMakeでは、大文字と小文字の混合コマンドもサポートされています.ソースファイルcxxは1つの数字の平方根を計算する機能を完成し、最初のバージョンは非常に簡単で、コードは以下の通りです.
// tutorial.cxx
// A simple program that computes the square root of a number
#include 
#include 
#include 
int main (int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stdout,"Usage: %s number
"
,argv[0]); return 1; } double inputValue = atof(argv[1]); double outputValue = sqrt(inputValue); fprintf(stdout,"The square root of %g is %g
"
, inputValue, outputValue); return 0; }

2.バージョン番号を追加し、ヘッダファイルを構成する
私たちが偽の最初の特徴は、実行可能なファイルとエンジニアリングにバージョン番号を提供することです.ソースコードで一人でできるようになったら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は、ヘッダファイルを含め、以前に追加したバージョン番号を使用します.最終的なソースコードは次のとおりです.
// tutorial.cxx
// A simple program that computes the square root of a number
#include 
#include 
#include 
#include "TutorialConfig.h"

int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d
"
, argv[0], Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR); fprintf(stdout,"Usage: %s number
"
,argv[0]); return 1; } double inputValue = atof(argv[1]); double outputValue = sqrt(inputValue); fprintf(stdout,"The square root of %g is %g
"
, inputValue, outputValue); return 0; }

主な変更点は、ヘッダファイルTutorialConfig.hは含まれており、バージョン番号は使用情報の一部として印刷されている.
二.ライブラリの追加
このステップでは、プロジェクトにライブラリを追加します.このライブラリには、平方根を計算する実装が含まれています.実行ファイルは、コンパイラが独自に提供する標準平方根計算方法の代わりに、このライブラリを使用できます.このチュートリアルでは、ライブラリをサブライブラリに設定し、MathFunctionsと名前を付けます.CMakeLists.txtでは、以下のように実現される.
add_library(MathFunctions mysqrt.cxx)

ソースファイルcxxにはmysqrtという関数があり、この関数はコンパイラバージョンの平方根関数に比べて簡略化されたバージョンを提供します.新しいライブラリを使用するには、最上位ルートディレクトリのCMakeListsを使用します.txtはadd_subdirectoryを呼び出し、これによりライブラリが構築される.また、ヘッダファイルMathFunctions/MathFunctions.hが関数プロトタイプによって見つけられるように、別のパスを追加することもできます.
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 

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

次に、MathFunctionsライブラリを使用可能にするかどうかを考えてみましょう.この機能は、大規模なライブラリ、またはサードパーティのコードに依存するライブラリで必要です.最初のステップは、最上位ルートディレクトリのCMakeLists.txtにオプションを追加するには、次の手順に従います.
# should we use our own math functions?
option (USE_MYMATH 
        "Use tutorial provided math implementation" ON) 

CMake-GUIでは、この値はデフォルトのON値で表示され、ユーザーは自由に変更できます.この値はキャッシュファイルに格納され、ユーザはcmakeコマンドを実行するたびに一度設定する必要はありません.
# 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})

注:1、コードの中のUSE_MYMATHは後で定義されます.2、MathFunctionsがコンパイルされて使用する必要がある場合、USE_MYMATHは定義される必要があります.3、変数(このルーチンのEXTRA_LIBS)の使用は、任意のオプションのライブラリを収集し、後のコードで徐々に実行可能ファイルにリンクするために使用することができる.大規模な工事を維持する際には、通常、多くのオプション部分があり、工事の明確さを維持するために、これは一般的な使い方です.
// tutorial.cxx
// A simple program that computes the square root of a number
#include 
#include 
#include 
#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
"
, argv[0], Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR); fprintf(stdout,"Usage: %s number
"
,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
"
, inputValue, outputValue); return 0; }

上のソースコードではUSEも使用していますMYMATH.前のTutorialConfigでh.inプロファイルに次の行を追加します.
#cmakedefine USE_MYMATH

三.インストールとテスト
この手順では、インストールルールとテストサポートをエンジニアリングに追加します.インストールルールは簡単で直接的です.MathFunctionsの場合、MathFuncitionsのCMakeListsでtxtに2行のコードを追加すると、ライブラリとヘッダファイルをインストールできます.
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)

また、最上位ルートディレクトリのCMakeLists.txtには、実行可能なファイルとヘッダファイルをインストールするためのコードもいくつか追加する必要があります.
# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"        
         DESTINATION include)

ここまで来たら、自分のチュートリアルを構築することができます.make installを入力すると、プロジェクトに適切なヘッダファイル、ライブラリファイル、実行可能ファイルがインストールされます.ここで、CMake変数CMAKE_INSTALL_PREFIXは、インストールファイルのルートディレクトリを定義するために使用されます.
テストを追加するのも簡単で直接的な操作です.最上位ルートディレクトリのCMakeLists.txtでは、アプリケーションが正しいかどうかを測定するために、いくつかの簡単なテストコードを追加することができます.
include(CTest)

# does the application run
add_test (TutorialRuns Tutorial 25)

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

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

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

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

構築後、ctestコマンドラインを実行してテストを実行します.
PS:公式サイトのチュートリアルにはあと数歩残っていますが、筆者はあまり役に立たないような気がしますが...ここまで書いておきます.注意:省略する手順:4、システム内省の追加(Adding System Introspection)5、生成されたファイルとジェネレータの追加(Adding a Generated File and Generator)6、インストールプログラムの構築(Building an Installer)