上野家のホームページ
ナーマル,マリン,ココ
[
新規
|
一覧
|
検索
|
最新
|
ヘルプ
]
開発/cmake
のソース
資料室
開発
/
cmake
のソース
[
差分
|
バックアップ
|
リロード
]
[ ]
差分
を表示
開発/cmake
へ行く。
* CMake [#qafcc222] CMakeは,ソフトウェアのビルドの自動化ツール。 WindowsではVisual Studioを使って,LinuxなんかだとMakeなどのツールを使ってプログラムのコンパイルを行うことが多いが,使用するライブラリや動作環境に応じてさまざまな設定を行う必要がある。~ 例えば,OpenGLやOpenCVなどを利用するプログラムをコンパイルするとき,ヘッダファイルの場所,ライブラリー名やその場所などの指定などがあるが,それらの具体的な設定定義がめんどくさい。 CMakeはこの煩雑な設定作業を手助けしてくれるツール。簡単なテキストファイルを一つを作れば,後の処理はCMakeが自動的に処理してくれる。Windows,Linux, Mac等の環境も考慮して処理されるためマルチプラットフォームで作る必要があるプログラムでもビルド出来るようになる。 詳細は,~ - [[公式サイトのチュートリアル>https://cmake.org/cmake/help/latest/guide/tutorial/index.html]]~ - [[OpenCVのCMakeチュートリアル>http://opencv.jp/cmake/cmake_tutorial.html]]~ なんかを読みましょう。 ** Windowsマシンにインストール [#q13be7b8] cmake for Windowsは,Windowsで動作するcmake。これをインストールしてみる。 [[公式サイト>https://cmake.org/download/]]から,最新版の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。 * CMakeを使ってみる [#x5325fb5] 適当なディレクトリで以下のようなソースコードを作成する。~ ''hello.c'' #code(c){{ #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やプロジェクトファイルを作成する作業。作成したいプロジェクトファイルの種類を指定して実行させます。 ** CMakeによるビルド [#lb7b2f52] 上記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!
開発/cmake のバックアップソース(No. All)
現: 2020-12-26 (土) 15:07:38
yuji
Counter: 810, today: 1, yesterday: 0
Copyright©2008 Yuji Ueno All Rights Reserved.
ログイン
ユーザ名:
パスワード:
IDとパスワードを記憶
パスワード紛失
メインメニュー
ホーム
でぶlog
資料室
最新ページ一覧
全ページ一覧
ヘルプ
» 関連ページ
» Wikiソース
» 編集履歴
» バックアップ一覧
» 添付ファイル一覧
フォーラム
お問い合わせ