サブロウ丸

Sabrou-mal サブロウ丸

主にプログラミングと数学

master mind by c++; part 3 CMake

今回やること

  • CMakeの導入
  • github actionでのcmakeテストの追加

Cmakeとは

Makeファイルなどのプロジェクトファイル自動生成ツールです.
最近の, どのプロジェクトにも大抵用意されてありますね.
勝手に作るCMake入門 その1 基本的な使い方 - かみのメモ に詳細な説明があります.

CMake

フォルダ構成

GitHub - nariaki3551/master_mind_cpp at feature/cmake

master_mind_cpp $ tree ./
./
├── README.md
└── src
    ├── def.h
    ├── main.cpp
    ├── ...
    └── utils.h

main.cppを分割しました. main.cppが実行ファイルで, この中で他のヘッダーファイルをimportして使用しています.

CMakeLists.txtの作成

簡単なものを作成します.

ルートディレクトリと, srcディレクトリにそれぞれCMakeLists.txtとして コンパイルの仕様書を作成します.
ルートディレクトリには下記を作成.

cmake_minimum_required(VERSION 3.1)
project(MasterMind CXX)

add_subdirectory(src)  # srcディレクトリをcmake管理に追加

srcディレクトリには

add_executable(   # main.cppを使って, mastermindをビルド
  mastermind
  main.cpp
  )
target_compile_options(   # コンパイルオプション -Wall を追加
  mastermind PUBLIC
  -Wall
  )
target_compile_features(  # stdバージョン(-std=c++17)指定
  mastermind PUBLIC
  cxx_std_17
  )

を配置.

master_mind_cpp $ tree ./
./
├── README.md
├── CMakeLists.txt  # 追加!!
└── src
    ├── CMakeLists.txt # 追加!!
    ├── def.h
    ├── main.cpp
    ├── ...
    └── utils.h

ビルド

おなじみの

$ mkdir build; cd build; cmake ..; make
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /xxx/master_mind_cpp/build
[ 50%] Building CXX object src/CMakeFiles/mastermind.dir/main.cpp.o
[100%] Linking CXX executable mastermind
[100%] Built target mastermind

./src/build/src/mastermind というバイナリが生成されればビルド完了です.

バイナリ生成場所の指定

バイナリを生成する場所を変えるには, CMAKE_RUNTIME_OUTPUT_DIRECTORY変数を編集します. cmake .. -DCMAKE_RUNTIME_OUTPUT_DIRECTORY="xxx" で設定できます. デフォルト値を指定する場合はルートディレクトリのCMakeLists.txtに下記のように追記します.

cmake_minimum_required(VERSION 3.1)
project(MasterMind CXX)

# setting of CMAKE_RUNTIME_OUTPUT_DIRECTORY
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)  # 変数が設定されていなければ
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) # 設定!!
endif()

# add mastermind
add_subdirectory(src)

GitHub action - CMake テスト

GitHubにはactionという, pushやpull requestのたびに動く自動テスト機能があります. ビルドテストや単体テストが実行され, 問題があれば赤いチェックマークが表示されます.

f:id:inarizuuuushi:20210616220613p:plain:w400

画像の一番右にありますね.

f:id:inarizuuuushi:20210616220843p:plain

テンプレートが用意されてあります. 画像右のCMakeを使います.

f:id:inarizuuuushi:20210616221048p:plain

今回はデフォルトのものをそのまま使います. もしサードパーティのソフトを利用してコンパイルする場合は, そのインストール処理もこのyamlファイルに記載する必要があります.
また一番上

on:
  push:
    branches: [ main ]  # mainブランチへのpush時
  pull_request:
    branches: [ main ]  # mainブランチへのpush request時

の部分を

on: [push, pull_request ]

に変えると, 全てのpush, pull_request実行時にこのテストが実行されるように変更できます.
右上のstart commit で pushするとok.

f:id:inarizuuuushi:20210616221643p:plain

動いていますね.

まとめ

CMakeを導入しました. またgithub actionでの自動CMakeビルドテストを追加しました.

コード

参考

他の記事