Programing/Tool/CMake/Guides/Tutorial のバックアップ差分(No.2)


  • 追加された行はこの色です。
  • 削除された行はこの色です。
#contents
----
[[CMake Tutorial>https://cmake.org/cmake/help/latest/guide/tutorial/index.html]]の簡易翻訳

*はじめに [#z0b8f822]

*Step1.基本 [#p4c44c30]
最も基本的なプロジェクトは、ソースコードから実行ファイルをビルドします。

- 基本プロジェクト
最も基本的なプロジェクトは、ソースコードから実行ファイルをビルドするプロジェクトです。
単純なプロジェクトの場合、必要なのは3行の CMakeLists.txt ファイルのみです。
次のような CMakeLists.txt ファイルをディレクトリに作成します。
このファイルがチュートリアルの出発点になります。
次のような CMakeLists.txt ファイルを Setp1 ディレクトリに作成します。

#geshi(CMake){{
#geshi(make,number){{
cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial tutorial.cxx)
}}
Note: この例では CMakeLists.txt で小文字のコマンドを使用していることに注意してください。
大文字、小文字、および大文字と小文字が混在するコマンドをCMakeでサポートします。
** バージョン番号と設定済みヘッダファイルの追加 [#c6e974cf]

Note that this example uses lower case commands in the CMakeLists.txt file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx is provided in the Step1 directory and can be used to compute the square root of a number.
- バージョン番号の追加
実行ファイルとプロジェクトにバージョン番号を追加します。
#geshi(make,number){{
cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)
}}

- 設定済み設定ファイル
バージョン番号をソースコードに渡すためのヘッダファイルを設定します。
configure_fileコマンドを使用することで、テンプレートファイル(TutorialConfig.h.in)内にCMakeLists.txtで定義した変数の内容を埋め込んで、ヘッダファイル(TutorialConfig.h)を生成します。
#geshi(make,number){{
configure_file(TutorialConfig.h.in TutorialConfig.h)
}}
- 検索パスの追加
設定済みファイルはバイナリツリーに書き込まれるため、インクルードファイルを検索するために、パスのリストに追加する必要があります。 
CMakeLists.txt の最後に次の行を追加します。
#geshi(make,number){{
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )
}}
- テンプレートファイル
次の内容でソースディレクトリに テンプレートファイル(TutorialConfig.h.in)を作成します。
#geshi(make,number){{
// 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@ が変数値に置き換えられます。
- ソースファイル
次に、tutorial.cxxを変更して、構成済みヘッダーファイル TutorialConfig.h をインクルードします。
最後に、以下のように実装することで実行可能ファイルの名前とバージョン番号を出力します。。
#geshi(make,number){{
  if (argc < 2) {
    // report version
    std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
              << Tutorial_VERSION_MINOR << std::endl;
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }
}}