はじめてのCMake
さて,外部計算機に接続して再生ボタンは出るのですが,動作しないことがあります:
あれ?今日は動いちまったな.でも, lauhch.jsonだとかtasks.jsonだとか,なんか色々言ってきてうまくいかないことも多い.再生ボタンはデバッグができて便利なので動かせたら良いが,それはここで解説することにして, ここでは,再生ボタン以外で,プログラムをビルドする方法も学びましょう.
インストール
あなたのMacのVSCodeで, MicrosoftのCMake Toolsをインストールしましょう.こいつは, twxs氏が作成したツールを強化したものです.
CMakeLists.txt作成
さて, VSCodeでローカルのフォルダーを開いています.で, CMakeLists.txt というファイルを作成し, お助けモンキーに書いてもらいましょう!
保存できたら,CMakeの基本の操作方法を教えてもらいましょう!
なお,後日,さるが申すに,以下の一行を書いといた方がAppleでは幸せである,そうだ
cmake_minimum_required(VERSION 3.10)
set(CMAKE_OSX_SYSROOT macosx) ←これを入れといた方がMacでは幸せ
project(MultiTargets)
# P1ターゲット
add_executable(P1 P1/main.cpp)
....
先に言えよ
あなたのMacでビルド
教えてもらったので,下の方のターミナルでやってみましょう!
arm64:PROG_EXE $ pwd ←念のため,現在位置を確認 /Users/sugimoto/test/PROG_EXE ←CMakeLists.txt のある場所ですね! arm64:PROG_EXE $ mkdir build arm64:PROG_EXE $ cd build/ /Users/sugimoto/test/PROG_EXE/build arm64:build $ cmake .. -- The C compiler identification is AppleClang 17.0.0.17000013 -- The CXX compiler identification is AppleClang 17.0.0.17000013 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (1.7s) -- Generating done (0.0s) -- Build files have been written to: /Users/sugimoto/test/PROG_EXE/build ←なんか準備完了的な感じ
射撃準備完了!
- 一応解説しとくと, このへんでは, どうやらMacで実行していることに気づき
- ここいらで, Macだからヒゲ親父のgccではなくて, LLVMのclangを使うように自動設定しているっぽい. まあ,初心者は動けばどうでも良いはずだ!
ま,このように, CMakeってのは, 自分で自分がどんなコンピュータであるのかを調べ,まあ指定されてなければ適当な感じでビルド準備をするシステムです.
実際にビルドするのは,CMakeの元祖のmakeというコマンドです(makeを上手に設定するのが神の領域まで複雑化したので,CMakeができた).
では,撃て!
arm64:build $ make P1 ←P1フォルダーのを再生ボタンクリック [100%] Built target P1 ←できた,っうんだから,できたに相違ない arm64:build $ ./P1 ←CMakeはビルドするだけで実行はしない.あなたが,実行してね エンジン1の回転数は1000 エンジン2の回転数は2000 arm64:build $ make P2 [ 50%] Building CXX object CMakeFiles/P2.dir/P2/main.cpp.o [100%] Linking CXX executable P2 [100%] Built target P2 arm64:build $ ./P2 noname-engine created noname-engine created engine:1 rotation=1000 engine:2 rotation=2000 arm64:build $ make P3 [ 50%] Building CXX object CMakeFiles/P3.dir/P3/main.cpp.o [100%] Linking CXX executable P3 [100%] Built target P3 arm64:build $ ./P3 Hello, World! Starting solver-B x=1, y=0.36417 error= -0.00370976
あなたのMacではうまく動くようになりました!忘れずに
- 昔は,忘れずに「保存」でした.それは,すでに過去のものになりました.我々は,保存などという年寄り臭いことはしません.
- コミットしてプッシュしましょう!
ですが,その前に,一つだけ追加のコマンドを入力しておきましょう:
arm64:build $ git config pull.rebase false
忘れずにPUSH
そもそもVSCodeが「プッシュしてねえぞボケ〜」マークを出してくるので
この辺と同じように,コミット・プッシュしておきましょう.
計算機クラスタでビルド
では, あなたのMacのVSCodeで, 外部の計算機フォルダーを開いてみましょう.
変更があったはずなのに,気づいてないです.「起動するたびにGitサイトをチェックして,更新があったらダウンロード」という行動はしないのです.ですので,自分でリフレッシュしてください.
どうやら同期できたみたいです.では, ここでもビルドしてみましょう.作業内容は全く同じ:
[sugimoto@sun0 PROG_EXE]$ pwd ←念のため,現在位置を確認 /net/sun0/sugimoto/test/PROG_EXE ←CMakeLists.txt のある場所ですね! [sugimoto@sun0 PROG_EXE]$ mkdir build [sugimoto@sun0 PROG_EXE]$ cd build/ [sugimoto@sun0 build]$ cmake .. -- The C compiler identification is GNU 11.5.0
-- The CXX compiler identification is GNU 11.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /net/sun0/sugimoto/test/PROG_EXE/build ←なんか準備完了的な感じ
射撃準備完了!
- 一応解説しとくと, このへんでは, どうやらヒゲ親父の手下であること(おそらくLinuxであること)に気づき
- ここいらで, Linuxなりの自動設定しているっぽい. まあ,初心者は動けばどうでも良いはずだ!
では,撃て!
[sugimoto@sun0 build]$ make P1
[ 50%] Building CXX object CMakeFiles/P1.dir/P1/main.cpp.o
[100%] Linking CXX executable P1
[100%] Built target P1 [sugimoto@sun0 build]$ ./P1 エンジン1の回転数は1000 エンジン2の回転数は2000 [sugimoto@sun0 build]$ make P2 [ 50%] Building CXX object CMakeFiles/P2.dir/P2/main.cpp.o [100%] Linking CXX executable P2 [100%] Built target P2 [sugimoto@sun0 build]$ ./P2 noname-engine created noname-engine created engine:1 rotation=1000 engine:2 rotation=2000 [sugimoto@sun0 build]$ make P3 [ 50%] Building CXX object CMakeFiles/P3.dir/P3/main.cpp.o [100%] Linking CXX executable P3 [100%] Built target P3 [sugimoto@sun0 build]$ ./P3 Hello, World! Starting solver-B x=1, y=0.36417 error= -0.00370976
あなたのMacと,作業がまんま同じですね.当研究室でMacを買っている理由は
- 操作方法が,Linuxである「計算機クラスタ」「大型計算機センター」と同じである
- MacのノートPCは, 全般的に, WindowsPCより安価である
- スタバで開いていても周囲の目を気にしないで済む
といのが,その理由なのです.