Makeのパターンルール

たとえば make step3 を実行したときに:

  1. step3.cs をビルドして
  2. そのあと mono step3.exe を実行

…という動作にしたい、ということですよね?


目次

✅ 方法:パターンルールを使う

以下のように Makefile を書き換えれば、
make step3 など任意のターゲット名でビルド&実行ができます。


🔧 改良版 Makefile

# C# ソースコードをコンパイルするためのコマンド
CSC = mcs
GTK_FLAGS = -pkg:gtk-sharp-3.0

# ソース一覧
SOURCES = step1 step2 step3 step4 step5 step6 step7 cs_anime_DrawingArea_0010 cs_anime_DrawingArea_0020

# デフォルト:ビルド & 実行
all: build run

# 全部ビルド
build: $(SOURCES:%=%.exe)

# 全部実行
run:
	@for exe in $(SOURCES); do mono $$exe.exe; done

# 共通ルール:.exe を作って実行
%: %.cs
	$(CSC) $(GTK_FLAGS) -out:$@.exe $< && mono $@.exe

# 単純にビルドだけしたいとき
%.exe: %.cs
	$(CSC) $(GTK_FLAGS) -out:$@ $<

# クリーンアップ
clean:
	rm -f *.exe

✅ 使い方

コマンド内容
make step3step3.cs をコンパイル → mono step3.exe を実行
make build全部ビルド(実行はしない)
make runビルド済みの .exe をすべて実行
make clean.exe を削除

これで make stepX でビルドから実行までスムーズにできます!

改良前

# Makefile

# C# ソースコードをコンパイルするためのコマンド
CSC = mcs
GTK_FLAGS = -pkg:gtk-sharp-3.0

# 出力ファイル名
OUT = app.exe

# 各ステップのターゲット
all: step3

step1:
	$(CSC) $(GTK_FLAGS) -out:step1.exe step1.cs
	./step1.exe

step2:
	$(CSC) $(GTK_FLAGS) -out:step2.exe step2.cs
	mono step2.exe

step3:
	$(CSC) $(GTK_FLAGS) -out:step3.exe step3.cs
	mono step3.exe

Program:
	$(CSC) $(GTK_FLAGS) -out:Program.exe Program.cs
	mono Program.exe
	
# クリーンアップ用
clean-all:
	rm -f *.exe

# Dot Net 用
run:
	dotnet run

build:
	dotnet build

clean:
	dotnet clean

よかったらシェアしてね!
目次