CMake anchor.png

CMakeは,ソフトウェアのビルドの自動化ツール。

WindowsではVisual Studioを使って,LinuxなんかだとMakeなどのツールを使ってプログラムのコンパイルを行うことが多いが,使用するライブラリや動作環境に応じてさまざまな設定を行う必要がある。
例えば,OpenGLやOpenCVなどを利用するプログラムをコンパイルするとき,ヘッダファイルの場所,ライブラリー名やその場所などの指定などがあるが,それらの具体的な設定定義がめんどくさい。

CMakeはこの煩雑な設定作業を手助けしてくれるツール。簡単なテキストファイルを一つを作れば,後の処理はCMakeが自動的に処理してくれる。Windows,Linux, Mac等の環境も考慮して処理されるためマルチプラットフォームで作る必要があるプログラムでもビルド出来るようになる。

詳細は,

なんかを読みましょう。

Page Top

Windowsマシンにインストール anchor.png

cmake for Windowsは,Windowsで動作するcmake。これをインストールしてみる。

公式サイトから,最新版のWindowsインストーラーをダウンロードする。

ダウンロードした.msiファイルをダブルクリックして実行すればインストールが始まる。

  • ようこそ画面でNextをクリック
  • ライセンス条項の確認のチェックをして,Next
  • Add CMake to the system PATH for all usersをチェックしてNext
  • インストールディレクトリを入力してNext
    インストールディレクトリは, C:\WinApl\CMake\ とかにする。
  • Installをクリック
  • Finishをクリック

これでインストールされます。

コマンドプロンプトを起動して,

D:\home\ueno> cmake --version
cmake version 3.11.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

と表示されればOK。

Page Top

CMakeを使ってみる anchor.png

適当なディレクトリで以下のようなソースコードを作成する。
hello.c

Everything is expanded.Everything is shortened.
1
2
3
4
5
6
7
 
 
 
-
|
|
!
#include <stdio.h>
 
int main(void)
{
    printf("Hello!\n");
    return 0;
}

CMakeの設定ファイルは,CMakeLists.txtという名前にする。
以下は最低限必要なCMakeLists.txt。

cmake_minimum_required(VERSION 2.8)
project(hello C)
add_executable(hello hello.c)

最初の行は,このCMakeLists.txtを使うのに必要なCMakeのバージョン。ここで指定したバージョンより古いと動作しないようになる。
GNU Autoconfでも似たような設定(AC_PREREQ)がある。でも,こんな設定はちょっと面倒。

次の行のprojectは,プロジェクト名と使用する言語を設定する。C++の場合は,CXXと設定する。

3行目のadd_executableは,実行ファイルの作成を指示するもの。引数として実行ファイル名と,ソースファイル名を指定する。
複数ソースファイルがある場合は, add_executable(hello hello.c message.c message2.c) とかにする。
ライブラリを作成する場合は,add_libraryを使う。

3行目だけでも一応動作はするがいろいろワーニングメッセージが出るので,最低限はこのようにしておく。

CMakeを実行する場合は,

> cmake [オプション] パス

とする。この時,パスのディレクトリにあるCMakeLists.txtが読み込まれて処理される。

CMakeは,ConfigureとGenerateという過程が実行される。
Configureは,CMakeLists.txtからビルドに必要な情報を収集する作業のこと。もし,CMakeLists.txtがおかしかったり,依存ライブラリが見つからなかったりした場合はエラーが表示されるので,確認して修正することが出来る。
Generateは,Configureで集めた情報を使ってMakefileやプロジェクトファイルを作成する作業。作成したいプロジェクトファイルの種類を指定して実行させます。

Page Top

CMakeによるビルド anchor.png

上記CMakeLists.txtをhelloに置いて,buildディレクトリを作成してcmakeを実行してみる。
GCCを使ってコンパイル出来るように,Makefileを作成するためのGENERATORオプション(-G)で指定している。(Unix Makefiles)
引数はディレクトリ名だが,すでにhello\buildにいるので引数は..にしておく。すると,いくつかのファイルが生成される。

> mkdir build
> cd build
> cmake -G "Unix Makefiles" ..
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: D:/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/MinGW/bin/c++.exe
-- Check for working CXX compiler: D:/MinGW/bin/c++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/home/ueno/soft/cmake/mylib/build

どんなファイルが作成されたか見てみる。

> ls
CMakeCache.txt  CMakeFiles  CMakeLists.txt  Makefile  cmake_install.cmake

Makefileが生成されているので,後はmakeでコンパイル出来る。

> make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/hello.c.obj
[100%] Linking C executable hello.exe
[100%] Built target hello

実行ファイルhello.exeが出来ているので,実行してみる。

> hello
Hello!

makeコマンドでビルドしたが,cmakeでビルドする場合は,

> cmake --build .

と,します。.の意味は,Generateで作成したディレクトリ,つまりbuildを指定するため。

GENERATORオプションを使用しない場合はデフォルトのシステムでビルドするようになる。デフォルトはVisual Studio 2017になっている。 (cmake for windowsだから)

> mkdir buildvs
> cd buildvs
> cmake ..
-- Building for: Visual Studio 15 2017
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 6.1.7601.
-- The C compiler identification is MSVC 19.16.27026.1
-- The CXX compiler identification is MSVC 19.16.27026.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/home/ueno/soft/cmake/mylib/buildvs

作成されたファイルは,

> ls
ALL_BUILD.vcxproj          CMakeCache.txt  CMakeLists.txt  ZERO_CHECK.vcxproj          cmake_install.cmake  hello.vcxproj
ALL_BUILD.vcxproj.filters  CMakeFiles      Project.sln     ZERO_CHECK.vcxproj.filters  hello.vcxproj.filters

が出来ている。
Visual Studioのソリューションファイルが出来ているので,この場合Visual Studioでコンパイル出来る。

コンパイルしてみる。

> cmake --build . --config release

これで,releaseディレクトリにhello.exeが出来ているので,実行してみる。

> release\hello
Hello!

新しくコメントをつける

題名
ゲスト名
投稿本文
より詳細なコメント入力フォームへ

トップ   凍結 差分 バックアップ 複製 名前変更 リロード   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom
Counter: 802, today: 3, yesterday: 0
最終更新: 2020-12-26 (土) 16:07:38 (JST) (1217d) by yuji